您好,欢迎访问一九零五行业门户网

php怎么实现发送邮件功能

php怎么实现发送邮件功能?
第一步:打开网址下载phpmailer,phpmailer 需要 php 的 sockets 扩展支持,而登录 qq 邮箱 smtp 服务器则必须通过 ssl 加密的, php 还得包含 openssl 的支持。
第二步:使用 phpinfo() 函数查看 socket 和 openssl 扩展信息(wamp server 默认启用了该扩展)。
openssl 如果没有开启请打开php.ini文件进行开启
首先检查php.ini中;extension=php_openssl.dll是否存在, 如果存在的话去掉前面的注释符‘;', 如果不存在这行,那么添加extension=php_openssl.dll。
phpmailer 核心文件
第三步:qq 邮箱设置
所有的主流邮箱都支持 smtp 协议,但并非所有邮箱都默认开启,您可以在邮箱的设置里面手动开启。
第三方服务在提供了账号和密码之后就可以登录 smtp 服务器,通过它来控制邮件的中转方式。
第四步:开启 smtp 服务
选择 imap/smtp 服务,点击开启服务
第五步:验证密保
发送短信“配置邮件客户端”至1069-0700-69
第六步:获取授权码
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 = 'smtp.qq.com';// 设置使用ssl加密方式登录鉴权$mail->smtpsecure = 'ssl';// 设置ssl连接smtp服务器的远程服务器端口号$mail->port = 465;// 设置发送的邮件的编码$mail->charset = 'utf-8';// 设置发件人昵称 显示在收件人邮件的发件人邮箱地址前的发件人姓名$mail->fromname = '发件人昵称';// smtp登录的账号 qq邮箱即可$mail->username = '12345678@qq.com';// smtp登录的密码 使用生成的授权码$mail->password = '**********';// 设置发件人邮箱地址 同登录账号$mail->from = '12345678@qq.com';// 邮件正文是否为html编码 注意此处是一个方法$mail->ishtml(true);// 设置收件人邮箱地址$mail->addaddress('87654321@qq.com');// 添加多个收件人 则多次调用方法即可$mail->addaddress('87654321@163.com');// 添加该邮件的主题$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('replyto@example.com', '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 errorsreturn $mail->send();}
更多相关知识,请访问!
其它类似信息

推荐信息