Sending Email in PHP

How to send email using PHP. The following section describes that with coding…..

Before using Mail in PHP, note that, PHP mail is intended for only a small scale use and not for large scale Email systems. So for large scale, you may use other mal softwares.

Email can be sent

  • as ordinary plain message
  • as an HTML Message
  • with attachments

We will see, one by one

Mail in PHP

there is a function mail() which helps in sending mails through PHP Scripts.

the syntax is mail(To address, Subject, Message, headers)

To address – to which address you want to send mail

Subject of the mail -

Message – body of the mail and

headers – are the http headers sent along with the mail to inform the email clients

Example Headers are

  1. “From:abc@xyz.com ” //to inform the from address
  2. “Reply-To:xyz@xyz.com ” //Reply to a different email id
  3. “Bcc:a@a.com ” //Blind Carbon Copy
  4. “Cc:b@b.com” //Carbon Copy
  5. Return-Path:abc@a.com”
  6. X-Mailer: PHP5 //inform the email client
  7. MIME-Version:1.0
  8. Content-Type: text/plain” //text/html or multipart/mixed, multipart/alternative (content type is useful for attaching files with MIME headers)
  9. Content-Disposition: attachment;filename=”image.jpg”
  10. Content-Transfer-Encoding: base64″ //Encoding Standards

Email as ordinary Plain message

<?php

$to=”abc@xyz.com”;

$subject=”This is a Simple mail from PHP”;

$message=”<h1>this is a text message</h1>”;

$m=mail($to,$subject,$message);

if($m)

echo “Mail sent successfully”;

else

echo “Mail Failed”;

?>

The mail() function returns a boolean value, so if the mail is sent successfully, it returns 1 else 0. Since the above script does not uses any headers, the mail will be sent as plain text mail and the H1 tag wont be validated for a HTML.

The following code send mail as a HTML page

Email as HTML message

<?php

$to=”abc@xyz.com”;

$subject=”This is a Simple mail from PHP”;

$message=”<h1>this is a text message</h1>”;

$headers = “Content-Type: text/html \r\n”;

$m=mail($to,$subject,$message,$headers);

if($m)

echo “Mail sent successfully”;

else

echo “Mail Failed”;

?>

The above script uses a MIME type as text/html, to the message with H1 rendered as an HTML Tag and the message will display with size of header 1

Email with attachments

Lot of headers to be used for Emails with attachments.

<?php

//set the to, subject and message
$to=”tspembedded@gmail.com”;
$subject=”Here’s the mail with attachment”;
$message=”I am attaching a PDF file for your information”;

//Open the file to attach

$filename=”rts.pdf”; //name of the file and its path

$f=fopen($filename,”rb”); //open the file under binary mode
$data=fread($f,filesize($filename));  //read the file and its size
fclose($f); //close the file pointer
//header setting
$data=chunk_split(base64_encode($data)); //split the file and encode each chunks to base 64
$headers=”Content-Type: application/pdf \r\n”; //since it is a pdf file, mention the mime type
$headers.=”Content-Disposition: attachment;filename=rts.pdf \r\n”; //header to attach the file with filename
$headers.=”Content-Transfer-Encoding: base64 \r\n”.$data.”\r\n”; //header with encode setting

$m=mail($to,$subject,$message,$headers);
if($m)
echo “mail to $to is a success”;
else
echo “Mail to $to Failed”;
?>

the Mime type used in the above script is application/pdf, so only the attachment will be shown in the email client, but the message wont be showed at all…

to make mixed mode, use the mime type as multipart/mixed.

For any other queries and suggestions, Please Comment!!!!!!

2 comments:

  1. Great work .. but would be excellent if you could add syntax highlighter plugin to show the PHP code in highlighted way :)

    ReplyDelete
  2. Nice tutorial. I was looking for this ...

    ReplyDelete