[英]PhpMailer Multiple Attachments
I´m trying to send multiple attachments with phpmailer. 我正在尝试使用phpmailer发送多个附件。 I get the whole url of the files I'm trying to send and with a for loop
I put it into the $mail->addAttachment
parameter, but when i try to send it throws the error: 我得到了我要发送的文件的完整URL,并通过for loop
将其放入$mail->addAttachment
参数,但是当我尝试发送时会引发错误:
Could not access file:.... 无法访问文件:....
// 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]);
}
}
Thanks a lot for your cooperation. 非常感谢您的合作。
You're passing in URLs instead of local paths, which is deliberately not supported by addAttachment
. 您传递的是URL而不是本地路径,这是addAttachment
故意不支持的。 PHPMailer is not an HTTP client, so fetch the files yourself, and then pass them to PHPMailer. PHPMailer不是HTTP客户端,因此请自己获取文件,然后将其传递给PHPMailer。 For example: 例如:
file_put_contents('/tmp/file.jpg', file_get_contents($url));
$mail->addAttachment('/tmp/file.jpg');
Alternatively, skip writing it to a file and pass it as a string (make sure you pass in a filename or set the MIME type - see PHPMailer docs on that ): 或者,跳过将其写入文件并以字符串形式传递(确保您传递文件名或设置MIME类型- 参见PHPMailer文档 ):
$data = file_get_contents($url);
$mail->addStringAttachment($data, 'file.jpg');
You might want to do some error checking around these too. 您可能还想对这些内容进行一些错误检查。
<?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';
try this, its working for me. 试试这个,对我有用。 you can add multiple add attachment to send attachments 您可以添加多个添加附件以发送附件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.