Home Daily Programming PHP Knowledge PHP implements sending emails (4)

PHP implements sending emails (4)

Jan 07, 2019 pm 03:53 PM


In the previous article "PHP Implementation of Sending Emails (3)", we gave you a brief introduction to the method library through PHPMailer To achieve the functional effect of sending emails. Next, we will continue to explain how to implement the specific function of sending emails through PHPMailer based on the content of the previous article.

PHP implements sending emails (4)

email.php code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

<?php

 

/*发送邮件方法

 *@param $to:接收者 $title:标题 $content:邮件内容

 *@return bool true:发送成功 false:发送失败

 */

function sendMail($to,$title,$content) {

    // 这个PHPMailer 就是之前从 Github上下载下来的那个项目

    require &#39;./PHPMailer/PHPMailerAutoload.php&#39;;

 

    $mail = new PHPMailer;

    //使用smtp鉴权方式发送邮件

    $mail->isSMTP();

    //smtp需要鉴权 这个必须是true

    $mail->SMTPAuth = true;

    // qq 邮箱的 smtp服务器地址,这里当然也可以写其他的 smtp服务器地址

    $mail->Host = &#39;smtp.qq.com&#39;;

    //smtp登录的账号 这里填入字符串格式的qq号即可

    $mail->Username = &#39;244103592@qq.com&#39;;

    // 这个就是之前得到的授权码,一共16位

    $mail->Password = &#39;hlclkdigsqqdbged&#39;;

    $mail->setFrom(&#39;244103592@qq.com&#39;, &#39;send_user_name&#39;);

    // $to 为收件人的邮箱地址,如果想一次性发送向多个邮箱地址,则只需要将下面这个方法多次调用即可

    $mail->addAddress($to);

    // 该邮件的主题

    $mail->Subject = $title;

    // 该邮件的正文内容

    $mail->Body = $content;

 

    // 使用 send() 方法发送邮件

    if(!$mail->send()) {

        return &#39;发送失败: &#39; . $mail->ErrorInfo;

    } else {

        return "发送成功";

    }

}

 

// 调用发送方法,并在页面上输出发送邮件的状态

var_dump(sendMail(&#39;2286445505@qq.com&#39;,&#39;会议主题&#39;,&#39;今天下午开会&#39;));

Copy after login

PHPMailer method library download address: http://www.php.cn/ xiazai/learn/5627

In the above codesendMail method, after introducing PHPMailerAutoload.php and instantiating PHPMailer, Related function methods can be called.

$mail->isSMTP() means using SMTP service;

$mail->SMTPAuth sets whether to use authentication ( Must be true);

$mail->HostSet the sender’s SMTP server address;

##$mail-> ;UsernameSet the sender's qq mailbox username;

$mail->Password Set the sender's mailbox password. Note that when using qq mailbox, fill in the "authorization password" "Instead of the email login password! (For the method of obtaining the qq authorization code, please refer to the article "PHP Implementation of Sending Mail (1)")

$mail->setFrom() is used to set the sender Information, such as the sender in the email format description, will be displayed here as send_user_name (xxxx@qq.com), and send_user_name is displayed as the name.

$mail->addAddress() Used to set the recipient’s email address.

$mail->Subject Set the email title;

$mail->Body Set the email body.

Finally use the send() method to send the email and determine whether the email is sent successfully. If it fails, the error message $mail->ErrorInfo will be output.

That’s it for the introduction of how to implement the email sending function in PHP. It’s also very simple and easy to understand. I hope it will be helpful to friends in need!



The above is the detailed content of PHP implements sending emails (4). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)