PHPMailer无法附加pdf文件

时间:2014-06-24 10:57:34

标签: php email attachment phpmailer

我一直在尝试发送带有pdf附件的电子邮件。我使用PHPMailer但我似乎无法管理它。我一直在尝试大量不同的附加方法,并尝试了不同的文件路径。这是代码:

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

1 个答案:

答案 0 :(得分:0)

在以下行中:

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

尝试将其更改为:

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

您已经有了要附加的PDF的路径,但是还需要再次提供其文件名。 我在尝试其他PHPmailer功能时已经对此进行了测试