0
Hello I’m having trouble generating an email with an attachment. The attachment is a PDF file generated by the JSPDF library. First I started by generating the pdf and calling the php script through ajax.
var pdf = doc.output();
$.post(
"/smt/mail.php",
{
data: pdf
},
function(data) {
console.log(data.resultado);
}
, 'json');
I also tried with the line of code var pdf = btoa(doc.output());
but the mistake seems to be the same In the PHP script I have:
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
if(count($files) > 0){
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}\n";
$fp = fopen($files[$i],"rb");
$data = fread($fp,filesize($files[$i]));
fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
}
$message .= "--{$mime_boundary}--";
$mail = new PHPMailer(true);
try{
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
//$mail->SMTPDebug = 1;
$mail->CharSet = 'UTF-8';
//$mail->Host = 'smtp.office365.com';
$mail->Host = 'mail2.mailbox.pt';
$mail->Username = "****"; // SMTP account username example
$mail->Password = "****"; // SMTP account password example
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->From = "***";
$mail->FromName = "******";
$mail->AddAddress($to, "Test");
$mail->AddReplyTo("****", '*****');
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->Send())
With this code I can send the mail however without the attachment
After a little debug I made sure the condition line if(is_file[$i])
returned.
someone can help me?
Thanks in advance
Welcome to sopt, the site is in Portuguese, translate your question.
– user28595
I translated the original
– André