PHP使用gmail發郵件

來源:互聯網
上載者:User

實驗室要舉辦一個會議,要我做一個會議的網站。使用了開源的openconf,十分省事。但發郵件簡單的使用了PHP的mail函數,沒有經過SMTP認證,發送的郵件很可能出現在對方的垃圾箱裡,甚至會被退信。搜尋了一下有很多PHP類庫支援利用SMTP發送郵件,比如PHPMailer。

下面是利用PHPMailer實現的一個函數以及簡單的測試代碼。【注意,運行此函數需要openconf原始碼根目錄下的class.phpmailer.phpclass.smtp.php

請將“yourgmail@gmail.com”替換成你的gmail,寫上對應密碼。將“test@qq.com”改為要你要測試的郵箱。

<?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      require_once("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 = "yourgmail@gmail.com"; // SMTP伺服器上此郵箱的使用者名稱,有的只需要@前面的部分,有的需要全名。請替換為正確的信箱使用者名       $mail->Password = "******"; // SMTP伺服器上該郵箱的密碼,請替換為正確的密碼       $mail->From = "yourgmail@gmail.com"; // SMTP伺服器上發送此郵件的郵箱,請替換為正確的郵箱,$mail->Username 的值是對應的。                      $mail->FromName = "yourName"; // 真實寄件者的姓名等資訊,這雷根據需要填寫       $mail->CharSet = "utf-8"; // 這裡指定字元集。       //$mail->Encoding = "base64";     $mail->AddAddress ( $sendto_email, $sendto_name ); // 收件者郵箱和姓名       //$mail->AddReplyTo('yourgmail@gmail.com',"管理員");//這一項根據需要而設       //$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";      */    $mail->Body = $body;    if (! $mail->Send ()) {         //郵件發送失敗         return false;     } else {         //郵件發送成功         return true;     } } //function end?>
將上面代碼存為gmail.php,編寫下面的main.php
<?phprequire_once "gmail.php";echo "send mail begin";echo "    ";if(stmp_mail("your@qq.com","subject","body"))    echo "success";else    echo "failed";echo "    ";echo "send mail end";?>

使用瀏覽器訪問mail.php測試。


______________UPDATED 2013.04.27___________________________________

下面市openconf需要做的更改:

將上述檔案gmail.php放在opencof根目錄 修改include.php 在<。php 下面一行添加

require_once "gmail.php";  
替換函數oc_mail:
function oc_mail($to, $subject, $body, $hdr='', $enc='quoted-printable') {        global $OC_configAR;        $headers = (empty($hdr) ? $OC_configAR['OC_mailHeaders'] : $hdr);        $headers = "MIME-Version: 1.0\r\n" . $headers . "\r\nContent-Type: text/plain; charset=UTF-8\r\nContent-Transfer-Encoding: " . $enc;        $headers = preg_replace("/\r/", "", $headers);    return (stmp_mail($to, $subject, $body));}    


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.