码迷,mamicode.com
首页 > Web开发 > 详细

PHP通过mail()或Socket发邮件

时间:2015-08-01 23:12:31      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

PHP通过mail()或Socket发邮件

1.PHP中发送邮件的方法

PHP发送邮件是“非常的简单” 因为他提供了mail()函数直接发送,但这也继

 register globals 成为了对初学者的第二大杀手。

(1)通过mail()函数发送邮件

技术分享

(2)通过socket通讯,使用SMTP传输

技术分享

2.mail()函数的使用

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

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

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

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

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

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

headers 可选。规定额外的报头,比如 From, Cc 以及 Bcc。

parameters 可选。规定 sendmail 程序的额外参数。 

PHP.ini  的配置  

mail配置

SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

if (@mail($to, $subject, $message, $headers)) {
    echo "支持mail发送";
} else {
    $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
    $smtp->debug = FALSE;//是否显示发送的调试信息
    $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);

}

 

3.socket()方式发送原理

使用fsockopen函数打开一个Internet连接

fsockopen(string hostname, int port, int [errno], string [errstr], int [timeout]); 

这里由于要使用SMTP协议,所以端口号为25。在打开连接成功后,会返回一

 个socket句柄,使用它就可以象使用文件句柄一样的。可使用的操作有fputs(),

fgets(),feof(),fclose() 

220 服务就绪(在socket连接成功时,会返回此信息)

221 正在处理

250 请求邮件动作正确,

354 开始发送数据,结束以 .

500 语法错误,命令不能识别

550 命令不能执行,邮箱无效

552 中断处理:用户超出文件空间 

function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
{
$this->debug = FALSE;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 30; //is used in fsockopen()
#
$this->auth = $auth;//auth
$this->user = $user;
$this->pass = $pass;
#
$this->host_name = "localhost"; //is used in HELO command
$this->log_file ="";

$this->sock = FALSE;
}
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
{
$mail_from = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|(\r\n))(\\.)", "\\1.\\3", $body);
$header .= "MIME-Version:1.0\r\n";
if($mailtype=="HTML"){
$header .= "Content-Type:text/html\r\n";
}
$header .= "To: ".$to."\r\n";
if ($cc != "") {
$header .= "Cc: ".$cc."\r\n";
}
$header .= "From: $from<".$from.">\r\n";
$header .= "Subject: ".$subject."\r\n";
$header .= $additional_headers;
$header .= "Date: ".date("r")."\r\n";
$header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";
list($msec, $sec) = explode(" ", microtime());
$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";
$TO = explode(",", $this->strip_comment($to));

if ($cc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
}

if ($bcc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
}

$sent = TRUE;
foreach ($TO as $rcpt_to) {
$rcpt_to = $this->get_address($rcpt_to);
if (!$this->smtp_sockopen($rcpt_to)) {
$this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
$sent = FALSE;
continue;
}
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
$this->log_write("E-mail has been sent to <".$rcpt_to.">\n");
} else {
$this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
$sent = FALSE;
}
fclose($this->sock);
$this->log_write("Disconnected from remote host\n");
}
echo "<br>";
echo $header;
return $sent;
}

实现代码:

<?php
require_once("email.class.php");
$to      = ‘chenyigenalex@qq.com‘;
$subject = ‘the subject‘;
$message = ‘hello‘;
$headers = ‘From: webmaster@example.com‘ . "\r\n" .
    ‘Reply-To: webmaster@example.com‘ . "\r\n" .
    ‘X-Mailer: PHP/‘ . phpversion();
$smtpserver = "smtp.163.com";//SMTP服务器
$smtpserverport =25;//SMTP服务器端口
$smtpusermail = "jasonchen_love@163.com";//SMTP服务器的用户邮箱
$smtpemailto = "buyaofafeiwo@qq.com";//发送给谁
$smtpuser = "jasonchen_love";//SMTP服务器的用户帐号
$smtppass = "123456";//SMTP服务器的用户密码
$mailsubject = "PHP测试邮件系统";//邮件主题
$mailbody = "<h1> 这是一个测试程序http://www.cnblogs.com/chenyigen/ </h1>";//邮件内容
$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
if (@mail($to, $subject, $message, $headers)) {
    echo "支持mail发送";
} else {
    $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
    $smtp->debug = FALSE;//是否显示发送的调试信息
    $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);

}

?>

 

PHP通过mail()或Socket发邮件

标签:

原文地址:http://www.cnblogs.com/chenyigen/p/4694718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!