【问题标题】:PHPMailer can't attach a pdf filePHPMailer 无法附加 pdf 文件
【发布时间】:2014-08-14 14:38:42
【问题描述】:

我一直在尝试发送一封包含 pdf 附件的电子邮件。我正在使用 PHPMailer,但我似乎无法管理它。我一直在尝试大量不同的 addattachment 方法,还尝试了不同的文件路径。代码如下:

    require_once ("class.phpmailer.php");
    $lc_name = $_SESSION['lc_name'];

    $filename = "email_attachment/".$lc_id.".pdf";
    require_once ("func_ui.php");

    $vou_mail = func_ui::select_mail($lc_id, 3);

    if($vou_mail["auto"] == 0)
    {
        return;
    }
    else
    {
        $custom_body = $vou_mail["body"];
    }

    $body = "Dear customer,<br><br>

    $custom_body<br><br>

    Kind Regards,<br><br>

    $gm_name<br>
    Centre Manager";

    $mail = new PHPMailer();
    try {
    $mail->IsSMTP();
    $mail->Host = "****"; // SMTP server
    $mail->SMTPAuth = true; // enable SMTP authentication
                            // $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
    $mail->Port = ***; // set the SMTP port for the GMAIL server
    $mail->Username = "******"; // GMAIL username
    $mail->Password = "****"; // GMAIL password
    $from_name = '***';
    $subject = '';
    //$mail->AddAttachment($filename, "", "base64", "application/pdf");
    //$mail->AddAttachment(realpath('./email_attachment/1220.pdf'),'1220.pdf','base64', 'application/pdf');

    $mail->SetFrom('info@***.com', "");
    $mail->AddReplyTo("info@***.com", "");
    $mail->Subject = "$lc_name - Voucher.";
    //$mail->IsHTML(false);
    $reason = $mail->addAttachment("/email_attachment/1220.pdf");
    $mail->AltBody = "To view the message, please use an HTML compatible m_email viewer!"; // optional, comment out and test
    $mail->MsgHTML($body);

    $mail->AddAddress($email_v, "");

    if ($reason == false){
        echo "Didn't like this line\n";
    }
    else
    {
        echo "worked ?";
    }

    if(! $mail->Send())
    {
        echo "Mailer Error: " . $mail->ErrorInfo;
    }
    else
    {
        echo "Message sent!";
    }
    $mailer->ClearAddresses();
    $mailer->ClearAttachments();
    } catch (phpmailerException $e) {
        echo $e->errorMessage(); //Pretty error messages from PHPMailer
    } catch (Exception $e) {
        echo $e->getMessage(); //Boring error messages from anything else!
    }

【问题讨论】:

  • 您是否也尝试过绝对文件路径?
  • 究竟是什么?我正在使用基于 linux 的服务器。那么它应该像 var/public_html/email_attachment/1220.pdf 一样吗?
  • 绝对路径是从根目录开始的路径。在 *nix 世界中它类似于 /var/www/mysite.com/pdfs/mypdf.pdf 或在 Win 世界中类似于:C:\wwwroot\mysite.com\pdfs\mypdf.pdf 注意 *nix 系统上的开头斜线 /var。
  • 立即尝试但似乎没有用:/

标签: php email attachment phpmailer


【解决方案1】:

你有以下行:

$reason = $mail->addAttachment("/email_attachment/1220.pdf");

尝试将其更改为:

$mail->addAttachment('/email_attachment/1220.pdf','1220.pdf');

您有想要附加的 PDF 的路径,但还需要再次提供它的文件名。 我在尝试其他 PHPmailer 函数时对此进行了测试

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多