phpmailer,透過gmail smtp寄信

以下是透過gmail smtp發信的發信,並使用phpmailer:

step1:下載phpmailer

從這下載:http://sourceforge.net/projects/phpmailer/

step2:使用phpmailer
裡面的檔案只有兩個是我們要的
1.class.phpmailer
2.class.smtp
把這兩個檔案放在你要放置的目錄。

step3:寫一支發送email的php程式:

開頭先將剛剛的class.phpmailer.php引入

<?php
require_once("phpmailer/class.phpmailer.php");
class Mail
{
    public static function send_mail($user_email, $user_name, $subject, $body)
    {
        $mail = new PHPMail();
        $mail->IsSMTP();
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = "ssl";
        $mail->Host = "smtp.gmail.com";
        $mail->Port = 465;
        $mail->CharSet = "utf-8";

        $mail->Username = "xxxxxx@xxx.xx";
        $mail->Password = "email password";

        //$mail->AddAttachment("");
    
        $mail->Subject = $subject;
        $mail->From = "xxxxxx@xxx.xxx";
        $mail->FromName = "xxxxxx";
        $mail->Body = $body;
        $mail->IsHTML(true);
        $mail->AddAddress($user_email, $user_name);
        if (!$mail->Send()) {
            echo 'OOPs!! Fail to send email'.$mail->ErrorInfo;
            return $this->noview();
        } else {
        //
        }
    }
}

之後呼叫這個涵式即可。
以上只是個範例,可依自己方式修改。