• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

phpmailer使用php发邮件案例解析

php 搞代码 3年前 (2022-01-22) 28次浏览 已收录 0个评论

这次给大家带来phpmailer使用php发邮件案例解析,phpmailer使用php发邮件的注意事项有哪些,下面就是实战案例,一起来看一下。

SMTP 服务器认证密码,需要妥善保管(PS:密码直接没有空格)

第七步:PHP发送邮件

基本代码

下面的代码演示了 PHPMailer 的使用方法,注意 PHPMailer 实例的配置过程。

// 引入PHPMailer的核心文件require_once("PHPMailer/class.phpmailer.php");require_once("PHPMailer/class.smtp.php"); // 实例化PHPMailer核心类$mail = new PHPMailer();// 是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可 默认关闭debug调试模式$mail->SMTPDebug = 1;// 使用smtp鉴权方式发送邮件$mail->isSMTP();// smtp需要鉴权 这个必须是true$mail->SMTPAuth = true;// 链接qq域名邮箱的服务器地址$mail->Host = &#<b>%本文@来源gao@!dai!ma.com搞$$代^@码!网</b><strong>搞代gaodaima码</strong>39;smtp.qq.com';// 设置使用ssl加密方式登录鉴权$mail->SMTPSecure = 'ssl';// 设置ssl连接smtp服务器的远程服务器端口号$mail->Port = 465;// 设置发送的邮件的编码$mail->CharSet = 'UTF-8';// 设置发件人昵称 显示在收件人邮件的发件人邮箱地址前的发件人姓名$mail->FromName = '发件人昵称';// smtp登录的账号 QQ邮箱即可$mail->Username = '[email protected]';// smtp登录的密码 使用生成的授权码$mail->Password = '**********';// 设置发件人邮箱地址 同登录账号$mail->From = '[email protected]';// 邮件正文是否为html编码 注意此处是一个方法$mail->isHTML(true);// 设置收件人邮箱地址$mail->addAddress('[email protected]');// 添加多个收件人 则多次调用方法即可$mail->addAddress('[email protected]');// 添加该邮件的主题$mail->Subject = '邮件主题';// 添加邮件正文$mail->Body = '<h1>Hello World</h1>';// 为该邮件添加附件$mail->addAttachment('./example.pdf');// 发送邮件 返回状态$status = $mail->send(); 

我在thinkphp5.0中使用代码

/*** 邮件发送* @param $to 接收人* @param string $subject 邮件标题* @param string $content 邮件内容(html模板渲染后的内容)* @throws Exception* @throws phpmailerException*/function send_email($to,$subject='',$content=''){  vendor('phpmailer.PHPMailerAutoload');//require_once 'vendor/phpmailer/PHPMailerAutoload.php';  $mail = new PHPMailer;  $arr = db('config')->where('inc_type','smtp')->select();  $config = convert_arr_kv($arr,'name','value');  $mail->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码  $mail->isSMTP();//Enable SMTP debugging// 0 = off (for production use)// 1 = client messages// 2 = client and server messages  $mail->SMTPDebug = 0;//调试输出格式//$mail->Debugoutput = 'html';//smtp服务器  $mail->Host = $config['smtp_server'];//端口 - likely to be 25, 465 or 587  $mail->Port = $config['smtp_port'];  if($mail->Port === 465) $mail->SMTPSecure = 'ssl';// 使用安全协议//Whether to use SMTP authentication  $mail->SMTPAuth = true;//发送邮箱  $mail->Username = $config['smtp_user'];//密码  $mail->Password = $config['smtp_pwd'];//Set who the message is to be sent from  $mail->setFrom($config['smtp_user'],$config['email_id']);//回复地址//$mail->addReplyTo('[email protected]', 'First Last');//接收邮件方  if(is_array($to)){    foreach ($to as $v){      $mail->addAddress($v);    }  }else{    $mail->addAddress($to);  }  $mail->isHTML(true);// send as HTML//标题  $mail->Subject = $subject;//HTML内容转换  $mail->msgHTML($content);//Replace the plain text body with one created manually//$mail->AltBody = 'This is a plain-text message body';//添加附件//$mail->addAttachment('images/phpmailer_mini.png');//send the message, check for errors  return $mail->send();}

相信看了本文案例你已经掌握了方法,更多精彩请关注搞代码其它相关文章!

推荐阅读:

PHP冒泡排序使用详解

PHP实现二叉树深度与广度优先遍历算法步骤详解


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:phpmailer使用php发邮件案例解析
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址