Good Afternoon...

I have an email function that I have been using for a long time to send mail to our membership.

But now I need to be able to send a .pdf as an attachment to them.

I expect that it should be a pretty simple process, but no clue what it is.

Here is the function that I currently use...

//**********************************************
// Function to send emails
//  call with email address / from address / subject / body of message
function mail_sender($to,$from,$subject,$message_body)
{
$message = "<html><head><title>".$sitename."</title></head><body>";
$message=$message.$message_body;
$message .= "</body></html>";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";// More headers
$headers .= "From: <".$from.">" . "\r\n";   //  modified function to include $from
$message = wordwrap($message,70);
// Send email
mail($to,$subject,$message,$headers);
}
//***********************************************

What would be the quickest, cleanest, easiest way to attach a .pdf that I have on file in each email...

Thanks in advance for your response.

Douglas

Recommended Answers

All 4 Replies

I recommend a prebuilt library for that. Trying to setup the headers yourself, which will work across many different mail servers and clients is not easy. You could try PHPMailer or Swiftmailer. There are plenty of libraries that will make this easy to do.

You need to set up your mail to be a multi-part MIME message, which means you need to generate boundaries and MIME headers for each section (message & attachments) as well as an overall/outer header. Additionally, non-text files (binary files) such as pdf's or images should be base64 encoded so they do not become corrupted when transmitted.

Here are the mail functions I wrote and use, which work fine for me sending attachments or using inline images etc.

function mime_attach($content,$filename,$format="text/plain",$encode=0,$embed=0) {
   $mime = "Content-Type: $format; \n"
         . ($encode?"Content-Transfer-Encoding: base64\n":"")
         . "Content-Disposition: ".($embed?"inline":"attachment")."; filename=\"$filename\"\n"
         . "Content-ID: <$filename>\n\n"
         . ($encode?base64_encode($content):$content)."\n";

   return $mime;
}

function send_mail($to,$message,$subject,$format="plain",$attach="",$priority=0) {
   $boundary = "==allyourbasearebelongtous==";

   $headers  = "From: \"Altiora Martial Arts\" <noreply@altiorainsurance.com.au>\n"
             . "Reply-To: <martialarts@altiorainsurance.com.au>\n"
             . "MIME-Version: 1.0\n"
             . "Content-Type: multipart/mixed; boundary=\"$boundary\"";
   if ($priority) $headers .= "X-Priority: 1 (Highest)\n"
                           .  "X-MSMail-Priority: High\n"
                           .  "Importance: High\n"; 

   $message = "--$boundary\n"
            . "Content-Type: text/$format; \n"
            . "Content-Disposition: inline\n\n"
            . "$message\n";

   if (is_array($attach))
      foreach ($attach as $item)
         $message .= "--$boundary\n".$item;
   else if ($attach!="")
      $message .= "--$boundary\n".$attach;

   $message .= "--$boundary--\n";

   mail($to, $subject, $message, $headers);
}

Example usage:

$message = "Test message. <br/>This can include HTML formatting."

//repeat this line for multiple attachments.
$atts[] = mime_attach(file_get_contents("./example.pdf"),"example.pdf","application/pdf",1);

send_mail("recipient@email.com",
        $message,
        "Test Email Subject",
        "html",
        $atts);

hi

I want my email message to look something like this.

Hello user,
Pls find the attachment file.

Thanks
For more info

xyz,
street No.
State

Country.

but wen i tried in the code,there is no file attached instead the contents are printed in email body with some ascii code.i tried putting \n and <br> in the code for formatting,but same problem.
If the email msg is of a single line,say."Hello user.Pls find the attachment .".Its working fine else the above problem

Pls help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.