Claus
Claus

Reputation: 550

Unable to attach pdf to email in php

I've tried to follow the examples given in this forum, like the example given by freestate here - Error with PHP mail(): .

Below is my code copied from his revised example. It emails fine. And an attachment is shown in the email when received. However it cannot be opened and is always 125k in size.

I have verified that a file size is returned with "$file_size = filesize($file);". Also that the "fopen" does open the stream. In fact before I added the Multipart to the header, I could see the content in the body of the text (albeit not readable).

I'm only trying to attach a basic pdf file, on a window's platform (windows 10), Apache 2.4, PHP 5.6.16.0.

What would cause the attachment to fail to attach properly?

    emailer('//serverName/path/', 'documentName.pdf');

    function eMailer($path, $filename)
    {
        $eol = PHP_EOL;
        $to = "[email protected]";
        $subject = "File Name: ".$filename;

        $body = "The this the written content of the body";

        $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()));

        $header = "From: NameOfPerson <[email protected]>".$eol.
              "MIME-Version: 1.0\r\n".
              "Content-Type: multipart/mixed; boundary=\"".$uid."\"";

        $message = "--".$uid.$eol.
             "Content-Type: text/html; charset=ISO-8859-1".$eol.
             "Content-Transfer-Encoding: 8bit".$eol.$eol.
             $body.$eol.

             "--".$uid.$eol.
             "Content-Type: application/pdf; name=\"".$filename."\"".$eol.
             "Content-Transfer-Encoding: base64".$eol.
             "Content-Disposition: attachment; filename=\"".$filename."\"".$eol.
             $content.$eol.
             "--".$uid."--";

        echo (mail($to, $subject, $message, $header))?'success':'failure';  
    }

Upvotes: 1

Views: 1190

Answers (2)

Gopal Satpati
Gopal Satpati

Reputation: 136

Try below code:

$name           = "Name";
$email      = "Email Address";
$to             = "$name <$email>";
$from           = "XYZ";
$subject        = "TEST SUBJECT";
$mainMessage    = "Hi, here's the file.";
$fileatt        = "./test.pdf";
$fileatttype    = "application/pdf";
$fileattname    = "newname.pdf";
$headers        = "From: $from";
// File
$file           = fopen ( $fileatt, 'rb' );
$data           = fread ( $file, filesize ( $fileatt ) );
fclose ( $file );
// Attach the file
$semi_rand  = md5 ( time () );
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers       .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
$message        = "This is a multi-part message in MIME format.\n\n" . "-{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\n" . "Content-Transfer-Encoding: 7bit\n\n" . $mainMessage . "\n\n";
$data           = chunk_split ( base64_encode ( $data ) );
$message      .= "--{$mime_boundary}\n" . "Content-Type: {$fileatttype};\n" . " name=\"{$fileattname}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileattname}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "-{$mime_boundary}-\n";
// Send Email
if (mail ( $to, $subject, $message, $headers )) {   
    echo "The email was sent.";
} else {    
    echo "There was an error sending the mail.";
}

Upvotes: 0

Claus
Claus

Reputation: 550

So after spending considerable time trying to solve the attachment problem by myself, I decided to use one of the two common packages out there PHPMailer and Swiftmailer. Both seemed pretty good. I work on just one enterprise project and I do not have Composer installed. So I wanted something easy to download and plug into my project manually. After reading the installation instructions for swiftmailer, I decided this would be easy for me to install and use.

These are the steps I took

  1. I downloaded the Swiftmailer zip file from Github
  2. From the unzipped file I copied the "lib" folder right into my project "C:\myproject\lib"
  3. Then I read the usage instructions in the online manualenter image description here
  4. To use it in my project all I needed to do was include it in my code like this:

        function eMailer($path, $filename, $mailto, $subject, $message, $user)
    {
        $file = $path.$filename;
        require_once 'C:\myProject\lib\swift_required.php';
    
        // Create the SMTP configuration
        $transport = Swift_SmtpTransport::newInstance("mymail_server_address ie: 192.111.22.33", 25);
        $transport->setUsername($user['email']);
        $transport->setPassword($user['password']);
    
        // Create the mail transport configuration
        $transport = Swift_MailTransport::newInstance();
    
        // Create the message
        $message = Swift_Message::newInstance();
        $message->setTo(array(
          $mailto['email']=> $mailto['name']
        ));
        $message->setSubject($subject);
        $message->setBody($message);
        $message->setFrom($user['email'], $user['name']);
        $message->attach(Swift_Attachment::fromPath($file));
    
        // Send the email
        $mailer = Swift_Mailer::newInstance($transport);
        $mailer->send($message);    
    }
    

This obviously has much more capabilities than I need right now, but if in the future I need a mailer function in another part of my project it's there ready for me to use.

Upvotes: 2

Related Questions