专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 人工智能

施用PHP,PHPMailer和GMail发送电子邮件

发布时间:2011-07-03 08:26:03 文章来源:www.iduyao.cn 采编人员:星星草
使用PHP,PHPMailer和GMail发送电子邮件
简要介绍如何使用PHPMailer通过GMail的SMTP协议发送邮件。

下载PHPMailer
点击 http://phpmailer.sourceforge.net/ 进入PHPMailer在Source Forge的发布页, 或者直接点击 下载。

解压缩并上传
将下载下来的PHPMailer压缩包解开,然后将解开的目录和文件上传到可以使用PHP的web服务器。

发送Gmail的代码样例

    关键部分:
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username@gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password

    完整的样例代码:
function send_by_gmail($to, $subject, $content){
    // send mail using PHPMailer
    require_once("PHPMailer/class.phpmailer.php");

    $mail             = new PHPMailer();

    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
    $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
    $mail->Port       = 465;                   // set the SMTP port for the GMAIL server

    $mail->Username   = "user@gmail.com";  // GMAIL username
    $mail->Password   = "password";            // GMAIL password

    $mail->SetFrom($to, 'username');
    $mail->Subject    = $subject;
    $mail->Body = $content;

    $mail->AddAddress($to, "username");

    if(!$mail->Send()) {
        return array('status'=>true, 'msg'=>"Mailer Error: " . $mail->ErrorInfo);
    } else {
        return array('status'=>false, 'msg'=> "Message sent!");
    }
}


本文镜像: PHP发送Gmail邮件
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: