Apex Business Support

Contact us now on
0845 1166 081

Reception

Sending multiple email attachments using php

You can send emails using php using the php mail() function.

To add attachments to the email you can also set the header information to allow the email be a multipart message.

To add multiple attachments to your php email sending script you will need to allow the attachment, attachment name and attachment mim type variables to be arrays so that we can load in more than one attachment.

The following php function can be used to send your emails, all you need to do is copy and paste the function into your php page, and then call on the function inserting all the php variables that the function needs.

If you are attaching multiple attachments then the attachment array must be seperated by a |

function send_email ($to, $from, $subject, $message, $cc="", $bcc="", $att_path="", $att_mime="", $att_name="Tip sheet") {
$headers = "From: " . $from . "rn";
$headers .= "Reply-To: " . $from . "rn";
if ($cc <> "") { $headers .= "CC: " . $cc . "rn"; }
if ($bcc <> "") { $headers .= "BCC: " . $bcc . "rn"; }
if ($att_path <> "")
{
$att_path = explode("|",$att_path);
$att_mime = explode("|",$att_mime);
$att_name = explode("|",$att_name);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "MIME-Version: 1.0n" .
"Content-Type: multipart/mixed;n" .
" boundary="{$mime_boundary}"";
$email_message .= "This is a multi-partmessage in
MIME format.nn" .
"--{$mime_boundary}n" .
"Content-Type:text/html; charset="iso-8859-1"n" .
"Content-Transfer-Encoding: 7bitnn" .
nl2br($message) . "nn";
for ($i = 0; $i < count($att_path); $i++)
{
if ($att_path[$i] <> "")
{
if ($att_name[$i] == "")
{
$att_name[$i] = "Attachment " . $i;
}
$file = fopen($att_path[$i],"rb");
$data = fread($file,filesize($att_path[$i]));
fclose($file);
$data = chunk_split(base64_encode($data));
$ext = strtolower(substr(strrchr($att_path[$i], "."),1));
$attachment_filename = $att_path[$i] . "." . $ext;
$email_message .= "--{$mime_boundary}n";
$email_message .= "Content-Type: {$att_mime[$i]};n" .
" name="{$att_name[$i]}"n" .
"Content-Disposition: attachment;n" .
" filename="{$att_name[$i]}"n" .
"Content-Transfer-Encoding: base64nn" .
$data . "nn";
}
}
$email_message .= "--{$mime_boundary}--nn";
$email_message = $email_message.strip_tags($message);
$message = $email_message;
}
else
{
$headers .= "Content-Type: text/html; charset=ISO-8859-1 ";
$headers .= "MIME-Version: 1.0 ";
}
return mail($to, $subject, $message, $headers);
}

Tags: ,

Share this on Share this on Facebook Share this on Twitter  Share this on MySpace Share this on Google Share this on Yahoo Share this on Technorati Share this on Reddit Share this on Digg Share this on Delicious Share this on LinkedIn 

Newsletter sign up . . .

Apex Logo
Join-up below and we will send you some great ideas on how to be more effective when marketing your company!

Name *
Email *

Array
(
    [type] => 2
    [message] => mysql_close(): no MySQL-Link resource supplied
    [file] => /home/tpdeqhpm/public_html/footer.php
    [line] => 2
)