通过php从gmail发送邮件时从表单添加附件

时间:2013-07-31 19:06:10

标签: php gmail bulk-mail

我正在尝试使用php从gmail发送邮件时添加atatchment。但它显示错误......它说Could not access file: hai.jpg

以下是我使用的代码

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>

我不知道如果我在这里做正确的方法。有谁可以指导我完成这个?

2 个答案:

答案 0 :(得分:1)

我注意到代码中没有两个itens:

  1. 使用php $_FILES变量,该变量存储有关上传文件的信息
  2. 在表单元素中使用enctype属性,这是将文件与表单一起上传所必需的。
  3. 所以,你的代码必须对这两个人进行处理。

    您的表单将如下所示:

    <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>
    

    您的代码可能会处理$_FILES变量:

    <!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>
    

答案 1 :(得分:0)

不是那么直接将文件从客户端浏览器上传到服务器。

Here's a simple tutorial