如何使用PHPMailer将远程文件附加到电子邮件

时间:2019-06-03 07:15:48

标签: php phpmailer

我尝试使用PHP Mailer附加本地文件。仅当附件文件位于自己的服务器中时,我才会获得附件,但是当我尝试从c驱动器中附加文件时,说[C:\ Users \ emp10144 \ Downloads],正在获得附件,但页面空白。我需要修改编码吗?下面是我使用的编码。

$mail->From = 'admin123@sampledemos123.online';
$mail->FromName = 'Admin';
$mail->AddAddress('targetmail@gmail.com', 'User');  // Add a recipient
//$mail->AddAddress('ellen@example.com');               // Name is optional
//$mail->AddAttachment('Daily_Milk_Report.csv','Daily_Milk_Report.csv'); 
This is working fine as the attcahment file is in own server
$filename = "C:\Users\emp10144\Downloads','Daily_Milk_Report.csv"; // Need to attcah this file from C drive/folder.
//$string = file_get_contents("C:\Users\emp10144\Downloads\sample.pdf");
$mail->AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/vnd.ms-excel');
$mail->IsHTML(true);                                  // Set email format to HTML

1 个答案:

答案 0 :(得分:-1)

否,您无法将URL传递到addAttachment并获取资源。

这是故意的; PHPMailer不是HTTP客户端,因此积极避免成为一个客户端。如果要执行此操作,则需要自己负责获取,这很容易做到:

$mail->addStringAttachment(file_get_contents($url, 'myfile.png'));
相关问题