Handling Email in PHP
Handling Email in PHP
It was 1971 when Ray Tomlinson invented and developed electronic mail, as we know it
today, by creating ARPANET’s networked email system.
By 1993 the word “electronic mail” had been replaced by “email” in the public lexicon
and internet use had become more widespread.
Example:
A sender using an Apple email client with a Gmail server can send an email to another user
using a Zoho mail server on an Outlook email client. This is possible because the servers and
the email clients follow the rules and standards defined by the email protocols.
SMTP stands for Simple Mail Transfer Protocol. SMTP is the principal email protocol that is
responsible for the transfer of emails between email clients and email servers.
SMTP stands for Simple Mail Transfer Protocol. It was first proposed in 1982. It is a
standard protocol used for sending e-mail efficiently and reliably over the internet.
Key Points:
• SMTP is application level protocol.
• SMTP is connection oriented protocol.
• SMTP is text based protocol.
• It handles exchange of messages between e-mail servers over TCP/IP
network.
• Apart from transferring e-mail, SMPT also provides notification regarding
incoming mail.
• When you send e-mail, your e-mail client sends it to your e-mail server
which further contacts the recipient mail server using SMTP client.
• These SMTP commands specify the sender’s and receiver’s e-mail address,
along with the message to be send.
• The exchange of commands between servers is carried out without
intervention of any user.
• In case, message cannot be delivered, an error report is sent to the sender
which makes SMTP a reliable protocol.
How Does SMTP Work?
SMTP is an asymmetrical protocol, meaning that there are many clients interacting with one
server, using a basic model popular in the 1980s which is now mostly defunct outside of email
protocols. SMTP runs on TCP/IP and listens on port 25.
Email is submitted by a mail client (mail user agent, MUA) to a mail server (mail
submission agent, MSA) using SMTP on TCP port 587. Most mailbox providers still allow
submission on traditional port 25. The MSA delivers the mail to its mail transfer agent
(mail transfer agent, MTA). Often, these two agents are instances of the same software
launched with different options on the same machine.
The boundary MTA uses DNS to look up the MX (mail exchanger) record for the
recipient's domain (the part of the email address on the right of @ ). The MX record
contains the name of the target MTA. Based on the target host and other factors, the
sending MTA selects a recipient server and connects to it to complete the mail
exchange.
POP Protocol
POP stands for Post Office Protocol. Email clients use the POP protocol support in the
server to download the emails. This is primarily a one-way protocol and does not sync back
the emails to the server.
It is generally used to support a single client. There are several versions of POP but the
POP 3 is the current standard.
Key Points
• POP is an application layer internet standard protocol.
• Since POP supports offline access to the messages, thus requires less
internet usage time.
• POP does not allow search facility.
• In order to access the messaged, it is necessary to download them.
• It allows only one mailbox to be created on server.
• It is not suitable for accessing non mail data.
• POP commands are generally abbreviated into codes of three or four
letters. Eg. STAT.
IMAP Protocol
IMAP stands for Internet Message Access Protocol. IMAP Protocol is used to sync the
emails in the server with the email clients. It allows two-way sync of emails between the
server and the email client, while the emails are stored on the server.
It was first proposed in 1986. There exist five versions of IMAP as follows:
1. Original IMAP
2. IMAP2
3. IMAP3
4. IMAP2bis
5. IMAP4
Key Points:
• IMAP allows the client program to manipulate the e-mail message on the
server without downloading them on the local computer.
• The e-mail is hold and maintained by the remote server.
• It enables us to take any action such as downloading, delete the mail
without reading the mail.It enables us to create, manipulate and delete
remote message folders called mail boxes.
• IMAP enables the users to search the e-mails.
• It allows concurrent access to multiple mailboxes on multiple mail servers.
Comparison between POP and IMAP
S.N. POP IMAP
The body section contains the code that dictates email’s structure, as well as contains
any content on display for the user—including any text and embedded links. In an
HTML email, this would include HTML code.
Format of E-mail :
An e-mail consists of three parts that are as follows :
1. Envelope
2. Header
3. Body
These are explained as following below.
1. Envelope :
The envelope part encapsulates the message. It contains all
information that is required for sending any e-mail such as
destination address, priority and security level. The envelope is
used by MTAs for routing message.
2. Header :
The header consists of a series of lines. Each header field consists of a
single line of ASCII text specifying field name, colon and value. The
main header fields related to message transport are :
The mail() function allows you to send emails directly from a script.
Syntax
mail(to,subject,message,headers,parameters);
Parameter Values
Parameter Description
Example
Send a simple email:
<?php
// the message
$msg = "First line of text\nSecond line of text";
// send email
mail("[email protected]","My subject",$msg);
?>
More Examples
Send an email with extra headers:
<?php
$to = "[email protected]";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: [email protected]" . "\r\n" .
"CC: [email protected]";
mail($to,$subject,$txt,$headers);
?>
In the code below, if the e-mail address is not well-formed, then store an error
message:
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}