ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## 一.使用以前下载的phpmailer.zip Sdk包(比较麻烦) 1. extend目录下创建一个phpmailer文件夹 ```linux cd extend mkdir phpmailer ``` 2. 解压phpmailer.zip,把里面的文件拷贝或移动到extend/phpmailer目录下 ```linux tar -zxvf phpmailer.zip mv -r phpmailer extend ``` 3. 修改类名 class.phpmailer.php 改为PHPMailer class.smtp.php改为SMTP 4. 改命名空间 PHPMailer.php、SMTP.php中添加 namespace phpmailer; 并且在PHPMailer.php中,use phpmailer\SMTP; 同时把底下的phpmailerException继承的Exception前添加 \ 5.创建一个SendEmail.php类,封装具体的发送邮件方法 ```php <?php namespace phpmailer; class SendEmail { //发送邮件方法 $to发送给谁, $title邮件标题 , $content邮件内容 //同时把相关的配置信息保存在application/extra/email.php中 public static function send($to, $title, $content) { if (empty($to)) { return false; } date_default_timezone_set('PRC');//set time $mail = new PHPMailer; $mail->isSMTP(); //$mail->SMTPDebug = 2; //Ask for HTML-friendly debug output $mail->Debugoutput = 'html'; //Set the hostname of the mail server $mail->Host = config('mail.host'); //Set the SMTP port number - likely to be 25, 465 or 587 $mail->Port = config('mail.port'); //Whether to use SMTP authentication $mail->SMTPAuth = true; //Username to use for SMTP authentication $mail->Username = config('mail.username'); //Password to use for SMTP authentication $mail->Password = config('mail.password'); //Set who the message is to be sent from $mail->setFrom(config('mail.username'), config('mail.nickname')); $mail->addAddress($to); $mail->Subject = $title; $mail->msgHTML($content); //send the message, check for errors if (!$mail->send()) { return false; } else { return true; } } } ``` application/extra/email.php文件 ```php <?php return [ //qq邮箱配置 'host' => 'smtp.qq.com', //qq邮箱smtp发送服务器地址 'port' => 587, //qq邮箱smtp发送端口号 'username' => '*********@qq.com', //配置邮箱的发送者 'password' => '密钥', //smtp下的密钥 'nickname' => '阳光男孩' //发送者昵称 ]; ``` 详细使用可以参考菜鸟教程下的phpmailer使用方法:[地址]([https://www.runoob.com/w3cnote/php-phpmailer.html](https://www.runoob.com/w3cnote/php-phpmailer.html)) 6. 控制器中发送邮件 ```php ... public function send() { $res = \phpmailer\Email::send('57******35@qq.com','测试邮件标题','这是一封测试专用邮件,请忽略'); if ($res) { return json(['code'=>200,'msg'=>"发送成功"]); } } ``` ## 使用phpmailer提供的composer包 1. 使用composer下载phpmailer ```php composer require phpmailer/phpmailer ``` 2. 在应用公共函数common.php写个发送邮件的函数(腾讯邮箱的为例),需要注意的是实例化PHPMailer的时候。另外可以在application/extra下单独配置一个email.php文件存放配置信息 ```php /** * 发送邮件 * @param string $tomail 接收邮件者邮箱 * @param string $name 接收邮件者名称 * @param string $subject 邮件主题 * @param string $body 邮件内容 * @param string $attachment 附件列表 * @return boolean * @throws phpmailerException */ function send_mail($tomail, $name, $subject = '', $body = '', $attachment = null) { $mail = new PHPMailer\PHPMailer\PHPMailer(); //实例化PHPMailer对象(注意这里) $mail->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码 $mail->IsSMTP(); // 设定使用SMTP服务 $mail->SMTPDebug = 0; // SMTP调试功能 0=关闭 1 = 错误和消息 2 = 消息 $mail->SMTPAuth = true; // 启用 SMTP 验证功能 $mail->SMTPSecure = 'ssl'; // 使用安全协议 $mail->Host = "smtp.qq.com"; // SMTP 服务器 $mail->Port = 465; // SMTP服务器的端口号 $mail->Username = "XXXXX@qq.com"; // SMTP服务器用户名 $mail->Password = "XXXXX"; // SMTP服务器密码,这里是你开启SMTP服务时生成密码 $mail->SetFrom('XXXXX@qq.com', '发件人昵称'); $replyEmail = ''; //留空则为发件人EMAIL $replyName = ''; //回复名称(留空则为发件人名称) $mail->AddReplyTo($replyEmail, $replyName); $mail->Subject = $subject; $mail->MsgHTML($body); $mail->AddAddress($tomail, $name); if (is_array($attachment)) { // 添加附件 foreach ($attachment as $file) { is_file($file) && $mail->AddAttachment($file); } } return $mail->Send() ? true : $mail->ErrorInfo; } ``` 3. 在相对应的控制器中调用前面设置的send_mail方法 ```php ... public function send_email() { $toemail='57******35@qq.com';//收件人邮箱 $name='亲爱的57******35@qq.com,您好!'; //接收邮件者名称 $subject='邮件标题'; //邮件主题 $content='恭喜你,邮件测试成功。'; //邮件内容 //调用方法发送邮件 dump(send_mail($toemail,$name,$subject,$content)); } ```