PHPmailer 邮件发送组件的使用


PHPmailer 邮件的使用 

下载地址 phpmailer核心文件下载地址 

https://github.com/956077081/PHP_project/tree/master/phpmailer

或者

 http://www.phtstudy.xin/gitvendor.html   
<?php
//header("content-type : text/html;charset= utf-8");
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require './vendor/autoload.php';


$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.163.com';  // Specify main and backup SMTP servers  //选择 163 邮件服务器
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'xxx';                 // SMTP username  发送者  163邮箱的名称
    $mail->Password = 'xxxxxx';                           // SMTP password  //密码
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 25;      //163邮箱 端口见   http://help.163.com/09/1223/14/5R7P3QI100753VB8.html                           // TCP port to connect to
    //Recipients 收件人
    $mail->setFrom('xxx@163.com', 'fromsecretperson');  //发件人邮箱地址 和 名称 
    $mail->addAddress('xxx@126.com', '王子x');     // Add a recipient 接受者 地址和 名称
   // $mail->addAddress('ellen@example.com'); //多人时可再添加               // Name is optional
    $mail->addReplyTo('18706727398@163.com', '发送成功!');  //当发送成功 给此人的信息
   // $mail->addCC('cc@example.com');//添加抄送 指给多人发送时可以看到除了自己外其他接件人的信息地址
    //$mail->addBCC('bcc@example.com');  //给多人发送 接件人 看不到其他接件的信息
    //Attachments
    $mail->addAttachment('./girl.zip',"girljpg");         // Add attachments
   // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
    //Content
    $mail->isHTML(true);   //是否html格式                               // Set email format to HTML
    $mail->Subject = 'Here is the subject';  //邮件主题
    $mail->Body    = 'give you a beautiful girl picture body!</b>'; //邮件内容
    $mail->AltBody = 'give you a beautiful girl picture !'; //当不支持html时的内容.
    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个控件好好用 第一,需要下载PHPMailer文件包phpmailer. http://phpmailer.sourceforge.net/ 第二,确认你的服务器系统已经支持socket ,通过phpinfo();查看是否支持sockets(socket 是属于PHP扩展部分),如果显现为“enabled”,那就是支持了。 第三,把文件解压到你的web服务器目录下,调用类就可以了. 首先包含class.phpmailer.php,然后创建对象,设置参数,调用成员函数。具体请见下面的示例代码: 实例1,做成函数方便调用 <?php require("phpmailer/class.phpmailer.php"); function smtp_mail( $sendto_email, $subject, $body, $extra_hdrs, $user_name){ $mail = new PHPMailer(); $mail->IsSMTP(); // send via SMTP $mail->Host = "200.162.244.66"; // SMTP servers $mail->SMTPAuth = true; // turn on SMTP authentication $mail->Username = "yourmail"; // SMTP username 注意:普通邮件认证不需要加 @域名 $mail->Password = "mailPassword"; // SMTP password $mail->From = "yourmail@yourdomain.com"; // 发件人邮箱 $mail->FromName = "管理员"; // 发件人 $mail->CharSet = "GB2312"; // 这里指定字符集! $mail->Encoding = "base64"; $mail->AddAddress($sendto_email,"username"); // 收件人邮箱和姓名 $mail->AddReplyTo("yourmail@yourdomain.com","yourdomain.com"); //$mail->WordWrap = 50; // set word wrap 换行字数 //$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment 附件 //$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); $mail->IsHTML(true); // send as HTML // 邮件主题 $mail->Subject = $subject; // 邮件内容 $mail->Body = " <html><head> <meta http-equiv="Content-Language" content="zh-cn"> <meta http-equiv="Content-Type" content="text/html; charset=GB2312"> </head> <body> I love php。 </body> </html> "; $mail->AltBody ="text/html"; if(!$mail->Send()) { echo "邮件发送有误 <p>"; echo "邮件错误信息: " . $mail->ErrorInfo; exit; } else { echo "$user_name 邮件发送成功!<br />"; } } // 参数说明(发送到, 邮件主题, 邮件内容, 附加信息, 用户名) smtp_mail("yourmail@yourdomain.com", "欢迎使用phpmailer!", "NULL", "yourdomain.com", "username"); ?>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值