2018年6月28日 星期四

【PHP】簡易mail發送

// 寄信 email可用逗點分隔一次寄給多人
function send_mail($subject, $content, $email, $cc=array()){
 $charset = 'utf-8';
 $sitename = PROJECT_NAME;
 $ADM_EMAIL = ADM_EMAIL;
 $encoded_subject = mail_header_encode($subject,$charset);
 $sitename2 = mail_header_encode($sitename,$charset);  
 
 $headers = "From: ".$sitename2 ."<".$ADM_EMAIL.">". "\r\n";
 $headers .= "Content-Type: text/html; charset=$charset; format=flowed". "\r\n";
 $headers .= "MIME-Version: 1.0". "\r\n";
 $headers .= "Content-Transfer-Encoding: 8bit". "\r\n";
 $headers .= "X-Mailer: PHP". "\r\n";
 
 if ($cc){
  foreach($cc as $mail){
   $headers .= "Cc: $mail" . "\r\n";
  }
 }
 
 $r = mail($email, $encoded_subject, $content, $headers);
 return $r;
}

// 寄信用的編碼
function mail_header_encode($str,$charsets){
 return "=?$charsets?B?".base64_encode($str)."?=";
}

沒有留言:

張貼留言