Reputation: 23
I´m trying to send multiple attachments with 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:
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('[email protected]', '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.
Upvotes: 0
Views: 727
Reputation: 37810
You're passing in URLs instead of local paths, which is deliberately not supported by addAttachment
. PHPMailer is not an HTTP client, so fetch the files yourself, and then pass them to 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):
$data = file_get_contents($url);
$mail->addStringAttachment($data, 'file.jpg');
You might want to do some error checking around these too.
Upvotes: 2
Reputation: 450
<?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 = '[email protected]';
$mail->FromName = 'Mailer';
$mail->addAddress('[email protected]', '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
Upvotes: 0