如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
PHP需开启php_sockets 和 php_fileinfo
1.下载phpmailer
https://sourceforge.net/projects/phpmailer/files/
把下载的phpmailer放到vendor中,只保留以下几个文件即可,
2.打开composer.json 按如下修改
3.分别修改PHPmailer文件夹下文件的命名空间,修改为
namespace ven\PHPMailer;
4.写公共方法common.php, 根据注释修改成自己的配置项
/** * 发送邮件 * @param $toemail 收件人email * @param $subject 邮件主题 * @param $message 正文 * @param $from 发件人 * @param $cfg 邮件配置信息 * @param $sitename 邮件站点名称 */ function send_email($toemail, $subject, $message, $from = '', $cfg = array(), $sitename = '') { //判断openssl是否开启 $openssl_funcs = get_extension_funcs('openssl'); if (!$openssl_funcs) { return array('status' => -1, 'msg' => '请先开启openssl扩展'); } //表单提交 测试发送 /*if ($cfg && is_array($cfg)) { $from = $cfg['from']; $email = $cfg; } else { $config = cache('Config'); }*/ // $config = cache('Config'); $mail = new ven\PHPMailer\PHPMailer(); //Server settings // var_dump($mail);die; $mail->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码 $mail->SMTPDebug = 0; // Enable verbose debug output $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = 'smtp.qq.com'; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = '自己的发送账号'; // SMTP username $mail->Password = '填自己的,注意不是qq密码'; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted $mail->Port = 25; //端口 - likely to be 25, 465 or 587 //Recipients $mail->setFrom('287851074@qq.com', 'Mailer'); //发送方地址和昵称 $mail->addAddress($toemail, 'Joe User'); // Add a recipient //$mail->addReplyTo('info@example.com', 'Information'); //回复地址 //Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = $subject; //标题 $mail->Body = $message; //内容 //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; if (!$mail->send()) { return array('status' => -1, 'msg' => '发送失败: ' . $mail->ErrorInfo); } else { return array('status' => 1, 'msg' => '发送成功'); } }
5.打开命令行,cd到自己的项目目录
composer dump-autoload
6.写方法
public function email() { // send_email($toemail, $subject, $message, $from = '', $cfg = array(), $sitename = '') send_email('34689347@qq.com', '我是主题', '<a href="http://www.baidu.com/">我是百度,哈哈</a>', $from = '发件人', $cfg = array(), $sitename = '站点名称'); }
7.ok,成功发送邮件
补充
$site = $_SERVER['SERVER_NAME']; // if($r){ $url_r = "<a href='http://".$site."/index.php/default/e_regist.html?email=".$data['email']."&code=".$code."'>http://".$site."/index.php/default/e_regist.html?email={$data['email']}&code={$code}</a>"; $mail_to = $data['email']; $mail_subject = "注册账号"; $mail_body = " <pre> 你正在执行找回密码操作! 请点击 {$url_r}链接完成激活。 如果您没有在找回密码,你可以安全地忽略本邮件。 </pre>"; $res = Email::send_email($mail_to, $mail_subject, $mail_body, $from = '发件人', $cfg = array(), $sitename = '注册账号'); $this->jssuccess('验证信息已发送到你的邮箱,请验证后登陆');