1. Home
  2. /
  3. Web technology
  4. /
  5. PHP
  6. /
  7. [ PHP ] –...

[ PHP ] – PHPMailer 附件 寄多個MAIL 函式範例

評等結果
點擊便能為這篇文章進行評等!
[評等總次數: 0,平均評等: 0]

PHPMailer 附件 寄多個MAIL 函式範例

最近專案常常用到,所以寫了個簡單的小函式 把寄檔案附件及 同時 CC 給多人的功能都寫進來。

if (!function_exists('class_sendMail')) {
function class_sendMail($subject, $html, $user,$file='',$recipient=''){
//其它收件人
$recipient_ar = explode(',',$recipient);//以逗號分割
$mail = new PHPMailer;
$mail->isSMTP();// Set mailer to use SMTP
$mail->Host = 'mail.xxx.com.tw';//您的 mail server 位置
$mail->SMTPAuth = true;
$mail->Username = '帳號';
$mail->Password = '密碼';
$mail->SMTPSecure = false;// Enable TLS encryption, `ssl` also accepted
$mail->SMTPAutoTLS = false;
$mail->CharSet = "UTF-8";
$mail->Port = 25; // TCP port to connect to
$mail->setFrom('寄件信箱', '寄件者');
$mail->addAddress($user);// Add a recipient
foreach($recipient_ar as $value){
$mail->addAddress($value);// Add a recipient
}
$mail->isHTML(true);// Set email format to HTML
if($file!=''){
$mail->AddAttachment('檔案路徑'.$file); // 設定附件檔檔名 注意此範例 $file 只是檔名,若你檔案有分多層請連檔案路徑一同寫在 $file 並移除 '檔案路徑'. 字串
}
$mail->Subject = $subject;
$mail->Body = $html;
if(!$mail->send()) {return false;} else { return true;}
}
}
//載入寄信class
require_once "./plugins/PHPMailer/PHPMailerAutoload.php";
require_once "./plugins/PHPMailer/class.phpmailer.php";
require_once "./plugins/PHPMailer/class.smtp.php";
$html ='<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>信件標題 </title>
</head>
<body>';
$html .= '<p>信件內容</p>';
$html .= '</body></html>';
$user ='收件者mail';
$subject = '寄件標題';
$from = '寄件者mial';
$headers = "Content-type: text/html; charset=utf-8\r\n" ."From: $from\r\n";
$recipient='a@test.com.tw,b@test.com.tw,c@test.com.tw';//除了本來的收件者 還要寄給這些收件者時填,無請設為空字串
class_sendMail($subject,$html,$user,'',$recipient);

評等結果
點擊便能為這篇文章進行評等!
[評等總次數: 0,平均評等: 0]
!去下營經續永站本助幫能就下一點輕輕,話的您到助幫有章文的我果如 如果我的文章有幫助到您的話,輕輕點一下就能幫助本站永續經營下去!

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *