- <?php
- //发送电子邮件
- /**
- $email_to = '[email protected],[email protected]';//email地址,多个地址用,分隔开
- $email_subject = '邮件标题';//email标题
- $email_message = '邮件内容';//邮件内容
- $charset = 'UTF-8';//邮件编码
- $mailconf = array(
- 'mailsend'=>'2',
- 'mailserver'=>'smtp.126.com',
- 'mailport'=>'25',
- 'mailauth'=>'1',
- 'mailfrom'=>'sjf122 <[email protected]>',
- 'mailauth_username'=>'[email protected]',
- 'mailauth_password'=>'******',
- 'maildelimiter'=>'0',
- 'mailsilent'=>'0',
- );
- sharpng_sendmail($email_to, $email_subject, $email_message, $charset, $mailconf);
- */
- function sharpng_sendmail($email_to, $email_subject, $email_message, $charset, $mailconf){
- /**
- * 必须参数
- * ++++++++++++++++++++
- * 收信人地址
- * $email_to 例如[email protected]
- * 邮件标题
- * $email_subject 例如 祝贺您称为sharpng的客户
- * 邮件内容
- * $email_message 例如 sharpng是最好的wap建站程序之一,在您的帮助下,sharpng将会更好的成长
- * 发送方式(三选一)
- * $mailsend=1 通过 PHP 函数的 sendmail 发送(推荐此方式)
- * $mailsend=2 通过 SOCKET 连接 SMTP 服务器发送(支持 ESMTP 验证)
- * $mailsend=3 通过 PHP 函数 SMTP 发送 Email(仅 Windows 主机下有效, 不支持 ESMTP 验证)
- * 邮件头的分隔符,请根据您邮件服务器的设置调整此参数(三选一)
- * $maildelimiter=0 使用 LF 作为分隔符(通常为 Unix/Linux 主机)
- * $maildelimiter=1 使用 CRLF 作为分隔符(通常为 Windows 主机)
- * $maildelimiter=2 使用 CR 作为分隔符(通常为 Mac 主机)
- * 屏蔽邮件发送中的全部错误提示(二选一)
- * $mailsilent=1 是
- * $mailsilent=0否
- * ++++++++++++++++++++
- * 下面的 参数根据$mailsend参数的值选择性输入
- * ----------
- * 假如$mailsend=2/3
- * $mailserver SMTP 服务器,例如smtp.126.com
- * $mailport SMTP 端口 默认为 25,例如25
- * ----------
- * 假如$mailsend=2
- * $mailauth=1 是/$mailauth=0 否 - 要求身份验证,如果 SMTP 服务器要求身份验证才可以发信,请选择“是”(二选一)
- * $mailfrom 发信人邮件地址 如果需要验证, 必须为本服务器的邮件地址。邮件地址中如果要包含用户名,格式为“username <[email protected]>”,也可以只填地址,例如 [email protected]
- * $mailauth_username SMTP 身份验证用户名,如126邮箱的是[email protected]
- * $mailauth_password SMTP 身份验证密码
- * ----------
- */
- while(list($k,$v) = each($mailconf)) {
- ($k == 'mailsend') ? $mailsend = $v : '';
- ($k == 'mailserver') ? $mailserver = $v : '';
- ($k == 'mailport') ? $mailport = $v : '';
- ($k == 'mailauth') ? $mailauth = $v : '';
- ($k == 'mailfrom') ? $mailfrom = $v : '';
- ($k == 'mailauth_username') ? $mailauth_username = $v : '';
- ($k == 'mailauth_password') ? $mailauth_password = $v : '';
- ($k == 'maildelimiter') ? $maildelimiter = $v : '';
- ($k == 'mailsilent') ? $sendmail_silent = $v : '';
- }
- if($sendmail_silent) {
- error_reporting(0);
- }
- //分隔符
- $maildelimiter = !emptyempty($maildelimiter) ? "\r\n" : "\n";
- $email_subject = '=?'.$charset.'?B?'.base64_encode(str_replace("\r", '', str_replace("\n", '', $email_subject))).'?=';
- $mailsite = strstr($email_to,'@');
- if (in_array($mailsite,array('@gmail.com',))) {
- $email_message = str_replace("\r\n.", " \r\n..", str_replace("\n", "\r\n", str_replace("\r", "\n", str_replace("\r\n", "\n", str_replace("\n\r", "\r", $email_message)))));
- } else {
- $email_message = chunk_split(base64_encode(str_replace("\r\n.", " \r\n..", str_replace("\n", "\r\n", str_replace("\r", "\n", str_replace("\r\n", "\n", str_replace("\n\r", "\r", $email_message)))))));
- }
- foreach(explode(',', $email_to) as $touser) {
- $tousers[] = $touser;
- }
- $email_to = implode(',', $tousers);
- $headers = "From: $mailfrom{$maildelimiter}MIME-Version: 1.0{$maildelimiter}Content-type: text/plain; charset=$charset{$maildelimiter}Content-Transfer-Encoding: base64{$maildelimiter}";
- if($mailsend == 1 && function_exists('mail')) {
- @mail($email_to, $email_subject, $email_message, $headers);
- } elseif($mailsend == 2) {
- if(!$fp = fsockopen($mailserver, $mailport, $errno, $errstr, 30)) {
- die ("SMTP($mailserver:$mailport CONNECT - Unable to connect to the SMTP server");
- }
- stream_set_blocking($fp, true);
- $lastmessage = fgets($fp, 512);
- if(substr($lastmessage, 0, 3) != '220') {
- die ("SMTP($mailserver:$mailport CONNECT - $lastmessage");
- }
- fputs($fp, ($mailcfg['auth'] ? 'EHLO' : 'HELO')." SupeSite\r\n");
- $lastmessage = fgets($fp, 512);
- if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250) {
- die ("SMTP($mailserver:$mailport HELO/EHLO - $lastmessage");
- }
- while(1) {
- if(substr($lastmessage, 3, 1) != '-' || emptyempty($lastmessage)) {
- break;
- }
- $lastmessage = fgets($fp, 512);
- }
- if($mailauth) {
- fputs($fp, "AUTH LOGIN\r\n");
- $lastmessage = fgets($fp, 512);
- if(substr($lastmessage, 0, 3) != 334) {
- die ("SMTP($mailserver:$mailport AUTH LOGIN - $lastmessage");
- }
- fputs($fp, base64_encode($mailauth_username)."\r\n");
- $lastmessage = fgets($fp, 512);
- if(substr($lastmessage, 0, 3) != 334) {
- die ("SMTP($mailserver:$mailport USERNAME - $lastmessage");
- }
- fputs($fp, base64_encode($mailauth_password)."\r\n");
- $lastmessage = fgets($fp, 512);
- if(substr($lastmessage, 0, 3) != 235) {
- die ("SMTP($mailserver:$mailport PASSWORD - $lastmessage");
- }
- $mailfrom = $mailfrom;
- }
- fputs($fp, "MAIL FROM: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $mailfrom).">\r\n");
- $lastmessage = fgets($fp, 512);
- if(substr($lastmessage, 0, 3) != 250) {
- fputs($fp, "MAIL FROM: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $mailfrom).">\r\n");
- $lastmessage = fgets($fp, 512);
- if(substr($lastmessage, 0, 3) != 250) {
- die ("SMTP($mailserver:$mailport MAIL FROM - $lastmessage");
- }
- }
- foreach(explode(',', $email_to) as $touser) {
- $touser = trim($touser);
- if($touser) {
- fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $touser).">\r\n");
- $lastmessage = fgets($fp, 512);
- if(substr($lastmessage, 0, 3) != 250) {
- fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $touser).">\r\n");
- $lastmessage = fgets($fp, 512);
- die ("SMTP($mailserver:$mailport RCPT TO - $lastmessage");
- }
- }
- }
- fputs($fp, "DATA\r\n");
- $lastmessage = fgets($fp, 512);
- if(substr($lastmessage, 0, 3) != 354) {
- die ("SMTP($mailserver:$mailport DATA - $lastmessage");
- }
- $headers .= 'Message-ID: <'.gmdate('YmdHs').'.'.substr(md5($email_message.microtime()), 0, 6).rand(100000, 999999).'@'.$_SERVER['HTTP_HOST'].">{$maildelimiter}X-Priority: 3{$maildelimiter}X-Mailer: Discuz! Mailer{$maildelimiter}";
- fputs($fp, "Date: ".gmdate('r')."\r\n");
- fputs($fp, "To: ".$email_to."\r\n");
- fputs($fp, "Subject: ".$email_subject."\r\n");
- fputs($fp, $headers."\r\n");
- fputs($fp, "\r\n\r\n");
- fputs($fp, "$email_message\r\n.\r\n");
- fputs($fp, "QUIT\r\n");
- } elseif($mailsend == 3) {
- ini_set('SMTP', $mailserver);
- ini_set('smtp_port', $mailport);
- ini_set('sendmail_from', $mailfrom);
- @mail($email_to, $email_subject, $email_message, $headers);
- }
- }