search
HomeDaily ProgrammingPHP KnowledgePHP implements sending emails (2)

In the previous article "PHP Implementation of Sending Emails (1)", we introduced how to enable the qq mailbox to open the sending email service (mainly to obtain the authorization code). Next, we will continue to introduce you to the function of sending emails in PHP.

PHP implements sending emails (2)

Complete project source code for PHP to implement sending emails, download address: http://www.php.cn/xiazai/learn/5627

The php code to implement sending emails 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 this code, we define a sendMail method, which is to implement the function of sending emails to the specified object. The three parameters respectively represent a specified mailbox to be sent, the subject of the email sent, and the content of the email sent.

Here we use the example to call this method and send an email to the email address "2286445505@qq.com" with the subject "Meeting Topic" and the email content as "Meeting this afternoon". Then print it through var_dump() to check whether the email is sent successfully.

The results are as follows:

PHP implements sending emails (2)

At this time we can check whether the email "2286445505@qq.com" has received the email "Conference Subject".

PHP implements sending emails (2)

As shown in the picture, 2286445505@qq.com successfully received the email sent.

The above is an introduction to the function of sending emails in PHP, which is also a more practical PHP function in daily project development. So due to the length of the article, the content of this section will be introduced here. In later articles, we will continue to explain in detail the PHP method of realizing the function of sending emails.

The above is the detailed content of PHP implements sending emails (2). 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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),