Home  >  Article  >  Backend Development  >  php用phpmailer发送邮件

php用phpmailer发送邮件

WBOY
WBOYOriginal
2016-06-20 12:54:21812browse

发送邮件

     1.为了保证安全性,需要生成发送到邮件的URL,主要参数(key(key 需要在每次运行是自动生成随机码))   然后将URL发送到邮箱。

     2.解析url:首先根据用户名从数据库中查找出密钥key和过期时间,没有则表示该请求 是伪造的或者过期的,然后验证签名,验证过期时间,都验证通过,就可以修改密码, 密码修改完以后,删除数据库中的记录。

  注:但我并没有用数据库,而是用的redis. 链接上只用一个唯一码,根据唯一码到redis中获取用户的guid,而且是3600秒后连接失效(redis自已处理)。

    $client = redis();      

    $client->setex($linkkey, 7200, $userInfo['guid']);

    要在服务器打开25端口,不然无法发送邮件。

 /**     * 发送邮件方法     *      * @param string $from 来自     * @param string $to 收件人地址     * @param string $subject  邮件标题     * @param string $body 邮件正文     */    public function postmail($from,$to,$subject = '',$body = ''){        error_reporting(E_ALL);        date_default_timezone_set('Asia/Shanghai');//设定时区东八区        vendor('PHPMailer.class#phpmailer');        vendor('PHPMailer.class#smtp');        $mail             = new \PHPMailer();      //new一个PHPMailer对象出来        $body            = eregi_replace("[\]",'',$body); //对邮件内容进行必要的过滤        $mail->CharSet ="UTF-8";                   //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码        $mail->IsSMTP();                           // 设定使用SMTP服务        $mail->Host       = 'smtp.ym.163.com';        // SMTP 服务器//         $mail->SMTPDebug  = 1;                     // 启用SMTP调试 1 = errors  2 =  messages        $mail->SMTPAuth   = true;                  // 服务器需要验证        $mail->Port       = 25;                    // SMTP服务器的端口号        265/255  25                $mail->Username   = '用户名';   // SMTP服务器用户名        $mail->Password   = '密码';           // SMTP服务器密码        $mail->isHTML(true);        $mail->SetFrom('邮箱地址', $from);        $mail->Subject    = $subject;        $mail->MsgHTML($body);        $address = $to;        $mail->AddAddress($address, '');        //         $mail->AddEmbeddedImage("Public/bee/common/images/logo3.jpg", "logoimg","logo.jpg"); //设置邮件中的图片//         $mail->AddAttachment("Public/bee/common/images/logo3.jpg"); // attachment 附件        if(!$mail->Send()) {            echo 'Mailer Error: ' . $mail->ErrorInfo;        } else {            echo "Message sent!恭喜,邮件发送成功!";        }    }


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn