2011年12月22日 星期四

PHPMailer - PHP email class

Software: PHPMailer - PHP email class |
| Version: 2.0.4 |
| Contact: via sourceforge.net support pages (also www.codeworxtech.com) |
| Info: http://phpmailer.sourceforge.net |
| Support: http://sourceforge.net/projects/phpmailer/ |
| ------------------------------------------------------------------------- |
| Author: Andy Prevost (project admininistrator) |
| Author: Brent R. Matzelle (original founder) |
| Copyright (c) 2004-2007, Andy Prevost. All Rights Reserved. |
| Copyright (c) 2001-2003, Brent R. Matzelle |
| ------------------------------------------------------------------------- |
| License: Distributed under the Lesser General Public License (LGPL) |
| http://www.gnu.org/copyleft/lesser.html |
| This program is distributed in the hope that it will be useful - WITHOUT |
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| FITNESS FOR A PARTICULAR PURPOSE. |
| ------------------------------------------------------------------------- |
| We offer a number of paid services (www.codeworxtech.com): |
| - Web Hosting on highly optimized fast and secure servers |
| - Technology Consulting |
| - Oursourcing (highly qualified programmers and graphic designers)
----------------------------------------------------------------------------|
// 建立 PHPMailer 物件及設定 SMTP 登入資訊
require("../phpMailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "remote.smtp.server"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "me@localhost"; // SMTP username
$mail->Password = "123456"; // SMTP password

$mail->From = "myemail@localhost";
$mail->FromName = "My Name";

// 執行 $mail->AddAddress() 加入收件者,可以多個收件者
$mail->AddAddress("to@email.com","Josh Adams");
$mail->AddAddress("to2@email.com"); // optional name

$mail->AddReplyTo("jyu@aemtechnology.com","AEM");

$mail->WordWrap = 50; // set word wrap

// 執行 $mail->AddAttachment() 加入附件,可以多個附件
$mail->AddAttachment("path_to/file"); // attachment
$mail->AddAttachment("path_to_file2", "INF");

// 電郵內容,以下為發送 HTML 格式的郵件
$mail->IsHTML(true); // send as HTML
$mail->Subject = "testing email";
$mail->Body = "This is the HTML body";
$mail->AltBody = "This is the text-only body";

if(!$mail->Send())
{
echo "Message was not sent 

"; echo "Mailer Error: " . $mail->ErrorInfo; exit; } echo "Message has been sent";



CASE 2 aXXl
$mail             = new PHPMailer(); // defaults to using php "mail()"
$mail->CharSet    = "utf-8";
$mail->From       = 'admin@XX.XX.X.X';
$mail->FromName   = 'AXX';
$subject          = " system email test";
$mail->Subject    = $subject;

$data_user[0]['id']='xx@xx.xxxx';
$data_user[0]['name']='Bau';
$sql="";

$body = "Dear,
please reply to  testing email.";
$data=lazy_get_data($sql);
foreach($data as $value){
$body.=$value['empID'].'gg'.$value['empEmail']."
";
}

//$mail->SingleTo=TRUE; #個人理解,如果cc有多個接收mail只會顯示一個人的
$mail->ClearAddresses(); #由於內文不一樣,$mail->AddAddress 的陣列讓寄信人只寄一個,避免寄給很多人。

// optional, comment out and test
$body = eregi_replace("[\]",'',$body);
$mail->MsgHTML($body);
$mail->Encoding = "base64";

foreach($data_user as $value){

$mail->AddAddress($value['id'], $value['name']);
$mail->IsSMTP();
$mail->SMTPAuth   = false;  
//$mail->Port       = 25;  
//需使用SMTP驗證
//ou$mail->SMTPSecure = "ssl";       
// 利用SSL連線到伺服器

$mail->Host = "mail.global.frontbridge.com";      
//當然啦,信件寄送主機就是GMAIL

//$mail->Port = 465;                   
//指定SMTP port
//$mail->Username = "";  
//MAIL帳號

//$mail->Password = "";
if($debug=='1'){
echo "";
echo $mail->Subject."";
echo $to."[{$to_name}]";
echo $body;

}elseif($debug=='0'){
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
echo $to;
exit;
} else {
//echo "Message sent!";     
sleep(3);     
}

}
}

要注意Gmail 的 SMTP主機固定位址是 smtp.gmail.com,必須使用 465 埠以 SSL 的方式來連線
所以apache需要灌ssl才行。

沒有留言: