Sending emails from the script is a very useful functionality for the dynamic web application. The email sending functionality can be easily integrated with PHP. PHP mail() function provides a simple way to send emails from the script. You can send email with text or HTML content using the PHP mail() function.
Along with the HTML content, you can send the attachment with the email using PHP. Use some additional headers in the PHP mail() function to send emails with pdf/image/word attachments. This tutorial will show you how to send emails with single or multiple attachments in PHP.
Our example script helps web developers to send text or HTML emails including any type of file (like image, .doc, .docx, .pdf, .txt, etc.) as an attachment in PHP. To make the whole process simple, we will group the entire code into a function. You can send emails with multiple attachments by calling a single function.
We’ll create a custom PHP function and all the code will be grouped in this function for better usability. The multi_attach_mail() helps to send email with multiple attachments using PHP.
The multi_attach_mail()
function requires the following parameters.
$to
– Required. Specify the recipient’s email address.$subject
– Required. Specify email subject.$message
– Required. Specify email body content (text or HTML).$senderEmail
– Required. Specify the sender’s email address.$senderName
– Required. Specify sender name.$files
– Optional. An array of files path to attach with the email.This function returns TRUE on success or FALSE on error.
/*
* Custom PHP function to send an email with multiple attachments
* $to Recipient email address
* $subject Subject of the email
* $message Mail body content
* $senderEmail Sender email address
* $senderName Sender name
* $files Files to attach with the email
*/
function multi_attach_mail($to, $subject, $message, $senderEmail, $senderName, $files = array()){
// Sender info
$from = $senderName." <".$senderEmail.">";
$headers = "From: $from";
// Boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// Multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// Preparing attachment
if(!empty($files)){
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$file_name = basename($files[$i]);
$file_size = filesize($files[$i]);
$message .= "--{$mime_boundary}\n";
$fp = @fopen($files[$i], "rb");
$data = @fread($fp, $file_size);
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".$file_name."\"\n" .
"Content-Description: ".$file_name."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".$file_name."\"; size=".$file_size.";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $senderEmail;
// Send email
$mail = mail($to, $subject, $message, $headers, $returnpath);
// Return true if email sent, otherwise return false
if($mail){
return true;
}else{
return false;
}
}
The following example shows how you can use our custom PHP function to send email with multiple attachments. Call the multi_attach_mail()
function and pass the required parameters.
$to
– Receiver email address.$from
– Sender email address.$fromName
– Sender name.$subject
– Subject of the email.$files
– Attachment files path in array format.$htmlContent
– Email body content.// Email configuration
$to = 'recipient@example.com';
$from = 'sender@example.com';
$fromName = 'Sender Name';
$subject = 'Send Email with Multiple Attachments in PHP by CodexWorld';
// Attachment files
$files = array(
'files/codex-web-view.png',
'files/codex-site.pdf'
);
$htmlContent = '
<h3>Email with Multiple Attachments by CodexWorld</h3>
<h4>This HTML email is sent from the script with multiple attachments using PHP.</h4>
<p><b>Total Attachments:</b> '.count($files).'</p>';
// Call function and pass the required arguments
$sendEmail = multi_attach_mail($to, $subject, $htmlContent, $from, $fromName, $files);
// Email sending status
if($sendEmail){
echo 'The email is sent successfully.';
}else{
echo 'Email sending failed!';
}
Send Email with Attachment on Form Submission using PHP
The email with attachment functionality is very useful when you want to allow the user to submit the form with files and send the form data to an email. Our example script allows you to send any type of file as an attachment with the email from the script. You can also enhance or customize the functionality of multiple email attachments script as per your needs. Download the source code to get the email form script with attachments sending functionality.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
Hi Codeworld,
I am using the index.php and submit.php in the demo folder as I need the CSS for the styling of my form. But when I receive the email, the attachment I attached was not send. Please help me amend the code so the attachment can be attached when I received the email. Thanks
WOrk like a charm, thank you!
How can I adapt to send all of those in a directory?
Thank you!!
Simple and effective, thanks
In your PHP project “HTML EMAIL WITH MULTIPLE ATTACHMENTS”, do you have an HTML example that will call this app? This app also has individual file attachment and I would like to be able to submit all attachments as a group using (Ctrl+a).
Could you please say how to avoid the message goes to spam?
With my new php version update, my mail services got broken. After spending almost two days of time, I found your article. Phew!, finally got squared away. Thanks so much, very useful!.
CODEXWORLD IS MY FAVOURITE SITE.
how to attech the html form fields in thi s above code plz give the html to connect above code
i want to attach file using html form send mail through php. but i am not getting please help me.
how get the file from html to php
Check out the PHP file upload tutorial from here – How to Upload File in PHP.
thanks man working perfect:)
Thanks a lot
very good !!
Thanks a lot.. Really nice post.. very simple and easy way to send attachment and its working like charm.
Actually i want to mail pdf file automatically when some register a form…….please help me how do i do that……..?
Hat off,codexworld,I m looking for phpmysql complete code for news update self manageble,please help me
how can i send files from an html page to php page.
Thanks for the article.
I tried to use the code but I don’t received the file that I added by an HTML form using .
How can I fix the problem?
I set the 2 variables and inserted in the $files = array($attach1,$attach2);
$attach1 = $_FILES[‘attach1’][‘name’];
$attach2 = $_FILES[‘attach2’][‘name’];
THANKS!
@Marco
At first upload the files which you want to attach with the email. After that, set the variables with the uploaded file path like the below.
Hello codeworld
I want need php multi file attachment function . Please help me and provide the complete Code/ file with form.
Regard
Manoj