使用PHPmailer发送附件

Use PHPmailer to send attachment

我正在使用 PHPmailer 库发送电子邮件,我需要发送附件。

当前文件存储在服务器上,我有 link 文件存储在 var $link.

我正在使用以下方法尝试添加附件,但收到的电子邮件没有附件:

$phpmailer->AddAttachment('$link');

我也尝试过使用硬编码路径而不是我的变量:

$phpmailer->AddAttachment('http://mysite.co.uk/link/to/my/file.pdf');

电子邮件出现但没有任何附件。

我尝试使用 phpmailerException 来抓取任何错误,但没有得到任何响应。

我还能如何使用 PHPmailer 发送附件?

完整 PHP 代码:

require_once 'library/PHPMailer.php';
        $phpmailer = new PHPMailer();
        $phpmailer->isSMTP(); //switch to smtp
        $phpmailer->Host = 'smtp.sendgrid.net';
        $phpmailer->SMTPAuth = true;
        $phpmailer->SMTPSecure = 'ssl';
        $phpmailer->Port = 465;
        $phpmailer->Username = 'username';
        $phpmailer->Password = 'password';
        $phpmailer->AddAttachment('http://mysite.co.uk/blah/cv.pdf');
        //$phpmailer->AddAttachment('$link');

        $subject = 'Job Application';

        $messageConfirmation = "The <b> 'Apply for Job' </b> form has recently been completed on <b>www.mysite.com.</b><br/><br/> <b>Please see contact details below:<br /><br/> </b>";
        $messageConfirmation.= "<b>Applying for:</b> $title <br/><br/>";
        $messageConfirmation .= "<b>Name:</b> $name <br/><br/><b>Surname:</b> $surname <br/><br/> <b>Email:</b> $email <br/><br/> <b>Comment:</b> $comment <br/><br/>";
        $messageConfirmation .= "<b>CV:</b> $link<br /><br />";
        $messageConfirmation .= "Please can you call them back in the next 24 hours. <br/><br/> Thank you \n\nMy Site<br/><br/>";


        $imgheader = 'http://blah.co.uk/blah/wp-content/uploads/2014/11/header.png';
        $imgfooter = 'http://blah.co.uk/blah/wp-content/uploads/2014/11/footer.png';

        $message = '<!DOCTYPE HTML>'.
            '<head>'.
            '<meta http-equiv="content-type" content="text/html">'.
            '<title>Email notification</title>'.
            '</head>'.
            '<body>'.
            '<div id="header" style="width: 600px;height: auto;margin: 0 auto;color: #fff;text-align: center;font-family: Open Sans,Arial,sans-serif;">'.
            '<img src="'.$imgheader.'" >'.
            '</div>'.

            '<div id="outer" style="width: 600px;margin: 0 auto;margin-top: 10px;">'.
            '<div id="inner" style="width: 600px;margin: 0 auto; padding-left: 20px;background-color: #fff;font-family: Open Sans,Arial,sans-serif;font-size: 13px;font-weight: normal;color: #444;margin-top: 10px;">'.
            '<p>'.$messageConfirmation .'</p>'.

            '</div>'.
            '</div>'.

            '<div id="footer" style="width: 600px;height: auto;margin: 0 auto;text-align: center;padding: 10px;font-family: Verdena;">'.
            '<img src="'.$imgfooter.'" >'.
            '</div>'.
            '</body>';


        $phpmailer->IsHTML(true);
        $phpmailer->AddAddress(CONTACTUSFORMEMAIL);
        $phpmailer->From = CONTACTUSFORMEMAIL;
        $phpmailer->FromName ='blah';
        $phpmailer->WordWrap   = 50; // set word wrap to 50 characters
        $phpmailer->Subject    = $subject;
        $phpmailer->Body       = $message;
        $phpmailer->Send();



        $data['success'] = true;
        $data['message'] = 'Thank you for your application';

方法AddAttachment接收文件的路径。

如果你看源代码,你会看到它使用fopen函数打开文件,所以,我认为它不适用于链接,你应该自己下载文件并将路径传递给函数。

您好,请尝试以下代码

<?php
        $mail = new PHPMailer();
        $mail->From = $email_from;
        $mail->SetFrom($email_from, $name_from);
        $mail->AddAddress($email_to, $name_to);
        $mail->AddReplyTo($email_from, $name_from);
        $mail->MsgHTML($message_goes_here);
        $mail->Subject = $your_subject;
        $mail->AddAttachment($file_name_goes_here);
        if($mail->Send()) {
        } else {
            echo "\r\nMail not sent. " .  $mail->ErrorInfo;
        }
?>

问题是函数 AddAttachment uses the function is_file() 和文件 returns 在访问 URL 时为 false(这也适用于相对 URL),is_file() 只接受文件路径。所以正常情况下 phpmailer 应该在错误容器中添加一个错误。

我知道这听起来像是一个激进的想法,但您可以尝试 reading the docs, or perhaps checking return values of functions, maybe even trying an up to date version of PHPMailer,或者将您的代码基于最近的示例。

如果您在 $mail = new PHPMailer(true); 实例化时启用它们,PHPMailer 只会抛出异常,正如@TheGreenKey 所说,如果您启用调试,它将设置错误通知并产生错误消息。

如果您想添加 URL 内容作为附件,您可以这样做:

$mail->addStringAttachment(file_get_contents('http://mysite.co.uk/link/to/my/file.pdf'), 'cv.pdf');`

虽然这永远不会是快速、可靠或高效的。

使用 phpmailer 发送任何类型的附件。

<?php
require 'PHPMailer/class.phpmailer.php';
if(isset($_POST['send']))
{
  $file_name = $_FILES['file']['name']; //attachment
  $file_tmp =$_FILES['file']['tmp_name'];
  
    $abc=microtime(); 
    $path="upload/$abc".$file_name;
    move_uploaded_file($file_tmp,$path); //moving attachment to upload folder
     
     
        $mail = new PHPMailer(true); 

        $mail->IsSMTP();                           
        $mail->SMTPAuth   = false;                 
        $mail->Port       = 25;                    
        $mail->Host       = "localhost"; 
        $mail->Username   = "webmail_username";   
        $mail->Password   = "webmail_password";            
    
        $mail->IsSendmail();  
    
        $mail->From       = "abc@your.com";
        $mail->FromName   = "your.com";
    
        $mail->AddAddress('receiver_emailid');
    
        $mail->Subject  = "Thank you for contacting US";
        $mail->WordWrap   = 80; 
    
        $body="Thank You For Contacting Us";

        $mail->MsgHTML($body);
        $mail->IsHTML(true); 
        
        $mail->AddAttachment($path,$file_name); //Attachment File
     
     
        if(!$mail->Send())
        {
               echo "Mail Not Sent";
        }
        else
        {
            echo '<script language="javascript">';
            echo 'alert("Thank You Contacting Us We Will Response You As Early Possible")';
            echo '</script>';
     
        } 
        

}

了解更多详情:- click here