php发送邮件-亲测成功案例

文章目录

php发送邮件-亲测成功案例

php发送邮件-亲测成功案例

我的环境
服务器:iis
php:php7.2.5
操作系统:windows

操作步骤

  1. 先准备好线上能php的iis服务器

    1. php发送邮件-亲测成功案例
  2. 准备好QQ邮箱

  3. 开户这个,截图错了php发送邮件-亲测成功案例

  4. 准备好sendMail.zip解压到某个目录(window发送邮箱需要这个exe程序)
    1. sendMail解压后的sendMail.ini需要配置成如下内容

; configuration for fake sendmail

; if this file doesn't exist, sendmail.exe will look for the settings in
; the registry, under HKLM\Software\Sendmail

[sendmail]

; you must change mail.mydomain.com to your smtp server,
; or to IIS's "pickup" directory.  (generally C:\Inetpub\mailroot\Pickup)
; emails delivered via IIS's pickup directory cause sendmail to
; run quicker, but you won't get error messages back to the calling
; application.

smtp_server=smtp.qq.com

; smtp port (normally 25)

smtp_port=25

; SMTPS (SSL) support
;   auto = use SSL for port 465, otherwise try to use TLS
;   ssl  = alway use SSL
;   tls  = always use TLS
;   none = never try to use SSL

smtp_ssl=auto

; the default domain for this server will be read from the registry
; this will be appended to email addresses when one isn't provided
; if you want to override the value in the registry, uncomment and modify

;default_domain=mydomain.com

; log smtp errors to error.log (defaults to same directory as sendmail.exe)
; uncomment to enable logging

error_logfile=error.log

; create debug log as debug.log (defaults to same directory as sendmail.exe)
; uncomment to enable debugging

;debug_logfile=debug.log

; if your smtp server requires authentication, modify the following two lines

auth_username=25472690xx@qq.com#必填的,发邮箱的邮箱
auth_password=uwjdbfilqildexxx#必填的,发邮箱的授权码

; if your smtp server uses pop3 before smtp authentication, modify the 
; following three lines.  do not enable unless it is required.

pop3_server=
pop3_username=
pop3_password=

; force the sender to always be the following email address
; this will only affect the "MAIL FROM" command, it won't modify 
; the "From: " header of the message content

force_sender=

; force the sender to always be the following email address
; this will only affect the "RCTP TO" command, it won't modify 
; the "To: " header of the message content

force_recipient=

; sendmail will use your hostname and your default_domain in the ehlo/helo
; smtp greeting.  you can manually set the ehlo/helo name if required

hostname=


  1. 在php文件中编写脚本phpinfo()
    1. 查看php.ini所在目录
    2. ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210205143743173.png)
    		1. 如果没有此文件,则就在这目录创建此文件
    		2. 文件内容如下
    
extension=php_openssl.dll#开户openssl,否则发送邮件会报Extension missing: openssl
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.qq.com#我这里使用的是qq邮箱服务器
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = 25472690xx@qq.com#发送的邮箱
 
; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path ="F:\Download\sendmail -t -i"#sendmail的exe路径
mail.add_x_header = On

	3. 查看是否开户open_ssl
	4. ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210205143720617.png)
  1. 下载别人写的发送邮箱的工具类,关键就下面这2个(PHPMailer.php,SMTP.php)
  2. 链接
6. 我的代码

```php
<?php
/*
to:  邮件接收地址
subject: 邮件主题
body: 邮件内容
attachpath:附件地址
cc: 邮件抄送地址
bcc: 邮件暗抄送地址
*/
function send_mail($to, $subject="", $body="", $attachpath="", $cc="", $bcc="")
{
    // 对邮件内容进行必要的过滤
    //$body = eregi_replace("[\]",'',$body);

    // 设定时区
    date_default_timezone_set("PRC");

    require_once('PHPMailer.php');
    require_once("SMTP.php");

    // 实例化PHPMailer对象
    $mail = new PHPMailer();

    // 设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置为 UTF-8
    $mail->CharSet ="UTF-8";

    // 设定使用SMTP服务
    $mail->IsSMTP();

    // 启用 SMTP 验证功能
    $mail->SMTPAuth = true;

    // SMTP 安全协议
    $mail->SMTPSecure = 'ssl';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;

    // SMTP 服务器
    $mail->Host = "smtp.qq.com";

    // SMTP服务器的端口号
    $mail->Port = 465;

    // SMTP服务器用户名和密码
    $mail->Username = "25472690xx@qq.com";
    $mail->Password = "uwjdbfilqildexxx";#qq邮箱要开户上图中的功能

    // 设置发件人地址和名称,名称可有可无
    $mail->SetFrom("25472690xx@qq.com", "25472690xx");
    $mail->AddAddress("670392xxx@qq.com");
    // 设置邮件接收地址和名称,第二个参数无所谓。必须用AddAddress添加邮件接收地址。AddReplyTo方法没什么用。
    //$mail->AddReplyTo("xxxxxx@163.com", "xxxxxx");
    /*$mailaddrs = split(",", $to);
    foreach ($mailaddrs as $addres)
    {
        //校验邮箱地址是否合法
        if (filter_var($addres, FILTER_VALIDATE_EMAIL))
        {
            $mail->AddAddress($addres);
        }
    }*/

    /*// 设置邮件抄送地址
    if ($cc != "")
    {
        $ccaddrs = split(",", $cc);
        foreach ($ccaddrs as $ccaddr)
        {
            //校验邮箱地址是否合法
            if (filter_var($ccaddr, FILTER_VALIDATE_EMAIL))
            {
                $mail->addCC($ccaddr);
            }
        }
    }*/

    // 设置邮件暗抄送地址,私密发送,我这个php版本要要报错,所以注释掉
    /*if ($bcc != "")
    {
        $bccaddrs = split(",", $bcc);
        foreach ($bccaddrs as $bccaddr)
        {
            //校验邮箱地址是否合法
            if (filter_var($bccaddr, FILTER_VALIDATE_EMAIL))
            {
                $mail->addBCC($bccaddr);
            }
        }
    }*/

    // 设置邮件主题
    $mail->Subject = $subject;

    // 可选项,向下兼容考虑
    $mail->AltBody = "为了查看该邮件,请切换到支持 HTML 的邮件客户端";

    // 设置邮件内容
    $mail->MsgHTML($body);

    //使用HTML格式发送邮件
    $mail->IsHTML(true);

    // 添加附件,第一个参数是附件地址,第二个参数附件名
    //$mail->AddAttachment("images/phpmailer.gif");
    $mail->AddAttachment($attachpath);

    // 发送邮件
    if(!$mail->Send())
    {
        echo "发送失败:" . $mail->ErrorInfo . PHP_EOL;
    }
    else
    {
        echo "恭喜,邮件发送成功!" . PHP_EOL;
    }
}
$emailAddr = "670392xxx@qq.com";#收件人邮箱
send_mail($emailAddr, "测试邮件", "<h1>使用PHPMailer类发送的邮件。</h1>", "ax.png", "670392xxx@qq.com", "");
phpinfo();
?>
``


### 如果openssl,没有开启记得,记得重启下iss应用程序池及iis服务器
![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210205145454830.png)
![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210205145513656.png)
![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210205145543617.png)

上一篇:JS实现文字替换


下一篇:Implementing Iterators in Java