1. Trang chủ
  2. » Mẫu Slide

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

17 264 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 17
Dung lượng 340 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

 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 1

Handling 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 5

php.ini file

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

Trang 6

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

 string message – Specifies the messages that will be written in the mail

 string additional_headers – Specifies the mail sender’s address

Trang 7

Sending 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 8

Sending 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 9

Sending 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 10

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 samson@bluemountain.com

Trang 11

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

Trang 12

Example 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 13

Example 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 14

Example for using

attachment with email - III

if($mail_send)

{

echo “To : $to &nbsp 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 15

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

Trang 16

Executing 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

Ngày đăng: 30/11/2016, 22:12

TỪ KHÓA LIÊN QUAN