Sending emails from the script is a very useful functionality in the web application. Most of the websites used the email sending feature to send notifications to the user. If your web application developed with PHP or uses PHP, it’s very easy to send email from the script using PHP.
PHP provides an easy way to send emails from the website. You can send text or HTML email with mail() function in PHP. But sometimes email functionality needs to be extended for sending an attachment with the mail. In this tutorial, we will show you how to send email with attachment in PHP.
In the example script, we will make it simple to send text or HTML email including any types of files as an attachment (like image, .doc, .docx, .pdf, .txt, etc.) using PHP.
The PHP mail() function with some MIME type headers can be used to send email with attachment in PHP. In the following example code, MIME and Content-Type headers are used with mail() function to send email with attachment using PHP.
$to
– Recipient email address.$from
– Sender email address.$fromName
– Sender name.$subject
– Subject of the email.$file
– Relative path of the file that you want to attach with the email.$htmlContent
– Body content of the email (Text or HTML).The following script lets you send both types of messages (text or HTML) with an attachment file to the email.
<?php
// Recipient
$to = 'recipient@example.com';
// Sender
$from = 'sender@example.com';
$fromName = 'CodexWorld';
// Email subject
$subject = 'PHP Email with Attachment by CodexWorld';
// Attachment file
$file = "files/codexworld.pdf";
// Email body content
$htmlContent = '
<h3>PHP Email with Attachment by CodexWorld</h3>
<p>This email is sent from the PHP script with attachment.</p>
';
// Header for sender info
$headers = "From: $fromName"." <".$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" . $htmlContent . "\n\n";
// Preparing attachment
if(!empty($file) > 0){
if(is_file($file)){
$message .= "--{$mime_boundary}\n";
$fp = @fopen($file,"rb");
$data = @fread($fp,filesize($file));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($file)."\"\n" .
"Content-Description: ".basename($file)."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($file)."\"; size=".filesize($file).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $from;
// Send email
$mail = @mail($to, $subject, $message, $headers, $returnpath);
// Email sending status
echo $mail?"<h1>Email Sent Successfully!</h1>":"<h1>Email sending failed.</h1>";
?>
The example script allows you to send a single attachment to the email, to send an email with multiple attachments follow this tutorial – Send Email with Multiple Attachments in PHP
Sending Email to Multiple Recipients:
You can send email to multiple recipients at once with Cc and Bcc headers. Use the Cc and Bcc headers for sending email with attachment to multiple recipients in PHP.
$headers .= "\nCc: mail@example.com";
$headers .= "\nBcc: mail@example.com";
Send Email via SMTP Server in PHP
Here we have provided the easiest way to send email with attachment in PHP. You don’t need to include any library to send HTML email with attachments. Using the PHP default mail() function you can easily send email with pdf/image attachment.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
Thank you very much for great help
Great!!! Thanks a lot!!
This code worked perfect!
The code works. I get an email but my attached file (pdf or jpg) appears in hex format in email body. I’m on web host Hostinger. I think they run Linux servers. Could this have something to do with it and do I need to adjust my code. See below what I’m getting. Please help.
–==Multipart_Boundary_xe4a1d18bb25f8507066079069d78e651x
Content-Type: application/octet-stream; name=”test.pdf”
Content-Description: test.pdf
Content-Disposition: attachment;
filename=”test.pdf”; size=121846;
Content-Transfer-Encoding: base64
JVBERi0xLjQKMSAwIG9iago8PAovVHlwZSAgL1hPYmplY3QgCi9TdWJ0eXBlICAvSW1hZ2UgCi9X
aWR0aCAgNDI4IAovSGVpZ2h0ICAxNDQgCi9GaWx0ZXIgIC9GbGF0ZURlY29kZSAKL0JpdHNQZXJD
b21wb25lbnQgIDggCi9EZWNvZGVQYXJtcyA8PAovUHJlZGljdG9yICAxNSAKL0NvbG9ycyAgMyAK
…
Hello thank for the solution you save me but how can i insert php variable in htmlContent
You save my day, thanks bro !
All others solutions don’t work, even on php mail documentation.
how to add multiple Bcc address
Thanks Brohter, It Worked… None of stackoverlow.com solutions worked for me but your solution worked like a charm. Thanks again.
Nice code….but mail goes to spam. try to @gmail
Any suggestion?
Thx
friend, my file is an image, I’m sending it through a form
// Attached file
$ file = “files / codexworld.pdf”; ….. how would I receive my image here
How can I adapt to send multiple files ?, all the ones in a directory.
Thanks!!
Nice!
Nice nice! Thank you!
Thankyou so much ..helpful article..keep it up….
How to attach multiple file?
To send an email with multiple attachments, see this tutorial – https://www.codexworld.com/send-email-with-multiple-attachments-php/
I worked (unlike some other examples I had found). Thank you!
Hi, it’s working great for me!
But I have a small problem. I’ve been trying to add a Bcc in the header, and can’t seem to do it… I have no idea why. I’ve tried multiple variations of :
$headers .= “Bcc: Beny Scott”.” \n”;
Nothing works. Not with the email being hardcoded in, not with \r\n at the end, no idea why.
Use the Bcc header to add BCC mail address with this email. Add the following line of code after the first
$headers
.Thanks Bro Its Works for me. You are the greatest Developer in the world
I have uploaded the sending mail script on cpanel.
Mail sent.
But very slow.
Your demo is very fast?
What’s the matter?