Logo for Hasan Azizul Haque – Website and Mobile Apps developer

PHP simple text, html and attachment email

No Comments// Posted in Email, How to, PHP by on 12.30.11.

Sending simple email using PHP is piece of cake. Just use

<?php mail(to,subject,message,headers,parameters); ?>

where to is receiver email, subject is email subject, message is the email body, headers is optional it specifies mail type, from, cc, bcc etc. parameters is optional and can be used to pass additional flags as command line options to the program configured to be used when sending mail, as defined by the sendmail_path configuration setting.

Now I will show you how to send email in Text format, HTML format and email with attachment

Simple Text mail

 $to = 'receiver@somewhere.com';
 $subject = 'Simple Text email';
 $message = "Hello World!\n\nThis is my first Text mail.";
 $mail_sent = @mail( $to, $subject, $message, $headers );
// @ sign is the php error control operator. any error messages that might be generated by this expression will be ignored.
 echo $mail_sent ? "Mail sent" : "Mail failed";

HTML Email

 $to = 'receiver@somewhere.com';
 $subject = 'HTML email';
 $message = <<<EOF
  <body bgcolor="#EFEFEF"> 
        <b>Hello world</b> <br> 
        <font color="red">This is my first HTML Email</font> <br> 
        <a href="http://www.webnapps.co.uk/">Thanks. Hasan</a> 
// <<< sign is the php heredoc sign, almost same as double quote

         ."Content-type: text/html\r\n"; 

 //options to send to cc+bcc 
 //$headers .= "Cc: [email]name1@domain.com[/email]"; 
 //$headers .= "Bcc: [email]name2@domain.com[/email]";
 $mail_sent = @mail( $to, $subject, $message, $headers );
 echo $mail_sent ? "Mail sent" : "Mail failed";

mail with attachment

to send an email attachment using PHP mailer function is not very complex.  You just need to define mile boundary with a random number. You can send one or more files with the attachment.

 //array of file names
 $filenames = array('file1.zip','file2.jpg','zip3.txt',.........);
 //email set up
 $to = "receiver@somewhere.com";
 $subject = "email with attachment";
 $messageTxt = "This is my first email with attachments";
 $headers = "From: webmaster@domain.com\r\n";
 //setting the boundary
 $rand_seed = md5(time());
 $mime_boundary = "==Multipart_Boundary_x{$rand_seed}x";
 //attachment header
 $headers .= "MIME-Version: 1.0\r\n"
   ."Content-Type: multipart/mixed;\r\n"
   ." boundary=\"{$mime_boundary}\"\r\n";
 $message .= "This is a multi-part message in MIME format.\n\n"
   ."Content-Type:text/plain; charset=\"iso-8859-1\"\n\n"
   ."Content-Transfer-Encoding: 7bit\n\n" . $messageTxt . "\n\n";
 $message .= "--{$mime_boundary}\n";
 for($x=0;$x $file = fopen($files[$x],"rb");
   $data = fread($file,filesize($files[$x]));
   $data = chunk_split(base64_encode($data));
   $message .= "Content-Type: {\"application/octet-stream\"};\n"
    ." name=\"$files[$x]\"\n"
    ."Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n"
    ."Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
   $message .= "--{$mime_boundary}\n";
 $mail_sent = @mail( $to, $subject, $message, $headers );
 echo $mail_sent ? "Mail sent" : "Mail failed";


Cheerio !

You can leave a response, or trackback from your own site.

Leave a Reply

  • Categories

  • Recent Posts

  • Recent Comments

  • Archives

  • Sidebar Slideshow