By default, the PHP mail() does not support sending email with attachment. In order to send an attachment, u can either use the PEAR package or PHPMailer. But in reality, the hosting server may not provide these kinds of third party libraries.
A workaround for sending attachment using PHP mail() is to construct a MIME header which contains the attachment information. The following piece of codes should help.
<?php
/* Email Detials */
$mail_to = "<receipient address>";
$from_mail = "<sender address>";
$from_name = "<sender name>";
$reply_to = "<reply-to address>";
$subject = "<email subject>";
$message = "<email content>";
/* Attachment File */
// Attachment location
$file_name = "<attachment file name>";
$path = "<relative path/absolute path which contains the attachment>";
// Read the file content
$file = $path.$file_name;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
/* Set the email header */
// Generate a boundary
$boundary = md5(uniqid(time()));
// Email header
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$reply_to."\r\n";
$header .= "MIME-Version: 1.0\r\n";
// Multipart wraps the Email Content and Attachment
$header .= "Content-Type: multipart/mixed; boundary=\"".$boundary."\"\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$boundary."\r\n";
// Email content
// Content-type can be text/plain or text/html
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= "$message\r\n";
$header .= "--".$boundary."\r\n";
// Attachment
// Edit content type for different file extensions
$header .= "Content-Type: application/xml; name=\"".$file_name."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
$header .= $content."\r\n";
$header .= "--".$boundary."--";
// Send email
if (mail($mail_to, $subject, "", $header)) {
echo "Sent";
} else {
echo "Error";
}
?>
It should be able to deliver emails to Gmail and Hotmail without regarding as SPAM.
Done =)
Pingback: Configure Postfix/Sendmail for PHP mail() in Ubuntu | Eureka!
thanks , this code works. good job
you are welcome =)
Why the in my attachment it comes .Dat the extension name.
can’t get what u mean, can you elaborate more?
Thunks, good code. but if the mime is on text/html?
you want to change the mime header? i didn’t quite get what u mean?
this is working
Thank you !!
You are welcome =D
Its works fine. Thanks.
great =)
Fantastic Article… I really searched a lot and also tried a lot of codes but they just were not working. I needed to send an image as an attachment with an email to the member of the website. Real good effort there!!!
Thanks for your comment~ glad to know that my post could help~ =D
my wrapping makes a problem with this code it shows only the empty attachments file and no content in the body of the mail
can u post ur code here?
BAD look in ms outlook
maybe you could try PHPMailer.
its not working with me, don’t know why?
showing error.
what to do?
but when I use sendmail on terminal, it dispatches email and takes to spam
also in php.ini the path to sendmail is already given correctly on my linux machine
mail with attachment will be a good thing, but for now I want the simple mails to work in php without attachments and then I am sure mails without attachments will also go out.
can you help me please
Hi Molik,
First of all, what error did you get? can you post it here?
Second, if you find that the outgoing emails were treated as SPAM. most likely the domain is not in the email whitelist. Actually setup an email server is quite complicated and i dun have much experience on that. If you dun mind using gmail as a relay, i would suggest you read the following post.
Postfix – Relay via Gmail
I dun know if your server is using Sendmail/Postfix. In my experience, Postfix is more user friendly.
Hope the above information could help.
Kit
Best script i’ve seen so far, smooth and easy
Great job, man
I couldn’t have it working until I changed all those “$header” to “$message”. Here it is my working code:
Thanks for your code. =D
Will this work on a windows server running php?
I am trying to use the above code and although I get sent displayed on the screen no email is received and I am not sure what to try?
Is you server capable for sending email?
Maybe u are missing a mail transfer agent. here is a example about how to setup Postfix in Ubuntu.
Postfix – Relay via Gmail