Tải bản đầy đủ (.ppt) (17 trang)

Slide môn học PHP session 8 handling email with PHP

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (186.57 KB, 17 trang )

Handling Email with PHP
Session 19


Review









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
PHP / Session 19 / Slide 2 of 17


Objectives




Send Mails
Use attachments with mails

PHP / Session 19 / Slide 3 of 17


Email




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

PHP / Session 19 / Slide 4 of 17


php.ini file


The section for mail function in the php.ini file look like:

PHP / Session 19 / Slide 5 of 17


Sending 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
19 / Slide 6 of 17
string message – Specifies the messages that willPHPbe/ Session
written
in the mail



Sending email to a single user - I


For example, to send a mail using the mail() function
$to=“ ”;
$from=“ ”;
$subject=”An example”;
$body=”This is an example.”;
$send=mail($to, $subject, $body, $from);
if($send)
{
echo “Mail Sent to $to address!!!”;
}
PHP / Session 19 / Slide 7 of 17


Sending email to a single user - II
else
{
echo “Mail could not be sent to $to
address!!!”;
}
?>

Here, the mail is sent to from



PHP / Session 19 / Slide 8 of 17


Sending email to multiple users I


For example, to send a mail to multiple recipients
using mail() function
$to=“,
m,”;
$from=“”;
for($i=0;$i{
$to[$i]=trim($to[$i]);
$subject=“An example”;
$body= “This is an example.”;
$send=mail($to, $subject, $body, $from);
PHP / Session 19 / Slide 9 of 17


Sending 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

PHP / Session 19 / Slide 10 of 17


Using attachment with email




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


PHP / Session 19 / Slide 11 of 17


Example for using attachment

with email - I


For example, to send an email with an attachment
$file = “/var/www/html/job.txt”;
$fp = fopen($file,“r”);
$content=fread($fp,filesize($file));
fclose($fp);
$to=“ ”;
$from=“ ”;
$subject=“Urgent Opening”;
$body=“”;
$mime_boundary = uniqid(“”);
$header.= $from;
$header.= $to;
$header.= “MIME-VERSION: 1.0\r\n”;
$header.=”Content-type: multipart/mixed”;
PHP / Session 19 / Slide 12 of 17


Example for using attachment

with email - II


$header.= “boundary=\””.$mime_boundary.”\””;
$message.=”Urgent Requirement.”;
$message.=”\r\n\r\n”;
$message.=”—“.$mime_boundary.”\r\n”;
$message.=”Content-type:text/plain;charset=\“iso-88591\”\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);
PHP / Session 19 / Slide 13 of 17


Example 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!!!”;
}
?>

PHP / Session 19 / Slide 14 of 17


Example 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
PHP / Session 19 / Slide 15 of 17



Executing the codes


Execute the codes in the Mozilla Web browser.
The output appears as:

PHP / Session 19 / Slide 16 of 17


Summary










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
PHP / Session 19 / Slide 17 of 17



×