phpmailer实现发送邮件功能

发送邮件功能很多地方是需要用的,比如注册成功为用户发送邮件。这里只是简单的为大家介绍一下phpmailer的使用方法,php一般用这个插件进行邮件发送,掌握好后可以运用到实践项目中,当然一般发送邮件需要使用到消息队列,这里先不讲。

1.搭建简单的项目

在服务器上创建一个项目目录sendemail,里面写两个文件,一个是index.html,另一个是index.php.

图片.png

2.下载phpmailer

php离不开composer,我们用composer下载phpmailer。首先在浏览器上输入网址 https://packagist.org/ ,然后搜索phpmailer.如下图,第二个就是了。

图片.png

点进去,在命令行先进入到项目目录,执行这条语句。就可以下载phpmailer.
图片.png

执行命令如下所示。

~ % cd /usr/local/var/www/sendemail
sendemail % composer require phpmailer/phpmailer
Using version ^6.1 for phpmailer/phpmailer
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing phpmailer/phpmailer (v6.1.7): Loading from cache
phpmailer/phpmailer suggests installing psr/log (For optional PSR-3 debug logging)
phpmailer/phpmailer suggests installing league/oauth2-google (Needed for Google XOAUTH2 authentication)
phpmailer/phpmailer suggests installing hayageek/oauth2-yahoo (Needed for Yahoo XOAUTH2 authentication)
phpmailer/phpmailer suggests installing stevenmaguire/oauth2-microsoft (Needed for Microsoft XOAUTH2 authentication)
phpmailer/phpmailer suggests installing symfony/polyfill-mbstring (To support UTF-8 if the Mbstring PHP extension is not enabled (^1.2))
Writing lock file
Generating autoload files
1 package you are using is looking for funding.
Use the `composer fund` command to find out more!
sendemail % ls -l
total 32
-rw-r--r--  1 lijiwei  staff    65 Sep 16 11:36 composer.json
-rw-r--r--  1 lijiwei  staff  3257 Sep 16 11:37 composer.lock
-rw-r--r--  1 lijiwei  staff   480 Sep 16 11:15 index.html
-rw-r--r--  1 lijiwei  staff    33 Sep 16 11:16 index.php
drwxr-xr-x  5 lijiwei  staff   160 Sep 16 11:37 vendor

3.开启邮箱的IMAP/SMTP服务

图片.png

点击开启,需要用手机号发送短信到指定号码。


图片.png

发送之后点击我以发送,就会看到授权吗,授权码要记得。


图片.png

4.上代码

首先packagist有模版案例,可以直接copy过来,然后进行参数修改。


图片.png

首先写个简单的前台页面,index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <form action='index.php' method="post">
        <label>标题:</label><input type="text" name="title" ><br/>
        <label>内容:</label><input type="text" name="content" ><br/>
        <label>邮箱地址:</label><input type="email" name="email"><br/>
        <input type="submit" value="发送">
    </form>
</body>
</html>

然后写实现代码index.php

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

maileto($_POST['email'],$_POST['title'],$_POST['content']);
/**
 * 邮件发送函数
 * @param  [type] $to      [发送给谁(邮箱)]
 * @param  [type] $title   [邮箱标题]
 * @param  [type] $content [邮箱内容]
 * @return [type]          [description]
 */
function maileto($to,$title,$content)
{
    $mail = new PHPMailer(true);
    try {
        //Server settings
        $mail->SMTPDebug = 0; // 是否开启smtp的debug进行调试 ,0关闭,1开启
        $mail->isSMTP(); // 启用SMTP
        $mail->CharSet='utf-8'; //设置字符编码
        $mail->Host       = 'smtp.qq.com';// SMTP服务器,这里是qq邮箱
        $mail->SMTPAuth   = true;// 启用SMTP认证
        $mail->Username   = '2974798592@qq.com';// 发送邮件的邮箱,即自己的邮箱
        $mail->Password   = 'fqamafmrsdiodcea';// 授权码
        $mail->SMTPSecure = 'ssl';// 加密方式为tls或者ssl,根据需求自己改
        $mail->Port       = 465;// 端口号
        //Recipients
        $mail->setFrom('2974798592@qq.com', '白大侠'); //从哪发送的邮件,和上面的$mail->Username一样,后面是发送者的昵称,可以改
        $mail->addAddress($to);// 增加一个接受者的邮箱,这里用变量
        //$mail->addAddress('ellen@example.com');// 可以增加多个
        // $mail->addReplyTo('info@example.com', 'Information');
        // $mail->addCC('cc@example.com');   //抄送
        // $mail->addBCC('bcc@example.com'); //密送
        // 附件
        // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
        // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
        // Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = $title;//标题
        $mail->Body    = $content;//正文
        //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';////当邮件不支持html时备用显示,可省略 
        $mail->send();//发送邮件
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }
}

5.效果

图片.png

图片.png

还挺有趣的呢,感兴趣的可以了解了解。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容