PHP实现发送邮件的方法(基于简单邮件发送类)
发布时间:2020-05-23 17:59:26 所属栏目:PHP 来源:互联网
导读:这篇文章主要介绍了PHP实现发送邮件的方法,基于简单邮件发送类实现该功能.给出了简单邮件发送类,并说明了具体使用方法,需要的朋友可以参考下
|
本文实例讲述了PHP实现发送邮件的方法。分享给大家供大家参考,具体如下: 邮件发送类 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 = $log_file; //邮件发送成功失败的日志记录文件 $this->sock = FALSE; } function mail_encode($str) { //return '=?utf-8?B?'.base64_encode(mb_convert_encoding($str,"GBK","UTF-8")).'?='; return "=?UTF-8?B?".base64_encode($str)."?="; } /* 主函数,发送邮件 */ function sendmail($flag,$boundary,$to,$from,$subject = "",$body = "",$mailtype,$cc = "",$bcc = "",$additional_headers = "") { $mail_from = $this->get_address ( $this->strip_comment ( $from ) ); $body = ereg_replace ( '(^|(rn))(.)',"1.3",$body ); $header = "MIME-Version:1.0rn"; if ($mailtype == "HTML") { if ($flag == 2) { $header .= "Content-Type:multipart/mixed; boundary= $boundaryrn"; } else { $header .= "Content-Type:text/html; charset="UTF-8" rn"; } } $header .= "To: " . $to . "rn"; if ($cc != "") { $header .= "Cc: " . $cc . "rn"; } $header .= "From: $from<" . $from . ">rn"; $subject = self::mail_encode($subject); $header .= "Subject: " . $subject . "rn"; $header .= $additional_headers; $header .= "Date: " . date ( "r" ) . "rn"; $header .= "X-Mailer:By redhat (PHP/" . phpversion () . ")rn"; list ( $msec,$sec ) = explode ( " ",microtime () ); $header .= "Message-ID: <" . date ( "YmdHis",$sec ) . "." . ($msec * 1000000) . "." . $mail_from . ">rn"; $TO = explode ( ",",$this->strip_comment ( $to ) ); if ($cc != "") { $TO = array_merge ( $TO,explode ( ",$this->strip_comment ( $cc ) ) ); } if ($bcc != "") { $TO = array_merge ( $TO,$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 hostn" ); } return $sent; } /* 私有函数 */ function smtp_send($helo,$body = "") { if (! $this->smtp_putcmd ( "HELO",$helo )) { return $this->smtp_error ( "sending HELO command" ); } if ($this->auth) { if (! $this->smtp_putcmd ( "AUTH LOGIN",base64_encode ( $this->user ) )) { return $this->smtp_error ( "sending HELO command" ); } if (! $this->smtp_putcmd ( "",base64_encode ( $this->pass ) )) { return $this->smtp_error ( "sending HELO command" ); } } if (! $this->smtp_putcmd ( "MAIL","FROM:<" . $from . ">" )) { return $this->smtp_error ( "sending MAIL FROM command" ); } if (! $this->smtp_putcmd ( "RCPT","TO:<" . $to . ">" )) { return $this->smtp_error ( "sending RCPT TO command" ); } if (! $this->smtp_putcmd ( "DATA" )) { return $this->smtp_error ( "sending DATA command" ); } if (! $this->smtp_message ( $header,$body )) { return $this->smtp_error ( "sending message" ); } if (! $this->smtp_eom ()) { return $this->smtp_error ( "sending调用邮件发送类发送邮件 debug = TRUE;//是否显示发送的调试信息
if ($smtp->sendmail ( $flag,$smtpemailto,$smtpmailfrom,$mailsubject,$mailbody,$cc,$bcc,$additional_headers )) {
echo ("发送成功");
} else {
echo ("发送失败");
}
}
?>
希望本文所述对大家php程序设计有所帮助。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
