Tutorial Article How to send an attachment in an email using PHP- (PHP Send Email with Attachment)

Sending a valid email through coding guidelines has lately become a challenge. The spamming has taken roll all over the internet and out inbox full of unwanted emails.

Smart mail servers scans each and every bit of email information, its headers, content etc. and then deciding to keep it in inbox or move it to spam/junk folder.

In this article we will discuss with valid code/script on how to send an email with attachment with proper mail headers to avoid landing up a sent email in spam.

What you need?

We assume that you have web “form” with option for the user to upload a file and may be few other fields like name, address etc. and this form is submitted to a php file to accept form values.

We also assume that your web form File uploaded field attribute name is “photo”.

Now below is the code were I have placed relevant comments to make you understand what the script is actually doing.

Click here to view this in action


//Obtain file upload vars
$attached_file = $_FILES['photo']['tmp_name'];
$attached_file_type = $_FILES['photo']['type'];
$attached_file_name = $_FILES['photo']['name'];

//First we need to check that the is there any File Uploaded or NOT
$subject = "My email with attachment";
$strMessage = "<html><body><b>Here comes the email message.</b> </ br></ br> This can be a valid html email i.e. can contain html elements too.</body></html>";

$semi_rand = md5(time());

//Mime boundary makes the mail server understand exactly that from where the email message starts-end and email attachment starts-end

$mime_boundary = "Multipart_Boundary_x".$semi_rand."x";

//The following headers are required

$from = "emailFrom@example.com";
$bcc_email = "emailBcc@example.com";
$to = "emailTo@example.com";

$headers = "From: $from\n" .
"Bcc: $bcc_email\n" .
"Reply-To: $from\n" .
"X-Priority: 3\n" .
"X-MSMail-Priority: Normal\n" .
"X-Mailer: PHP/" . phpversion() ."\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;boundary=\"{$mime_boundary}\"";

$msg = "\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$strMessage . "\n\n";

// Read the attached file to be attached ('rb' = read binary)
$file = fopen($attached_file,'rb');
$data = fread($file,filesize($attached_file));

$data = chunk_split(base64_encode($data));

$msg .= "--{$mime_boundary}\n" .
"Content-Type: \"{$attached_file_type}\";\n" .
" name=\"{$attached_file_name}\"\n" .
"Content-Disposition: attachment;\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .

//Finally send an email and you are done.. Hurray!!!

Both comments and pings are currently closed.

4 Responses to “Tutorial Article How to send an attachment in an email using PHP- (PHP Send Email with Attachment)”

  1. Spent my time to read all the comments, but I really loved the post UncleCode.Com – Technology Blog | PHP – Script / Code on how to send an attachment in an email using PHP, PHP Send Email with Attachment, Sending attachment with an email using PHP . It proved to be very helpful to me and I am positive to all the commenters right here! I will get your feed, please keep on posting!

  2. Thank you for share very good info.

  3. Garry says:

    Hi Donald,

    (Your question is a bit out of context with the article, but, I am happy to answer it)

    We use the combination of three things to keep spammers away form sending spam emails-

    1. Use Javascript to validate the form. As this is client side scripting language, its every fast to let users know that need to fill in correct values.

    2. Use PHP to validate the form values again to make sure user has filled in correct values.

    3. Imp. use Captcha to keep spammers/auto spammers away from the form. Below are the following Url’s to get you started-




  4. Donald says:

    Do you use php or javascript on contact us web pages to keep website scrapers from sending spam email?