function _send_mail($to, $subject, $message, $nickname = '', $email = '', $attachments = array(), $priority = 3, $html = true, $smtp_config = array()) { $sys_config = jconf::get(); if (!($nickname && $email)) { $nickname = $sys_config['site_name']; $email = $sys_config['site_admin_email']; } $smtp_config = $smtp_config ? $smtp_config : jconf::get('smtp'); if ($smtp_config['enable'] && is_array($smtp_config['smtp']) && count($smtp_config['smtp'])) { $k = array_rand($smtp_config['smtp']); $smtp = $smtp_config['smtp'][$k]; if (is_array($smtp) && count($smtp)) { if ($nickname && $email) { $smtp['email_from'] = "{$nickname} <{$email}>"; } return _send_mail_by_smtp($to, $subject, $message, $smtp, $html); } else { jlog('SMTP', '$smtp is empty', 0); } } else { $charset = $sys_config['charset']; $jishigou_mail = jclass('jishigou/mail'); if (is_array($attachments) and count($attachments) >= 1) { $boundary = "----_NextPart_" . md5(uniqid(time())) . "_000"; $jishigou_mail->SetHeader('Content-Type: multipart/mixed;boundary="' . $boundary . '"'); $body = "--" . $boundary . "" . NEW_LINE . ""; $body .= "Content-Type: text/" . ($html ? 'html' : 'plain') . "; charset=\"" . $sys_config['charset'] . "\"" . NEW_LINE . ""; $body .= "Content-Transfer-Encoding: base64" . NEW_LINE . "" . NEW_LINE . ""; $body .= chunk_split(base64_encode($message)) . "" . NEW_LINE . ""; foreach ($attachments as $attachment) { $body .= "--" . $boundary . "" . NEW_LINE . ""; $body .= "Content-Type: application/octet-stream;" . NEW_LINE . "\t\tname=\"{$attachment['name']}\"" . NEW_LINE . ""; $body .= "Content-Transfer-Encoding: base64" . NEW_LINE . ""; $body .= "Content-Disposition: attachment;" . NEW_LINE . "\t\tFileName=\"{$attachment['name']}\"" . NEW_LINE . "" . NEW_LINE . ""; $body .= chunk_split(base64_encode(file_get_contents($attachment['path']))) . "" . NEW_LINE . ""; } $message = $body; } else { $jishigou_mail->SetHeader('Content-Type: text/' . ($html ? 'html' : 'plain') . '; charset=' . $sys_config['charset']); $jishigou_mail->SetHeader('Content-Transfer-Encoding: base64'); $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", $message))))))); } $subject = '=?' . $charset . '?B?' . base64_encode(str_replace("\r", '', str_replace("\n", '', $subject))) . '?='; $nickname = '=?' . $charset . '?B?' . base64_encode($nickname) . "?="; $jishigou_mail->SetSenderName($nickname); $jishigou_mail->SetSenderMail($email); $jishigou_mail->SetSendMailFrom($email); $jishigou_mail->SetUseHtml($html); $jishigou_mail->SetHeader("Return-Path: {$email}"); $jishigou_mail->SetHeader("MIME-Version: 1.0"); $jishigou_mail->SetHeader("X-Priority: {$priority}"); $jishigou_mail->SetHeader("Sender: {$email}"); $jishigou_mail->SetRecipient($to); $jishigou_mail->SetSubject($subject); $jishigou_mail->SetMessage($message); return $jishigou_mail->doSend(); } }
function mail_send_test() { $k = jget('k', 'int'); $smtp = jconf::get('smtp'); $test_smtp = $smtp['smtp'][$k]; jfunc('mail'); $ret = _send_mail_by_smtp($test_smtp['mail'], $this->Config['site_name'] . '-' . date('Y-m-d H:i:s'), '邮件测试正文---' . date('Y-m-d H:i:s'), $test_smtp); if ($ret) { echo '发送成功。'; } else { echo '发送不成功,请检查配置是否正确。'; } exit; }
function send_mail($to,$subject,$message,$nickname='',$email='',$attachments=array(),$priority=3,$html=true,$smtp_config=array()) { if(!($nickname && $email)) { $sys_config = ConfigHandler::get(); $nickname = $sys_config['site_name']; $email = $sys_config['site_admin_email']; } $smtp_config = $smtp_config ? $smtp_config : ConfigHandler::get('smtp'); if($smtp_config['enable']) { if($nickname && $email) $smtp_config['email_from'] = "{$nickname} <{$email}>"; return _send_mail_by_smtp($to,$subject,$message,$smtp_config,$html); } else { static $MailHandler=null; if(is_null($MailHandler)!=false) { $MailHandler=new MailHandler($email,$nickname,true); } if(is_array($attachments) and count($attachments)>=1) { $boundary="----_NextPart_".md5(uniqid(time()))."_000"; $MailHandler->SetHeader('Content-Type: multipart/mixed;boundary="'.$boundary.'"'); $body="--".$boundary."".NEW_LINE.""; $body.="Content-Type: text/html; charset=\"GB2312\"".NEW_LINE.""; $body.="Content-Transfer-Encoding: base64".NEW_LINE."".NEW_LINE.""; $body.=chunk_split(base64_encode($message))."".NEW_LINE.""; foreach($attachments as $attachment) { $body.="--".$boundary."".NEW_LINE.""; $body.="Content-Type: application/octet-stream;".NEW_LINE."\t\tname=\"{$attachment['name']}\"".NEW_LINE.""; $body.="Content-Transfer-Encoding: base64".NEW_LINE.""; $body.="Content-Disposition: attachment;".NEW_LINE."\t\tFileName=\"{$attachment['name']}\"".NEW_LINE."".NEW_LINE.""; $body.=chunk_split(base64_encode(file_get_contents($attachment['path'])))."".NEW_LINE."";; } $message=$body; } else { if(false!=$html) { $MailHandler->SetHeader('Content-Type: text/html; charset=gb2312'); } } $MailHandler->SetSenderName($nickname); $MailHandler->SetSenderMail($email); $MailHandler->SetSendMailFrom($email); $MailHandler->SetUseHtml($html); $MailHandler->SetHeader("Return-Path: {$email}"); $MailHandler->SetHeader("MIME-Version: 1.0"); $MailHandler->SetHeader("X-Priority: $priority"); $MailHandler->SetHeader("Sender: {$email}"); $MailHandler->SetRecipient($to); $MailHandler->SetSubject($subject); $MailHandler->SetMessage($message); Return $MailHandler->doSend(); } }