星期五, 12月 16, 2005

PHPMailer


不想用內建的mail()發信嗎?
這裡推薦一個不錯的發信程式, 有smtp, sendmail, mail 3種可選擇.
簡單的使用方法如下:

//引入phpmailer
require_once("class.phpmailer.php");


//初始化
$phpmail = new PHPMailer();

$phpmail->From = "user@domain.com";
$phpmail->FromName = "webmaster";
$phpmail->Host = ""smtp.domain.com;

//寄送方法(smtp, sendmail, mail)
$phpmail->Mailer = "smtp";
$phpmail->SMTPAuth = true;
$phpmail->Username = "smtp_user";
$phpmail->Password = "smtp_pwd";

//收件人
$phpmail->AddAddress("friend@hisdomain.com");
$phpmail->Subject = "測試";
$phpmail->Body = $body;
$phpmail->CharSet = "big5";
$phpmail->IsHTML(true);

//加入附檔
$phpmail->AddAttachment("c:/temp/11-10-00.zip", "new_name.zip");

if ( !$phpmail->Send() ) {
echo $phpmail->ErrorInfo;

}


官方網站 http://phpmailer.sourceforge.net/

利用 gmail 當 smtp 寄送郵件設定方式

 $phpmail->From = $mail_from;
    $phpmail->FromName = $mail_from_name;
    $phpmail->Host = $smtp_server;
    $phpmail->Mailer = $mailer;
    if (strpos($to_email, ",") === false) {
        $phpmail->AddAddress($to_email);
    } else {
        $email = explode(",", $to_email);
        for($i=0; $i<count($email); $i++) {
            $phpmail->AddBcc($email[$i]);
        }
    }
    $phpmail->Subject = $subject;
    $phpmail->Body = $sendata;
    $phpmail->CharSet = $mail_charset;
    $phpmail->IsHTML($is_html);
    $phpmail->Encoding = "base64";
    $phpmail->addEmbeddedImage($cardimagepath."/".$image_name, 'my-photo');
    if ($smtp_auth === true) {
        $phpmail->SMTPAuth = true;
        $phpmail->SMTPSecure = $smtp_secure; //gmail SMTP using SSL
        $phpmail->Port = $smtp_port; //gmail using 465 port
        $phpmail->Username = $smtp_username;
        $phpmail->Password = $smtp_password;
    }

    if (!$phpmail->Send()) {
        echo $phpmail->ErrorInfo;
        die();
    }

沒有留言: