0% found this document useful (0 votes)
12 views

Chapter5 Email Handling Notes

Uploaded by

swarali.bhosale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Chapter5 Email Handling Notes

Uploaded by

swarali.bhosale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

HANDLING EMAIL WITH PHP

Email handling with PHP


By Asst.Prof.Charushila Patil
Pratibha College of Commerce and Computer
Studies,Chinchwad,Pune 1
5. HANDLING EMAIL WITH PHP
5.1 Working of Mail system
5.2 Internet mail protocols

Email handling with PHP


5.3 Structure of an email message
5.4 Sending email with php
5.5 Sending HTML email
5.6 Email id validation and verification

2
COMPONENTS OF MAIL SYSTEM
 Mail User Agent(MUA):The MUA is the application the originating
sender uses to compose and read email, such as Eudora, Outlook, etc.

Email handling with PHP


 Mail Delivery Agent(MDA):A mail delivery agent or message
delivery agent(MDA) is a computer software component that is
responsible for the delivery of email messages to a local recipient’s
mailbox.

 Mail Transfer Agent(MTA): A message transfer agent or mail


transfer agent(MTA) or mail relay is software that tranfers electronic
mail messages from one computer to another using client server
architecture.

 Domain Name System(DNS): A DNS translates the domain name to


IP addresses. 3
WORKING OF MAIL SYSTEM

Email handling with PHP


4
2.2 INTERNET MAIL PROTOCOLS
 E-mail Protocols are set of rules that help the
client to properly transmit the information to or
from the mail server.

Email handling with PHP


Here, we will discuss various protocols such as
 SMTP

 POP3

 IMAP4

5
1.SMTP
 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.

Email handling with PHP


 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, SMTP also provides
notification regarding incoming mail.
 In case, message cannot be delivered, an error report is
sent to the sender which makes SMTP a reliable
protocol. 6
2. POP3

 POP stands for Post Office Protocol. It was first


proposed in 1984. It is generally used to support a
single client. Default port no is 110

Email handling with PHP


 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 message, it is necessary to
download them.
 It allows only one mailbox to be created on 7
server.
3.IMAP
 IMAP stands for Internet Message Access Protocol. It
was first proposed in 1986.Default port no is 143
 Key Points:
 IMAP allows the client program to manipulate the e-mail

Email handling with PHP


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
8
multiple mail servers.
STRUCTURE OF AN EMAIL MESSAGE
 There is a standard structure for emails. Email
contents are primarily classified as two, the header
and the body.

Email handling with PHP


 The Header
 The email header gives us common details about the
message such as the unique identity of the message.
1) Subject
 2) Sender (From:)
 3) Date and time received (On)
 4) Reply-to
 5) Recipient (To:)
 6) Recipient email address
 7) Attachments 9
 Subject
 The subject part is the topic of the message.

 Sender (From:)
 This field describes the ‘from’ address of the email. This will specify the sender’s email address.
Usually, it will be the “reply-to” address.

 Date and time received (On)


 This is the date and time the message received.

Email handling with PHP


 Reply-to
 This field describes the email address that will become the recipient of the reply to the particular
email. When you reply, it will go to this email address despite the sender email address.

 Recipient (To:)
 This is the first/last name of the email recipient as configured by the sender.

 Recipient email address


 The email address of the recipient is specified here.

 Attachments
 Some emails could be attached with files such as text, image, audio, video etc. These files are
specified here.

2) Body
 The actual content is stored in this part. This will be in the format of text. This field could also
include signatures or text generated automatically by the sender’s email system. 10
2.4 SENDING EMAIL WITH PHP
 PHP must be configured correctly in the php.ini file
with the details of how your system sends email.
Open php.ini file available in /etc/ directory and find
the section headed [mail function].
The configuration for windows:

Email handling with PHP



 [mail function] ;
 For Win32 only.
 SMTP = smtp.secureserver.net /smtp.gmail.com
 sendmail_from = [email protected]
 Linux users simply need to let PHP know the location
of their sendmail application. The path and any
desired switches should be specified to the
sendmail_path directive.
 The configuration for Linux:
11
 sendmail_path = /usr/sbin/sendmail -t -i
SENDING PLAIN TEXT EMAIL

 PHP makes use of mail() function to send an


email. This function requires three mandatory
arguments that specify the recipient's email

Email handling with PHP


address, the subject of the the message and the
actual message additionally there are other two
optional parameters.
 mail( to, subject, message, headers, parameters );

