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);
}

Array
(
    [type] => 1
    [message] => Uncaught Error: Call to undefined function split() in /home/tpdeqhpm/public_html/plugins/page/blog/blog.class.php:159
Stack trace:
#0 /home/tpdeqhpm/public_html/plugins/page/blog/templates/default.blog.php(27): blog_functions->get_tags('Email, Websites')
#1 /home/tpdeqhpm/public_html/plugins/page/blog/blog.php(17): include('/home/tpdeqhpm/...')
#2 /home/tpdeqhpm/public_html/plugins/templates/apexbusinesssupport/apexbusinesssupport.page.php(65): include('/home/tpdeqhpm/...')
#3 /home/tpdeqhpm/public_html/index.php(146): include('/home/tpdeqhpm/...')
#4 {main}
  thrown
    [file] => /home/tpdeqhpm/public_html/plugins/page/blog/blog.class.php
    [line] => 159
)