search
HomeDaily ProgrammingPHP KnowledgePHP implements sending emails (3)

PHP implements the method of sending emails, which can be used in website project development, such as user registration verification, activation verification, password retrieval and other functions. Well, in the previous article, through the case of qq mailbox, we have given you a brief introduction to the function and method of sending emails in PHP.

PHP implements sending emails (3)

## Reference article: "

PHP implements sending emails (1)" "PHP implements sending emails (2)"

The following will be combined with relevant code examples to continue to explain the steps of sending emails in PHP.

PHP code is as follows:

<?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;));

In the above code, a

sendMail method is defined, the three parameters of which are $to (receiver), $title (title ), $content (email content).

In this method, first we introduce the file

PHPMailerAutoload.php in PHPMailer. PHPMailer is a function package used to send emails, and PHPMailerAutoload.php is an automatically loaded file, which is convenient and fast.

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

Then PHP implements email sending The function can be achieved through PHPMailerAutoload.php. After introducing PHPMailerAutoload.php, we need to instantiate the PHPMailer class in it. Finally, you can directly call the relevant methods in the PHPMailer class to implement the email sending function.

Note: In PHPMailer, class.phpmailer.php is the main file, which is used to implement specific functions. class.smtp.php is used Files required by smtp.

Due to the length of the article, the knowledge content of this section will be introduced here. In later articles, we will continue to explain to you how to implement the function of sending emails in PHP.

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

Statement
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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.