PHPMail sends an attachment, but they are empty

Basically, I'm trying to send a PDF via PHPMail. the email is sent and I receive it in Outlook perfectly. The problem is that the application is broken and it does not open. I even tried sending HTML and it is also empty.

I tried to research on the forum, tried several "working codes", and other people worked with this code ... I have no idea why I do not work for me. I am using the latest version of PHPMail 5.2.2

$mail = new PHPMailer(); $staffEmail = "staffemail"; $mail->From = $staffEmail; $mail->FromName = "name"; $mail->AddAddress(' my@email.com '); $mail->AddReplyTo($staffEmail, "name"); $mail->AddAttachment('test.pdf'); $mail->Subject = "PDF file attachment"; $mail->Body = "message!"; $mail->Send(); 
+6
source share
5 answers

This custom PHP function will show a beginner PHP developer how to create an email script with an attachment function. Please note that there are no validation functions available inside the mail function.

 <?php function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) { $file = $path.$filename; $file_size = filesize($file); $handle = fopen($file, "r"); $content = fread($handle, $file_size); fclose($handle); $content = chunk_split(base64_encode($content)); $uid = md5(uniqid(time())); $name = basename($file); $header = "From: ".$from_name." <".$from_mail.">\r\n"; $header .= "Reply-To: ".$replyto."\r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; $header .= "This is a multi-part message in MIME format.\r\n"; $header .= "--".$uid."\r\n"; $header .= "Content-type:text/plain; charset=iso-8859-1\r\n"; $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; $header .= $message."\r\n\r\n"; $header .= "--".$uid."\r\n"; $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here $header .= "Content-Transfer-Encoding: base64\r\n"; $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"; $header .= $content."\r\n\r\n"; $header .= "--".$uid."--"; if (mail($mailto, $subject, "", $header)) { echo "mail send ... OK"; // or use booleans here } else { echo "mail send ... ERROR!"; } } ?> 

Next, we show an example of using this function to send an email message with one zip file attached:

 <?php $my_file = "somefile.zip"; $my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/"; $my_name = "Olaf Lederer"; $my_mail = " my@mail.com "; $my_replyto = " my_reply_to@mail.net "; $my_subject = "This is a mail with attachment."; $my_message = "Hallo,\r\ndo you like this script? I hope it will help.\r\n\r\ngr. Olaf"; mail_attachment($my_file, $my_path, " recipient@mail.org ", $my_mail, $my_name, $my_replyto, $my_subject, $my_message); ?> 

Are you looking for a script to send multiple attachments? Try our script mail attachment class.

If you want to send email messages to your sites via SMTP and Gmail, also check out our PHPMailer tutorial.

+7
source

I suggest you upload the file to the server and then attach it to the email. For example, you can use the following steps:

  • Upload file
  • Attach it to email
  • Send email
  • Delete this temporary file from the server

Try this because I think the reason is your problem.

Just in case: http://php.net/manual/en/features.file-upload.php

Good luck.

+2
source

Two problems are possible: either the wrong path for the attachment, or php does not have permission to this folder

+1
source

this one works for me, trying to send a resume, has attachmnet in zend

  $mail = new Zend_Mail (); $mail->setBodyHTML ( stripslashes ($message) ); // add attachment $fileContents = file_get_contents($attachemnet); $resume = $mail->createAttachment($fileContents); $resume->filename = $EmployeeDeatils['resume']; //$mail->createAttachment($attachemnet); $mail->setFrom ( $mail_template ['from_email'], $mail_template ['from_caption'] ); $mail->addTo ( $clientemail, $employee_name ); $mail->setSubject ($subject ); try { $mail->send (); } catch ( Exception $e ) { $this->_helper->errorlog ( " Send mail to member with activation link : " . $e->getMessage () ); } 
0
source

You can try using this function, which uses the simple PHP mail function:

 function mail_attachment ($from , $to, $subject, $message, $attachment){ $fileatt = $attachment; // Path to the file $fileatt_type = 'application/octet-stream'; // File Type $start= strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1; $fileatt_name = substr($attachment, $start, strlen($attachment)); // Filename that will be used for the file as the attachment $email_from = $from; // Who the email is from $email_subject = $subject; // The Subject of the email $email_txt = $message; // Message that the email has in it $email_to = $to; // Who the email is to $headers = "From: ".$email_from; $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); $msg_txt=""; //\n\nMail created from websiteaddress systems : http://websiteaddress.com $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $headers .= "\nwebsiteaddress-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $email_txt .= $msg_txt; $email_message = "This is a multi-part message in websiteaddress format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n".$email_txt. "\n\n"; $data = chunk_split(base64_encode($data)); $email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; $ok = @mail($email_to, $email_subject, $email_message, $headers); if($ok) { //echo "Attachment has been mailed !"; //header("Location: index.php"); } else { die("Sorry but the email could not be sent. Please go back and try again!"); } } 

Hope this helps you.

0
source

All Articles