Performs email sending process using the mail function mail function accepts three arguments.. Those are: Recipient’s email address Subject of the email Body of the email
Trang 1Handling Email with PHP
Session 19
Trang 2 Cookies provide us with the functionality of storing temporary
Web user information
Sessions enable Web sites store user requests and information
on the Web
Session library enables:
Creation
Serialization
Storage of session data
php.ini file located under the usr/local/php4/lib directory
Sessions enable PHP store user information on the Web server
Review
Trang 3 Send Mails
Use attachments with mails
Trang 4 Performs email sending process using the mail()
function
mail() function accepts three arguments Those are:
Recipient’s email address
Subject of the email
Body of the email
Recipient’s email address is compulsory Other
arguments are optional.
Local mail server for mail() function is specified in
the php.ini configuration file
Trang 5php.ini file
The section for mail function in the php.ini file look like:
Trang 6Sending Email
Syntax for the mail() function is:
mail(string to,string subject,string message
[, string additional_headers])
Where,
string to – Specifies the receiver’s mail addresses
string subject – Specifies the subject for the email
string message – Specifies the messages that will be written in the mail
string additional_headers – Specifies the mail sender’s address
Trang 7Sending email to a single
user - I
For example, to send a mail using the mail() function
<?php
$to=“john@bluemountain.com ”;
$from=“tom@bluemountain.com ”;
$subject=”An example”;
$body=”This is an example.”;
$send=mail($to, $subject, $body, $from);
if($send)
{
echo “Mail Sent to $to address!!!”;
}
Trang 8Sending email to a single
user - II
else
{
echo “Mail could not be sent to $to
address!!!”;
}
?>
Here, the mail is sent to john@bluemountain.com from
tom@bluemountain.com
Trang 9Sending email to multiple
users - I
using mail() function
<?php
$to=“john@bluemountain.com,sam@bluemountain.com
,j@bluemountain.com”;
$from=“samson@bluemountain.com”;
for($i=0;$i<count($to);$i++)
{
$to[$i]=trim($to[$i]);
$subject=“An example”;
$body= “This is an example.”;
$send=mail($to, $subject, $body, $from);
Trang 10Sending email to multiple
users - II
if($send)
{
echo “Mail was sent to all the addresses!!!”;
}
}
?>
Here, the mail is sent to all the users mentioned in the
receiver’s list from samson@bluemountain.com
Trang 11Using attachment with
An attachment is any text or graphics file sends with the email
Uses an additional parameter, header, with the mail() function
Syntax for the mail function with additional parameter is:
mail(string to, string subject, string message [, string additional_headers[, string additional_parameters]])
Where,
string to – Specifies the receiver’s mail addresses
string subject – Specifies the subject for the email
string message – Specifies the messages that will be written in the mail
string additional_headers – Specifies the mail sender’s address
string additional_parameters – Specifies the headers those are set for attaching a file with the email
Trang 12Example for using
attachment with email - I
For example, to send an email with an attachment
<?php
$file = “/var/www/html/job.txt”;
$fp = fopen($file,“r”);
$content=fread($fp,filesize($file));
fclose($fp);
$to=“john@bluemountain.com ”;
$from=“jenny@bluemountain.com ”;
$subject=“Urgent Opening”;
$body=“”;
$mime_boundary = uniqid(“”);
$header.= $from;
$header.= $to;
$header.= “MIME-VERSION: 1.0\r\n”;
$header.=”Content-type: multipart/mixed”;
Trang 13Example for using
attachment with email - II
$header.= “boundary=\””.$mime_boundary.”\””;
$message.=”Urgent Requirement.”;
$message.=”\r\n\r\n”;
$message.=”—“.$mime_boundary.”\r\n”;
1\”\r\n”;
$message.=”—“.$mime_boundary.”\r\n”;
$message.=“Content-Disposition:attachment;\r\n”;
$message.=“name=\“filename.extn\”\r\n”;
$message.=”\r\n\r\n”;
$message.=$file;
$message.=”\r\n\r\n”;
$message.=”—“.$mime_boundary.”\r\n”;
$content=“<P>.$content</P>”;
$content=str_replace(chr(10i,“<P></P>”,$content);
$mail_send=mail($to, $subject, $body, $message, $header);
Trang 14Example for using
attachment with email - III
if($mail_send)
{
echo “To : $to   From : $from <P>”;
echo “Subject : $subject<P>”;
echo “The attached file content :<P>”
echo $content;
}
else
{
echo “<BR><BR>Mail could not be sent!!!”;
}
?>
Trang 15Example for using
attachment with email - IV
In this example, a boundary is made to separate
the original mail from the attachments
Boundary is made with the help of uniqid()
function This function is used for assigning a
unique identifier to the original mail
The value of this function is assigned to the
mime_boundary variable
The content-type header indicates the type of
message that is attached with the email
Trang 16Executing the codes
Execute the codes in the Mozilla Web browser
The output appears as:
Trang 17 mail() function is the inbuilt function of PHP that is used for
sending mails
The implode() function is used for joining all the addresses
present in the array with a comma operator
A boundary is created in between the actual e-mail and the
attachment with the help of the uniqid() function
The base64_encode() function is used for accepting the data in the form of a string and returns the data in the original form to the
user
The chunk_split() function is used for separating the data into smaller portions