简体   繁体   中英

adding attachment from form while sending mail from gmail via php

i am trying to add atatchment while sending mail from gmail using php. But it shows error...it says Could not access file: hai.jpg

follwing is the code i 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 to that
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 to use for 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 is a 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>
        From Name:<input type="text" name="from_name" />
        From:<input type="text" name="from" />

    </div>
    <div>
        To 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 what if i am doing the right way here.Can anyone please guide me through this?

I've notice the absence of two itens in your code:

  1. The use of the php $_FILES variable, which stores information about the uploaded files
  2. The use of enctype attribute in your form element, which is required to upload files along with the form.

So, your code must treat this two itens.

Your form will look like this:

<form action="gmail.php" method="POST" enctype="multipart/form-data">
    <div>
        From Name:<input type="text" name="from_name" />
        From:<input type="text" name="from" />

    </div>
    <div>
        To 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 your code may treat the $_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 to that
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 to use for 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 is a plain-text message body';
//Attachs the file only if it was uploaded by 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>

Its not quite that straight forward uploading a file from the client browser to the server.

Here's a simple tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM