使用PHPMailer發送Gmail賬號郵件

詳細代碼如下
 
 
Php代碼 
//下面定義一個發送郵件的函數,已經測試通過。   
//$sendto_email:郵件發送地址   
//$subject:郵件主題   
//$body:郵件正文內容   
//$sendto_name郵件接受方的姓名,發送方起的名字。一般可省。  
function stmp_mail($sendto_email, $subject = null, $body = null, $sendto_name = null) { 
     
    vendor ( "PHPMailer.class#phpmailer" ); //導入函數包的類class.phpmailer.php  
     
 
    $mail = new PHPMailer (); //新建一個郵件發送類對象   
    $mail->IsSMTP (); // send via SMTP  
    $mail->Port = 25; //發送端口  
    $mail->Host = "ssl://smtp.gmail.com:465"; // SMTP 郵件伺服器地址,這裡需要替換為發送郵件的郵箱所在的郵件伺服器地址, 這裡使用瞭gmail的SMTP設置 
    $mail->SMTPAuth = true; // turn on SMTP authentication 郵件伺服器驗證開   
    $mail->Username = "leobrilliantlife@gmail.com"; // SMTP伺服器上此郵箱的用戶名,有的隻需要@前面的部分,有的需要全名。請替換為正確的郵箱用戶名   
    $mail->Password = "****"; // SMTP伺服器上該郵箱的密碼,請替換為正確的密碼   
    $mail->From = "leobrilliantlife@gmail.com"; // SMTP伺服器上發送此郵件的郵箱,請替換為正確的郵箱,$mail->Username 的值是對應的。                  
    $mail->FromName = "掌上順德"; // 真實發件人的姓名等信息,這裡根據需要填寫   
    $mail->CharSet = "utf-8"; // 這裡指定字符集!   
    $mail->Encoding = "base64"; 
    $mail->AddAddress ( $sendto_email, $sendto_name ); // 收件人郵箱和姓名   
    //$mail->AddReplyTo('sdaping@mail.ustc.edu.cn',"管理員");//這一項根據需要而設   
    //$mail->WordWrap = 50; // set word wrap   
    //$mail->AddAttachment("/var/tmp/file.tar.gz"); // 附件處理   
    //$mail->AddAttachment("/tmp/image.jpg", "new.jpg");   
    $mail->IsHTML ( true ); // send as HTML 
    $mail->Subject = $subject; // 郵件主題  
    // 郵件內容 
    $mail->Body = "<html><head>   
            <meta http-equiv=”Content-Language” content=”zh-cn”>   
            <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″></head>   
            <body>'.$body.'</body></html>"; 
    $mail->AltBody = "text/html"; 
    if (! $mail->Send ()) { 
        //郵件發送失敗 
        return false; 
    } else { 
        //郵件發送成功 
        return true; 
    } 
 
} //function end

作者“butter”
 

發佈留言

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