adding attachment from form while sending mail from gmail via php -
i trying add atatchment while sending mail gmail using php. shows error...it says could not access file: hai.jpg
follwing code use
gmail.php
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>phpmailer - gmail smtp test</title> </head> <body> <?php access date_default_timezone_set('etc/utc'); require '../mail/class.phpmailer.php'; $mail = new phpmailer(); $mail->issmtp(); $mail->smtpdebug = 2; $mail->debugoutput = 'html'; $mail->host = 'smtp.gmail.com'; $mail->port = 587; $mail->smtpsecure = 'tls'; $mail->smtpauth = true; $mail->username = "name@gmail.com"; //password use smtp authentication $mail->password = "passwrd"; $mail->setfrom($_post['from'], $_post['from_name']); $mail->addreplyto($_post['from'],$_post['from_name']); $mail->addaddress($_post['to'],$_post['to_name']); $mail->subject = 'phpmailer gmail smtp test'; $mail->msghtml($_post['message']); $mail->altbody = 'this plain-text message body'; $mail->addattachment($_post['attachment'],'application/octet-stream'); if(!$mail->send()) { echo "mailer error: " . $mail->errorinfo; } else { echo "message sent!"; } ?> </body> </html>
index.html
<form action="gmail.php" method="post"> <div> name:<input type="text" name="from_name" /> from:<input type="text" name="from" /> </div> <div> name:<input type="text" name="to_name" /> to:<input type="text" name="to" /> </div> <div> message:<textarea name="message"></textarea> </div> <div> attachment:<input type="file" name="attachment" /> </div> <input type="submit" value ="submit"/> </form>
i dont know if doing right way here.can please guide me through this?
i've notice absence of 2 itens in code:
- the use of php $_files variable, stores information uploaded files
- the use of enctype attribute in form element, required upload files along form.
so, code must treat 2 itens.
your form this:
<form action="gmail.php" method="post" enctype="multipart/form-data"> <div> name:<input type="text" name="from_name" /> from:<input type="text" name="from" /> </div> <div> name:<input type="text" name="to_name" /> to:<input type="text" name="to" /> </div> <div> message:<textarea name="message"></textarea> </div> <div> attachment:<input type="file" name="attachment" /> </div> <input type="submit" value ="submit"/> </form>
and code may treat $_files
variable:
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>phpmailer - gmail smtp test</title> </head> <body> <?php //access date_default_timezone_set('etc/utc'); require '../mail/class.phpmailer.php'; $mail = new phpmailer(); $mail->issmtp(); $mail->smtpdebug = 2; $mail->debugoutput = 'html'; $mail->host = 'smtp.gmail.com'; $mail->port = 587; $mail->smtpsecure = 'tls'; $mail->smtpauth = true; $mail->username = "name@gmail.com"; //password use smtp authentication $mail->password = "passwrd"; $mail->setfrom($_post['from'], $_post['from_name']); $mail->addreplyto($_post['from'],$_post['from_name']); $mail->addaddress($_post['to'],$_post['to_name']); $mail->subject = 'phpmailer gmail smtp test'; $mail->msghtml($_post['message']); $mail->altbody = 'this plain-text message body'; //attachs file if uploaded http post if (is_uploaded_file ($_files['attachment']['tmp_name'])) { $mail->addattachment($_files['attachment']['tmp_name'],$_files['attachment']['name'], 'base64',$_files['attachment']['type']); } if(!$mail->send()) { echo "mailer error: " . $mail->errorinfo; } else { echo "message sent!"; } ?> </body> </html>
Comments
Post a Comment