如何用PHPMailer基于php脚本发送邮件

详细教程请参考官方文档:http://phpmailer.worxware.com/?pg=tutorial

1. 下载PHPMailer并放到你的工程文件夹下,GitHub链接:https://github.com/PHPMailer/PHPMailer

2. 新建一个php文件,写下如下代码,我这里使用gmail的smtp server:

<?php
require("PHPMailerAutoload.php");

$mail = new PHPMailer();

define('GUSER', 'polyu.mail123@gmail.com'); // GMail username
define('GPWD', 'polyu123456'); // GMail password

function smtpmailer($to, $from, $from_name, $subject, $body) { 
    global $error;
    $mail = new PHPMailer();  // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true;  // authentication enabled
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 465; 
    $mail->Username = GUSER;  
    $mail->Password = GPWD;           
    $mail->SetFrom($from, $from_name);
    $mail->Subject = $subject;
    $mail->Body = $body;
    $mail->AddAddress($to);
    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        return false;
    } else {
        $error = 'Message sent!';
        return true;
    }
}

$subject = "[SuitSystem] Retrieve Password Mail";
$body = "Hi ".$username.", 
Please open the following website to reset your password. 
http://".$_SERVER['SERVER_ADDR'].":8000/resetUserPassword.php
This is a system generated message. Please do not reply.";  

if (smtpmailer($email, 'PasswordReset@gmail.com', 'Tracy', $subject, $body)) {
    echo "<script type='text/javascript'>alert('Email has been sent. Please check your mailbox.');history.back(-1); </script>";  
	exit;
}
?>
3.  history.back(-1);在alert对话框弹出后点击确定会跳转回上一个页面,如果没有这句话则页面会停留在当前的php脚本下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值