How to send email in PHP
Email ( Electronic mail ) is most popular communication service on internet . Generally it is common practice to use this service as marketing media for digital products. There are a lot of free email service available on internet like Gmail, Yahoo,AOL and more.
So as a developer we must know the mail functionality in PHP like send mail with php and read mail with PHP imap ,pop functions. in this tutorial we are going to learn how to send the mail with different MIM types and it mail components.
An email message consists of three components, the message envelope, the message header, and the message body. envelope controls the mail sending path like where it originated and where to be sent the mail. header part will denotes the contents of the email and sender and receiver information. body part contains the main user data information.In this series mail tutorial we learn with different PHP functions to send the mail.
On Linux server by default PHP comes with send mail or exim email server. But on windows PHP must be configured to allow from SMTP .
Main PHP extensions for email handling are
- Cyrus
- IMAP
- Mailparse
- vpopmail
PHP email function
mail ( $to , $subject , $message ,$headers) |
This function takes mainly four parameters, to,subject, message, headers.It will return a boolean that refers if mail is sent successfully or not.
$to parameter
$to parameter is a string to recipients of the email , we can give the ‘,’ comma separated email address. it can be send for more than one recipients at at time.
$to = '"To Display Name1" <reciever@domain1.dom>'; $to .= ',"To Display Name2" <reciever@domain2.dom>'; |
$subject parameter
$subject="This is my sample subject.";
$message parameter contain any length of string that is sperated with new line characters .
$message="Sample message line 1 n"; $message.="sample message line 2"; |
$header parameter
is the modifier or extending the mail sending option.It has many options ,but we will discuss importent ones here. see below snippet for example.
$headers = 'From: "Sender Display Name"' . PHP_EOL . 'Cc: "CC Display Name" ' . PHP_EOL . 'X-Mailer: PHP-' . phpversion() . PHP_EOL. 'MIME-Version: 1.0' . PHP_EOL. 'Content-type: text/plain; charset=UTF-8' . PHP_EOL. 'X-Priority: 1 (Higuest)'.PHP_EOL. 'X-MSMail-Priority: High'.PHP_EOL. 'Importance: High'.PHP_EOL. 'Reply-To: info@example.com'.PHP_EOL. 'Return-Path: info@example.com.PHP_EOL. 'Message-Id: <' . md5(uniqid(microtime())) . '@' . $hostname . '>'.PHP_EOL.
In the above code PHP_EOL is PHP constant that represent new line character .
Main list of email headers
 
From, email header notes from what email address is sent.
Cc , this email header inform the server that this mail also can be sent to this email address also.
X-Mailer, this email header inform the mail recipent client about the sender client.
MIME , this email header tells about the how the mail should be handle like plain text,html and attachment.
X-Priority, tells the priority of the email for generel server.
X-MSMail-Priority, tell the priority of the email for windows server hosted email servers.
Importance, tells the priority of the email this will work for some of the email server.
Reply-To, if user reply the sent email ,what the email address it to be taken, if it is not given default is From ,email header.
Return-Path, if an email is bounced, where to be notified , this can be same email address like From or other email also.this email header can be used to track some email campains .
Message-Id, is optional email header if we do not provide server will automatically generates and sends. this header can be used to track email campaings also.

