加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php mail()函数用方实例

发布时间:2020-12-13 05:17:58 所属栏目:PHP教程 来源:网络整理
导读:定义和用法 mail() 函数允许您从脚本中直接发送电子邮件。 如果邮件的投递被成功地接收,则返回 true,否则返回 false。 语法 mail(to,subject,message,headers,parameters) 参数 描述 to 必需。规定邮件的接收者。 subject 必需。规定邮件的主题。该参数不

定义和用法

mail() 函数允许您从脚本中直接发送电子邮件。

如果邮件的投递被成功地接收,则返回 true,否则返回 false。

语法

mail(to,subject,message,headers,parameters)

参数

描述

to

必需。规定邮件的接收者。

subject

必需。规定邮件的主题。该参数不能包含任何换行字符。

message

必需。规定要发送的消息。

headers

必需。规定额外的报头,比如 From,Cc 以及 Bcc。

parameters

必需。规定 sendmail 程序的额外参数。

说明

在 message 参数规定的消息中,行之间必须以一个 LF(n)分隔。每行不能超过 70 个字符。 (Windows 下)当 PHP 直接连接到 SMTP 服务器时,如果在一行开头发现一个句号,则会被删掉。要避免此问题,将单个句号替换成两个句号。

$text = str_replace("n.","n..",$text);

?>

实例一:发送html邮件

? ?

? ?

? ? ? Sending HTML email using PHP

? ?? ?

? ?

? ? ?

? ? ? ? ?$to = "xyz@somedomain.com";

? ? ? ? ?$subject = "This is subject";

? ? ? ? ?$message = "This is HTML message.";

? ? ? ? ?$message .= "

This is headline.

";

? ? ? ? ?$header = "From:abc@somedomain.com rn";

? ? ? ? ?$header = "Cc:afgh@somedomain.com rn";

? ? ? ? ?$header .= "MIME-Version: 1.0rn";

? ? ? ? ?$header .= "Content-type: text/htmlrn";

? ? ? ? ?$retval = mail ($to,$subject,$message,$header);

? ? ? ? ?if( $retval == true )

? ? ? ? ?{

? ? ? ? ? ? echo "Message sent successfully...";

? ? ? ? ?}

? ? ? ? ?else

? ? ? ? ?{

? ? ? ? ? ? echo "Message could not be sent...";

? ? ? ? ?}

? ? ? ?>

? ?

实例二:发送带附件的邮件

? ?// request variables // important

? ?$from=$_REQUEST["from"];

? ?$emaila=$_REQUEST["emaila"];

? ?$filea=$_REQUEST["filea"];

? ?if ($filea)

? ?{

? ? ? function mail_attachment ($from,$to,$attachment){

? ? ? ? ?$fileatt = $attachment; // Path to the file

? ? ? ? ?$fileatt_type = "application/octet-stream"; // File Type?? ? ? ?

? ? ? ? ?$start = strrpos($attachment,'/') == -1 ? strrpos($attachment,'//') : strrpos($attachment,'/')+1;

? ? ? ? ?$fileatt_name = substr($attachment,$start,strlen($attachment)); // Filename that will be used for the file as the attachment?

? ? ? ? ?$email_from = $from; // Who the email is from

? ? ? ? ?$subject = "New Attachment Message";

? ? ? ? ?$email_subject = ?$subject; // The Subject of the email?

? ? ? ? ?$email_txt = $message; // Message that the email has in it?

? ? ? ? ?$email_to = $to; // Who the email is to

? ? ? ? ?$headers = "From: ".$email_from;

? ? ? ? ?$file = fopen($fileatt,'rb');?

? ? ? ? ?$data = fread($file,filesize($fileatt));?

? ? ? ? ?fclose($file);?? ?

? ? ? ? ?$msg_txt="nn You have recieved a new attachment message from $from";

? ? ? ? ?$semi_rand = md5(time());?

? ? ? ? ?$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";?

? ? ? ? ?$headers .= "nMIME-Version: 1.0n" . "Content-Type: multipart/mixed;n" . " boundary="{$mime_boundary}"";

? ? ? ? ?$email_txt .= $msg_txt;

? ? ? ? ?$email_message .= "This is a multi-part message in MIME format.nn" . "--{$mime_boundary}n" . "Content-Type:text/html; charset="iso-8859-1"n" . "Content-Transfer-Encoding: 7bitnn" . $email_txt . "nn";

? ? ? ? ?$data = chunk_split(base64_encode($data));

? ? ? ? ?$email_message .= "--{$mime_boundary}n" . "Content-Type: {$fileatt_type};n" . " name="{$fileatt_name}"n" . //"Content-Disposition: attachment;n" . //" filename="{$fileatt_name}"n" . "Content-Transfer-Encoding: base64nn" . $data . "nn" . "--{$mime_boundary}--n";

? ? ? ? ?$ok = mail($email_to,$email_subject,$email_message,$headers);

? ? ? ? ?if($ok)

? ? ? ? ?{

? ? ? ? ? ? echo "File Sent Successfully.";

? ? ? ? ? ? unlink($attachment); // delete a file after attachment sent.

? ? ? ? ?}? ? ? ? ?

? ? ? ? ?else

? ? ? ? ?{

? ? ? ? ? ? die("Sorry but the email could not be sent. Please go back and try again!");

? ? ? ? ?}

? ? ? }

? ? ? move_uploaded_file($_FILES["filea"]["tmp_name"],'temp/'.basename($_FILES['filea']['name']));

? ? ? mail_attachment("$from","youremailaddress@gmail.com","subject","message",("temp/".$_FILES["filea"]["name"]));

? ?}

?>

? ?? ??

? ? ?

? ?

? ?

? ? ?

? ? ? ? ?

? ? ? ? ? ?

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ?? ? ? ? ? ? ? ?

? ? ? ? ? ?

? ? ? ? ?

? ? ?

? ?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!