为什么PHPmailer没有发送附件?

时间:2018-01-08 16:28:46

标签: php phpmailer

如果我将以下代码直接用于文件,例如test.php页面然后它发送带附件的电子邮件。

$email = new PHPMailer();
$email->From      = 'from address';
$email->FromName  = 'Your Name';
$email->Subject   = 'Message Subject';
$email->Body      = '<h1>hello message</h1>';
$email->AddAddress( 'to@gmail.com' );
$file_to_attach =  $path = '/home/xxxx/public_html/pdf/'.'1254.pdf';
$email->AddAttachment( $file_to_attach , '1254.pdf' );
return $email->Send();

但是,如果我使用类的方法发送带有上述相同代码的附件的电子邮件,则它不会发送附件(邮件正在发送)。

邮件类:

public static function sendMail ($to, $subject, $message, $from, $fromName, $attachment, $attachmentName) {

    $email            = new PHPMailer();

    $email->isHTML(true);
    $email->From      = $from;
    $email->FromName  = $fromName;
    $email->Subject   = $subject;
    $email->Body      = $message;
    $email->AddAddress($to);    
    $file_to_attach   = $attachment;
    $email->AddAttachment( $file_to_attach , $attachmentName ); 

    return $email->Send();

}

发送电子邮件:

$path           = '/home/xxx/public_html/pdf/'.$confirm_id.'.pdf';
$pathFileName   = $confirm_id.'.pdf';

$message = "Hello $name";    

$confirmMail = Mail::sendMail($email, "xxxx pdf confirmation", $message, "xxxx@xxxx.org", "xxxx.org", $path, $pathFileName );

if(!$confirmMail) {
    echo 'Confirmation mail is not sent';
}

你能告诉我我在哪里做错了吗?!

0 个答案:

没有答案