[PHP] email php发送邮件 →→→→→进入此内容的聊天室

来自 , 2019-04-27, 写在 PHP, 查看 155 次.
URL http://www.code666.cn/view/86b20716
  1. function sendEmail($to, $from, $subject, $message, $html_message=null) {
  2.         $eol = "\n";
  3.  
  4.         $mime_boundary = md5(time());  
  5.  
  6.         $mime_boundary_header = chr(34) . $mime_boundary . chr(34);
  7.  
  8.         if (empty($html_message)) {
  9.             $html_message = nl2br($message);
  10.         }
  11.  
  12.         $headers = "From: $from" . $eol;
  13.         $headers .= "Message-ID: <" . time() . " domain>" . $eol;
  14.         $headers .= "X-Mailer: PHP v" . phpversion() . $eol;          // These two to help avoid spam-filters
  15.         $headers .= 'MIME-Version: 1.0' . $eol;
  16.  
  17.         // Setup for text OR html
  18.         $headers .= "Content-Type: multipart/alternative; boundary=" .
  19.             $mime_boundary_header . $eol . $eol;
  20.         $msg = "This is a multi-part message in MIME format to $to." .
  21.             $eol . $eol;
  22.  
  23.         // Text Version
  24.         $msg .= "--" . $mime_boundary . $eol;
  25.         $msg .= "Content-Type: text/plain;" .$eol . $eol;
  26.  
  27.         //$msg .= "Content-Transfer-Encoding: base64".$eol;
  28.         //$msg .= base64_encode($txt_body).$eol.$eol;
  29.         $msg .= $message . $eol . $eol;
  30.  
  31.         // HTML Version
  32.         $msg .= "--".$mime_boundary . $eol;
  33.         $msg .= "Content-Type: text/html;{$eol}Content-Transfer-Encoding: 7bit" .
  34.             $eol . $eol;
  35.  
  36.         //$msg .= "Content-Transfer-Encoding: base64".$eol;
  37.         //$msg .= base64_encode($html_body).$eol.$eol;
  38.         $msg .= $html_message . $eol . $eol;
  39.  
  40.         // finish with two eol's for better security. see Injection.
  41.         $msg .= "--" . $mime_boundary . "--" . $eol . $eol;
  42.  
  43.         if (isset($_SERVER) && isset($_SERVER['HTTP_HOST']) && !preg_match('/\.com$/', $_SERVER['HTTP_HOST'])) {
  44.             // DEV
  45.             preg_match('/[\w|\d|\ |\-]+/', $subject, $subject_label);
  46.             if (defined('DOCUMENT_ROOT')) {
  47.                 $logfile = DOCUMENT_ROOT .'/scripts/logs/sendEmail/mail_'. microtime(true);
  48.  
  49.             } else {
  50.                 $logfile = '../../scripts/logs/sendEmail/mail_'. microtime(true);
  51.             }
  52.  
  53.             if (isset($subject_label[0])) {
  54.                 $logfile .= ' - '. $subject_label[0];
  55.             }
  56.             $fh = fopen($logfile . '.log', 'w+');
  57.  
  58.             if (!$fh) {
  59.                 $fh = fopen(preg_replace('/^\.\.\//', '', $logfile) . '.log', 'w+');
  60.             }
  61.  
  62.             if ($fh) {
  63.                 $filelog_content = date('Y-m-d H:i:s')."\n\n";
  64.                 $filelog_content .= print_r($to, true)."\n";
  65.                 $filelog_content .= print_r($subject, true)."\n\n";
  66.                 $filelog_content .= print_r($msg, true);
  67.  
  68.                 fwrite($fh, $filelog_content);
  69.                 fclose($fh);
  70.             }
  71.  
  72.         } else {
  73.             mail($to, $subject, $msg, $headers, "-fdonotreply@domain.com");
  74.         }
  75.     }
  76.  

回复 "email php发送邮件"

这儿你可以回复上面这条便签

captcha