【问题标题】:PhpMailer Multiple AttachmentsPhpMailer 多个附件
【发布时间】:2019-01-22 17:22:35
【问题描述】:

我正在尝试使用 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]);
      }
 }

非常感谢您的合作。

【问题讨论】:

    标签: php phpmailer


    【解决方案1】:

    您传递的是 URL 而不是本地路径,addAttachment 故意不支持。 PHPMailer 不是 HTTP 客户端,因此请自己获取文件,然后将它们传递给 PHPMailer。例如:

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

    或者,跳过将其写入文件并将其作为字符串传递(确保传入文件名或设置 MIME 类型 - see PHPMailer docs on that):

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

    您可能还想围绕这些进行一些错误检查。

    【讨论】:

      【解决方案2】:
      <?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';
      

      试试这个,它对我有用。您可以添加多个添加附件以发送附件

      【讨论】:

      • 他已经这样做了。 - 对addAttachment 的调用有一个循环。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 2011-09-13
      • 1970-01-01
      • 2016-10-28
      • 1970-01-01
      • 2017-04-29
      相关资源
      最近更新 更多