Handling email with 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.
Internet mail protocol
What is an email protocol?
Email protocol is a set of rules defined to ensure that emails can be exchanged between
various servers and email clients in a standard manner. This ensures that the email is
universal and works for all users.
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.
Why do we need email protocols?
In the case of email, the sender, recipients, and servers involved can all be different but
then they need to receive the data, decipher the content and render it in the same way the
sender has sent it. Email protocols define how the email message has to be encoded, how
it needs to be sent, received, rendered, and so on, and hence they are essential. While
email protocols make the process behind emails a bit complex, the protocols ensure that
email is a standard, reliable, and universal mode of communication.
SMTP Protocol
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
1 Generally used to Designed to
support single handle multiple
client. clients.
2 Messages are Messages are
accessed offline. accessed online
although it also
supports offline
mode.
3 POP does not It offers ability
allow search to search emails.
facility.
4 All the messages It allows
have to be selective
downloaded. transfer of
messages to the
client.
5 Only one mailbox Multiple
can be created on mailboxes can
the server. be created on
the server.
6 Not suitable for Suitable for
accessing non- accessing non-
mail data.
mail data i.e.
attachment.
7 POP commands IMAP
are generally commands are
abbreviated into not abbreviated,
codes of three or they are full. Eg.
four letters. Eg. STATUS.
STAT.
8 It requires Clients are
minimum use of totally
server resources. dependent on
server.
9 Mails once Allows mails to
downloaded be accessed
cannot be from multiple
accessed from locations.
some other
location.
10 The e-mails are Users can view
not downloaded the headings
automatically. and sender of e-
mails and then
decide to
download.
10 POP requires less IMAP requires
internet usage more internet
time. usage time.
The main sections of an email
Emails consist of two main sections: the header and the body.
The email header section handles the instructions for the email, including to whom it
should be sent and what kind of content is being sent.
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 :
1. To: It specifies the DNS address of the primary recipient(s).
2. Cc : It refers to carbon copy. It specifies address of secondary
recipient(s).
3. BCC: It refers to blind carbon copy. It is very similar to Cc. The
only difference between Cc and Bcc is that it allow user to send
copy to the third party without primary and secondary
recipient knowing about this.
4. From : It specifies name of person who wrote message.
5. Sender : It specifies e-mail address of person who has sent
message.
6. Received : It refers to identity of sender’s, data and also time
message was received. It also contains the information which
is used to find bugs in routing system.
7. Return-Path: It is added by the message transfer agent. This
part is used to specify how to get back to the sender.
3. Body:- The body of a message contains text that is the actual
content/message that needs to be sent, such as “Employees who are
eligible for the new health care program should contact their
supervisors by next Friday if they want to switch.” The message body
also may include signatures or automatically generated text that is
inserted by the sender’s email system.
The above-discussed field is represented in tabular form as follows :
Sending email and validation of Email_id with php
PHP mail() Function
Definition and Usage
The mail() function allows you to send emails directly from a script.
Syntax
mail(to,subject,message,headers,parameters);
Parameter Values
Parameter Description
to Required. Specifies the
receiver / receivers of
the email
subject Required. Specifies the
subject of the
email. Note: This
parameter cannot
contain any newline
characters
message Required. Defines the
message to be sent.
Each line should be
separated with a LF (\n).
Lines should not exceed
70 characters.
Windows note: If a full
stop is found on the
beginning of a line in the
message, it might be
removed. To solve this
problem, replace the full
stop with a double dot:
<?php
$txt = str_replace("\n.",
"\n..", $txt);
?>
headers Optional. Specifies
additional headers, like
From, Cc, and Bcc. The
additional headers
should be separated with
a CRLF (\r\n).
Note: When sending an
email, it must contain a
From header. This can
be set with this
parameter or in the
php.ini file.
parameters Optional. Specifies an
additional parameter to
the sendmail program
(the one defined in the
sendmail_path
configuration setting).
(i.e. this can be used to
set the envelope sender
address when using
sendmail with the -f
sendmail option)
Example
Send a simple email:
<?php
// the message
$msg = "First line of text\nSecond line of text";
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
// send email
mail("someone@example.com","My subject",$msg);
?>
More Examples
Send an email with extra headers:
<?php
$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";
mail($to,$subject,$txt,$headers);
?>
PHP - Validate E-mail
The easiest and safest way to check whether an email address is well-formed is
to use PHP's filter_var() function.
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";
}
PHP – Verify E-mail
checkdnsrr() function will take an email address and
performs a DNS lookup on the specified host (either a
hostname or IP address). You can use this function before
sending emails. The function will return an error
informing the user that the domain related to email
address probably doesn’t exist.
Example
<?php
// The email to validate
$email = “abc@xyz.com”;
// An optional sender
function domain_exists($email, $record = 'MX'){
list($user, $domain) = explode('@', $email);
return checkdnsrr($domain, $record);
}
if(domain_exists($email)) {
echo('This MX records exists; I will accept this email as valid.');
}
else {
echo('No MX record exists; Invalid email.');
}
}
?>