Php send email with attachment

18 Apr

The PHP mail() Function

Dynamic websites sends various type of emails to their users on difference different type on events. Php is a very popular high level language which is used create dynamic web applications.
Generally emails are sent on new user registration, When any user change his password or when any content are updated on the website.
Php has a built in mail() function to send emails to users. We can send any type of text or html emails with attachment to users.

The basic syntax of the mail() function.
mail(to, subject, message, headers, parameters)

Below code shows how we can send an simple plain text email.
$to = '';
$subject = 'New notificatin received';
$message = 'Hi Admin, new notication has been recieved on website';
$from = '';

// Sending email
if(mail($to, $subject, $message)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';

Sending HTML Formatted Emails
Many times we need to send html emails. Html email looks attractive and gain more user attention.
We can sent HTML email via php mail function very easily. We need to pass some special headers in email to send HTML emails.
Below is the code of html email

$to = '';
$subject = 'Welcome to our website';
$from = '';

// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();

// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Jane!</h1>';
$message .= '<p style="color:#080;font-size:18px;">Welcome to our website.</p>';
$message .= '</body></html>';

// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';

Php send mail with attachment

We can send any type of attachment through php mail function. The attachment file has to be uploaded on web server. Sending HTML mail or Sending attachment with the mail has the same process. We need to set some special headers to send attachment with the mail.Now we will create a function mail_attachment() that can be used to send attachment with the email

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";

Now we will see how we are using the mail_attachment() function to send mail.

$my_file = "";
$my_path = "/home/shared/";
$my_name = "John doe";
$my_mail = "";
$my_replyto = "";
$my_subject = "Please find attachement";
$my_message = "Hello James, Please attached document.";

mail_attachment($my_file, $my_path, "", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);

Leave a Reply

Your email address will not be published. Required fields are marked *