我正在尝试使用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]);
}
}
非常感谢您的合作。
答案 0 :(得分:1)
您传递的是网址而不是本地路径,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');
您可能还希望对这些内容进行一些错误检查。
答案 1 :(得分:0)
<?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';
尝试一下,它对我有用。您可以添加多个添加附件以发送附件