Home Backend Development PHP Tutorial PHPMailer implements email sending

PHPMailer implements email sending

Jul 28, 2016 am 08:28 AM
gt mail phpmailer smtp

Many times, we need to send emails to our members, such as registration activation, event push, etc.

There are many ways to send emails in PHP, such as the newer Swift Mailer, PHPMailer, etc. Here we mainly introduce the email sending class of PHPMailer.

Download

PHPMailer’s GitHub address: https://github.com/PHPMailer/PHPMailer

implementation

The code is as follows

<code><span><?</span>php 
    <span>//引入类文件</span>
    require_once <span>'PHPMailerAutoload.php'</span>;
    <span>//实例化类</span><span>$mail</span><span>=</span><span>new</span> PHPMailer();
    <span>//是否开始debug模式</span><span>$mail</span><span>-></span>SMTPDebug <span>=</span><span>1</span>;      <span>//这里的数字表示提示错误的的类型</span><span>//使用STMP服务</span><span>$mail</span><span>-></span>isSMTP();
    <span>//声明发送者邮箱SMTP服务器地址</span><span>$mail</span><span>-></span>Host <span>=</span><span>'smtp.163.com'</span>;  <span>//这个是163邮箱SMTP服务器地址</span><span>//开启SMTP认证</span><span>$mail</span><span>-></span>SMTPAuth <span>=</span><span>true</span>;
    <span>//发送邮箱帐号</span><span>$mail</span><span>-></span>Username <span>=</span><span>'admin'</span>;
    <span>//发送邮箱密码</span><span>$mail</span><span>-></span>Password <span>=</span><span>'admin'</span>;     <span>//注意这个密码可能不是平时登入时所用的密码,一般邮箱服务商为提供一个授权码,使用时填写这个授权码</span><span>//加密协议</span><span>$mail</span><span>-></span>STMPSecure <span>=</span><span>'SSL'</span>;
    <span>//端口号</span><span>$mail</span><span>-></span>Port <span>=</span><span>25</span>;

    <span>//发送者邮箱和昵称</span><span>$mail</span><span>-></span>setFrom(<span>'from@163.com'</span>,<span>'Admin'</span>);
    <span>//接受者邮箱和昵称</span><span>$mail</span><span>-></span>addAddress(<span>'to@163.com'</span>,<span>'Customer'</span>);

    <span>//是否开启HTML格式</span><span>$mail</span><span>-></span>isHTML(<span>true</span>);
    <span>//设置邮件的字符编码,不然中文乱码</span><span>$mail</span><span>-></span>CharSet<span>=</span><span>'UTF-8'</span>;  
    <span>//邮件主题</span><span>$mail</span><span>-></span>Subject <span>=</span><span>'subject'</span>;
    <span>//邮件内容</span><span>$mail</span><span>-></span>Body <span>=</span><span>'body'</span>;
    <span>//邮件替代内容</span><span>$mail</span><span>-></span>AltBody <span>=</span><span>'altbody'</span>;        <span>//当邮件不支持html时备用显示,可以省略 </span><span>//邮件附件</span><span>$mail</span><span>-></span>addAttachment(<span>'load.zip'</span>);

    <span>if</span>(<span>!</span><span>$mail</span><span>-></span>send()){
        echo <span>'Message could not be sent'</span>;
        echo <span>'Mail Error:'</span><span>.</span><span>$mail</span><span>-></span>ErrorInfo;
    }<span>else</span>{
        echo <span>'Message has been sent'</span>;
    }
</code>
Copy after login

The blogger has personally tested it and it is effective. You can learn step by step by watching the code

Here I point out a few issues

  • Introduction of class files
    After you download it from git or use composer to download it, you will get a lot of files. You only need to import this file. PHPMailerAutoload.php
  • SMTP server address and port number
  • Generally we need to enable the SMTP service in the email settings.
    NetEase’s email address is as follows:

     PHPMailer实现邮件发送 For other email addresses, you can use Baidu or Google, so I won’t go into details here.
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); }); The above has introduced how to implement email sending with PHPMailer, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

PHP methods and steps for sending emails to multiple people using PHPMailer PHP methods and steps for sending emails to multiple people using PHPMailer May 22, 2023 pm 06:10 PM

In web applications, it is often necessary to send emails to multiple recipients at once. PHP is a very popular web development language, and PHPMailer is a common PHP class library for sending emails. PHPMailer provides a rich interface, making sending emails in PHP applications more convenient and easy to use. In this article, we will introduce the methods and steps on how to use PHPMailer to send emails to multiple recipients. To download PHPMailer, you first need to go to the official website (

PHP development practice: Use PHPMailer to send emails to users in the MySQL database PHP development practice: Use PHPMailer to send emails to users in the MySQL database Aug 05, 2023 pm 06:21 PM

PHP development practice: Use PHPMailer to send emails to users in the MySQL database Introduction: In the construction of the modern Internet, email is an important communication tool. Whether it is user registration, password reset, or order confirmation in e-commerce, sending emails is an essential function. This article will introduce how to use PHPMailer to send emails and save the email information to the user information table in the MySQL database. 1. Install the PHPMailer library PHPMailer is

How to send HTML mail with embedded images using PHP and PHPMAILER? How to send HTML mail with embedded images using PHP and PHPMAILER? Jul 22, 2023 am 11:29 AM

How to send HTML mail with embedded images using PHP and PHPMAILER? HTML email is a richer and more personalized form of email that can insert pictures, links and styles into the email. Embedded images refer to sending images directly as part of the email in the HTML email instead of sending them as attachments. In PHP, we can use PHPMAILER to send HTML emails with embedded images. PHPMAILER is a powerful PHP email sending library

PHP and PHPMAILER: How to implement anti-spam function for email sending? PHP and PHPMAILER: How to implement anti-spam function for email sending? Jul 22, 2023 am 11:46 AM

PHP and PHPMAILER: How to implement anti-spam function for email sending? Introduction: In the Internet age, email has become an indispensable part of our daily life and work. However, with the popularity and use of email, the problem of spam has become increasingly serious, causing a lot of trouble to users. In order to solve this problem, this article will introduce how to use PHP and PHPMailer library to implement the anti-spam function of email sending. 1. Understand spam. Spam refers to those unsolicited

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

How to disable smtp email function in php How to disable smtp email function in php Mar 22, 2023 pm 03:22 PM

PHP is a powerful programming language that is widely used in the field of Web development. The SMTP email function is also an important part of PHP development. However, in some cases, you may want to disable SMTP mail functionality, and this article will explain how to do this.

PHP methods and precautions for sending attachment emails using the PHPMailer library PHP methods and precautions for sending attachment emails using the PHPMailer library May 21, 2023 pm 06:12 PM

PHP methods and precautions for sending attachment emails using the PHPMailer library. Email has become a very important way of communication in modern life. In many development projects, we need to use code to automatically send emails. At this time, the PHPMailer library is our best choice. PHPMailer is a library dedicated to sending emails in PHP. It can easily send emails, including HTML-formatted emails and attachments. This article will focus on how to send emails with attachments in the PHPMailer library.

See all articles