簡體   English   中英

PhpMailer多個附件

[英]PhpMailer Multiple Attachments

我正在嘗試使用phpmailer發送多個附件。 我得到了我要發送的文件的完整URL,並通過for loop將其放入$mail->addAttachment參數,但是當我嘗試發送時會引發錯誤:

無法訪問文件:....

 // ADJUNTOS
 $urls_x = explode(',',$urls);

 // QUITA EL ULTIMO ELEMENTO DE LA LISTA QUE VIENE VACIO
 $unset = count($urls_x);
 unset($urls_x[$unset-1]);
 $urls_count = count($urls_x);

 $nombre = $paciente['nombre1'].' '.$paciente['nombre2'].' 
 '.$paciente['apellido1'].' '.$paciente['apellido2'];
 $correo = strtolower($paciente['email']);

 $mail = new PHPMailer(TRUE);
 try {
      $mail->CharSet="utf-8";
      $mail->setFrom('sender_x@xxxx.com.co', 'SENDER');
      $mail->addAddress($correo, $nombre);
      $mail->Subject = 'XXXX SUBJECT';
      $mail->IsHTML(true);
      $mail->AddEmbeddedImage('../../img/mail/body.png', 'bodyimg',  
      '../../img/mail/body.png');
      $mail->Body = "<img src=\"cid:bodyimg\" />";

      for($i=0;$i<$urls_count;$i++){
     $mail->addAttachment($urls_x[$i]);
      }
 }

非常感謝您的合作。

您傳遞的是URL而不是本地路徑,這是addAttachment故意不支持的。 PHPMailer不是HTTP客戶端,因此請自己獲取文件,然后將其傳遞給PHPMailer。 例如:

file_put_contents('/tmp/file.jpg', file_get_contents($url));
$mail->addAttachment('/tmp/file.jpg');

或者,跳過將其寫入文件並以字符串形式傳遞(確保您傳遞文件名或設置MIME類型- 參見PHPMailer文檔 ):

$data = file_get_contents($url);
$mail->addStringAttachment($data, 'file.jpg');

您可能還想對這些內容進行一些錯誤檢查。

<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();          
$mail->Host = 'smtp1.example.com'; 
$mail->SMTPAuth = true;
$mail->Username = 'jswan';
$mail->Password = 'secret';
$mail->SMTPSecure = 'tls';  
$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('josh@example.net', 'Josh Adams');  // Add a recipient
$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name                               
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}
echo 'Message has been sent';

試試這個,對我有用。 您可以添加多個添加附件以發送附件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM