PHP发送邮件

xiaoxiao2021-02-28  119

PHP 使用 swiftmailer 插件发送邮件

文件介绍

swiftmailer 邮箱插件data.sql 新增sql表文件pwd.php qq邮箱授权码config.php 连接数据库配置文件PdoMySQL.class.php 封装PDO操作类doaction.php 注册发送邮件和登录操作文件

这里以qq邮箱为例:

1.到邮箱设置中开启IMAP/SMTP服务

2.然后在下面点击“生成授权码”,通过发送短信然后获取授权码

3.发送邮件服务器:smtp.qq.com,使用SSL,端口号465 // 在这里即这样写,注意:ssl好像只能小写 $transport = Swift_SmtpTransport::newInstance(‘smtp.qq.com’, 465, ‘ssl’);

4.然后设置账户名和密码 transport>setUsername(qq); transport->setPassword(‘第2步生成的授权码’);

附上 doaction 中的一段源码

// 如果数据插入成功 if ($res) { // 发送邮件,以QQ邮箱为例 // 1.配置邮件服务器,得到传输对象 $transport = Swift_SmtpTransport::newInstance('smtp.qq.com', 465, 'ssl'); // 2.设置登陆账号和密码 $transport->setUsername('1158862801@qq.com'); $transport->setPassword($emailPassword); // 3.得到发送邮件对象Swift_Mailer对象 $mailer = Swift_Mailer::newInstance($transport); // 4.得到邮件信息对象 $message = Swift_Message::newInstance(); // 5.设置邮件 // 设置发件人(管理员)的信息 $message->setFrom(array('1158862801@qq.com' => 'gzh')); // 设置接收人的信息 $message->setTo(array($email => 'PDO')); // 设置邮件主题 $message->setSubject('激活邮件'); // 设置邮件内容 // $token $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?act=active&token={$token}"; $urlencode = urlencode($url); $str = "亲爱的{$username}您好~!感谢您注册我们的网站。<br/> 请点击此链接激活账号即可登陆!<br/> <a href="{$url}">{$urlencode}</a> 如果点此链接无反应,可以将其复制到浏览器中来执行,链接有效时间24小时。" $message->setBody("{$str}", 'text/html', 'utf-8'); // 6.尝试发送邮件 try { if ($mailer->send($message)) { echo "恭喜您{$username}注册成功,请到邮箱激活之后登陆<br/>"; echo '3秒钟之后跳转到登陆页面'; echo '<meta http-equiv="refresh" content="3;url=index.php" />' ; } else { $PdoMySQL->delete($table, 'id='.$lastInsertId); echo '用户注册失败,请重新注册, 3秒钟后跳转到注册页面'; echo '<meta http-equiv="refresh" content="3;url=index.php" />' ; } } catch (Swift_ConnectionException $e) { echo '邮件发送错误'.$e->getMessage(); }

资源

swiftmailer 下载地址:https://github.com/swiftmailer/swiftmailer源码下载 下载仓库:https://github.com/gaozh1995/email-activate
转载请注明原文地址: https://www.6miu.com/read-65187.html

最新回复(0)