PHPMailer AddAttachment不起作用

时间:2015-04-15 02:41:49

标签: php phpmailer

所以我有以下内容......邮件是在没有附件的情况下发送的,我似乎无法找到原因。任何帮助将受到高度赞赏:)

$mail = new PHPMailer();

$mail->Host     = "localhost"; // SMTP server
$mail->From     = $dwwp_email;
$mail->FromName = "Bla bla title";
$mail->addReplyTo($dwwp_email);
$mail->IsHTML('true');

$mail->Username = "some dorky username";
$mail->Password = "letmeinMan";
$mail->AddAddress($to);

$mail->Subject  = $subject; 
$mail->AddAttachment('../attach/tnc.pdf', 'TnC.pdf');
$mail->Body     = $message;

if (!$mail->Send()) {
    echo 'Message was not sent.';
    echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent.';
}

2 个答案:

答案 0 :(得分:2)

我要做的第一件事是检查addAttachment()的返回值。根据{{​​3}}:

“如果无法找到或读取该文件,则返回false。”

假设您的文件实际存在于您引用的位置,请检查并确保您(或执行此代码的任何进程)具有读取权限。如上所述,使用完整路径(可能)是一个好主意。

此外,它应该无关紧要,但通常的好习惯是使用与声明中出现的相同的大小写来调用函数。在两种情况下,类声明与您编写它的方式不同(从小写开始):

$mail->addAttachment('/full/path/to/attach/tnc.pdf', 'TnC.pdf');
$mail->addAddress($to);

答案 1 :(得分:-2)

这是我为自己写的,并且可以完美地处理任何类型的附件。

发布参数:

1. from       -- from email id
2. subject    -- subject of email
3. message    -- message content of email
4. secret     -- UPqHPVAA7RV3eGZ1CySqAj1OM1eiurgbh8e7rgbergre87ge9rb
5. filename   -- name of file
6. path       -- path of the file

如果您愿意,可以使用普通的PHP变量替换post参数,或者只需通过POST调用发布这些参数


以下是我的邮件文件的代码

$result = array();
if(isset($_POST['from']) && isset($_POST['subject']) && isset($_POST['message']) && isset($_POST['secret']) && isset($_POST['filename']) && isset($_POST['path']))
{
    if(isset($_POST['to'])){
            $to = $_POST['to'];
    }else{
            $to = "my_email@live.com"; 
}
$path=$_POST['path'];
$filename=$_POST['filename'];
$file = $path.$filename;

$content = file_get_contents($file);

$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);

$subject = $_POST['subject'];
$message = $_POST['message'];
$secret = "UPqHPVAA7RV3eGZ1CySqAj1OM1eiurgbh8e7rgbergre87ge9rb"; //This is required to use this script. It ensures that only we can use this script.
$from = $_POST['from'];
$headers = "From:" . $from."\r\n";
$headers.="MIME-Version: 1.0\r\n";    
$headers.= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$headers.= "This is a multi-part message in MIME format.\r\n";
$headers.= "--".$uid."\r\n";
$headers.= "Content-type:text/html; charset=ISO-8859-1\r\n";
$headers.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers.= $message."\r\n\r\n";
$headers.= "--".$uid."\r\n";
$headers.= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$headers.= "Content-Transfer-Encoding: base64\r\n";
$headers.= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$headers.= $content."\r\n\r\n";
$headers.= "--".$uid."--";


if(strlen($_POST['secret'])!=100)
{
    $result['sent'] = FALSE;
    $result['error'] = "whatsmyname";//Malicious Script Use Attempt Thwarted.
    echo json_encode($result);
    exit();
}
if($secret == $_POST['secret'])
{
    if(mail($to,$subject,$message,$headers))
    {
        $result['sent'] = TRUE;
        echo json_encode($result);
        exit();
    }
    else
    {
        $result['sent'] = FALSE;
        $result['error'] = "mailserver";//Mail Server Down.
        echo json_encode($result);
        exit();
    }
}
else
{
    $result['sent'] = FALSE;
    $result['error'] = "whatsmyname";//Malicious Script Use Attempt Thwarted.
    echo json_encode($result);
    exit();
}

}

您可以以HTML格式创建邮件内容。意味着您可以将图像添加到邮件内容,为内容添加颜色代码以及使用html执行的所有其他操作。 以下是邮件正文的示例:

$message='<html>                            
          <body>
            <font size="3">Name : '. $name.' </font><br /><br />
            <font size="3">Email : '. $email.' </font><br /><br />
            <font size="3">Contact Number : '.$phonenumber.' </font><br /><br />
            <font size="3">Registration Date: '.$signupdate.' </font><br /><br />       
            <br />
            Thanks, <br />
        </body>
        </html>';

干杯!!!!