12
PARAMETER DESCRIPTION

Email handling with PHP


13
EXAMPLE :SENDING MAIL.
 Using mail() to send a simple email:
 <?php
[email protected];
$subject=“This is subject”;
$message = "Line 1\r\nLine 2\r\nLine 3";

Email handling with PHP



// In case any of lines are larger than 70 characters, w
e should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
$header=“from:[email protected]”;
 if(mail($to,$subject,$message,$header)
 {
 echo”Message sent successfully..”;}
 else
 { echo “Message could not be sent…”;}
?> 14
SENDING HTML EMAIL

 To send an HTML email


 Set MIME (Multipurpose Internet Mail

Email handling with PHP


Extensions) version,
 content type

 character set

15
Email handling with PHP
16
EXAMPLE
EMAIL ID VALIDATION
There are different methods to validate an email
address in PHP.

Email handling with PHP


 Method 1: Email validation using regular
expression.
 Method 2: Email validation using filter_var()
method.
 Method 3 : Email validation using
FILTER_SANITIZE_EMAIL filter.

17
METHOD 1: EMAIL VALIDATION USING
REGULAR EXPRESSION.

 This method uses regular expressions and inbuilt


email validation function. The input string is

Email handling with PHP


taken from the user and matches it with the
predefined regular expressions and if the regular
expression and input string found to be matched
than it returns true and proceed further.

18
HTML FORM
 <HTML>
 <BODY>

Email handling with PHP


 <h3>Enter email address!</h3>

 <form method=post action=“checkmail.php”>

 <input type=text name=txtmail size=40><br>

 <input type=submit value=“SUBMIT”>

 </form>

 </body>

 </html>

20
 <?php
 // PHP program to validate email
 $str=$_POST[‘txtmail’];
 //parse unnecessary characters to prevent exploits
 $email=htmlspecialchars(stripslashes(strip_tags($str)));
 //checks to make sure the email address is in a valid

Email handling with PHP


format
 if(ereg(“^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9]+(\.[a-z0-
9]+)*(\.[a-z]{2,3})$”,$email))
 {
 echo " Valid email address.";
 }
 else {
 echo “Invalid email address.";
 }
21

 ?>
METHOD 2: EMAIL VALIDATION USING
FILTER_VAR() METHOD.

 <?php
 $email = “[email protected]";

Email handling with PHP


 // Validate email

 if (filter_var($email,
FILTER_VALIDATE_EMAIL)) {
 echo("$email is a valid email address");
}

else {
echo("$email is not a valid email address");
}
22
 ?>
METHOD 3: EMAIL VALIDATION USING
FILTER_SANITIZE_EMAIL FILTER.
<?php
$email = “[email protected]";
// Remove all illegal characters from email
$email = filter_var($email, FILTER_SANITIZE_EMAIL);

Email handling with PHP


// Validate Email
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo("$email is a valid email address");
}
else {
echo("$email is not a valid email address");
}
?>
In the above example, use FILTER_SANITIZE_EMAIL 23
filter to remove all unsupported characters and then use
FILTER_VALIDATE_EMAIL filter to validate email.
EMAIL ID VERIFICATION
 We need to check whether the email address
corresponds to the real domain or not by ensuring
that the domain the user entered is registered in

Email handling with PHP


the DNS(Domain Name System).
 Validating email domains

 The checkdnsrr() function checks for the user


entered host name or the IP address in the
DNS.It returns true if any records are found,
returns false if no records are found i.e.in case of
any errors.

24
checkdnsrr() Function
The checkdnsrr() function is an inbuilt function in PHP which is used
to check the DNS records corresponding to the hostname or IP
address. This function can be used to verify whether a domain name
exists or not.

Email handling with PHP


Syntax:
bool checkdnsrr( string $host, string $type )
Parameters: This function accepts two parameters as mentioned
above and described below:
•$host: It is required parameter. It specifies the host name or IP
address which to be checked.
•$type: It is optional parameter. It specifies the type of DNS record to
be checked. Its possible values are: AAAA,CNAME, MX (default),
NAPTR, NS.
•Return Value: This function returns TRUE if records found, otherwise
returns FALSE.
25
EXAMPLE

<?php
$domain=“www.gmail.com";
if(checkdnsrr($domain,"MX")) {

Email handling with PHP


echo "Passed";
} else {
echo "Failed";
}
?>

26
Email handling with PHP
29
THANK YOU !

You might also like