HOW TO SEND EMAILS WITH PHP MAIL() FUNCTION AND PHP MAILER

  -  
Download Now thư điện tử Benchmark Report 2022 - A study of 100B Emails
*

*

*

*

*

The tác giả voluntarily contributed this tutorial as a part of Pepipost Write lớn Contribute program.

Bạn đang xem: How to send emails with php mail() function and php mailer

Introduction

In this tutorial, you will learn how lớn send e-mail in PHP using PHPMailer library via gmail SMTP. Like PHPMailer, there are few more good e-mail sending libraries in PHP e.g. PEAR::Mail interface, Swiftmailer etc which can help you easily send mail in PHP using email SMTP.

Prerequisites

Before starting with steps on how lớn send mail using SMTP in PHP example, lets first see what are few limits with email SMTP servers và how lớn overcome some of these:

Gmail limits the number of recipients in a single thư điện tử and the number of emails that can be sent per day. The current limit is 500 Emails in a day or 500 recipients in a single email. You can"t really increase this limit. If you want to lớn send above these limit, then you need to integrate with third-party e-mail delivery platform like Pepipost Sendgrid etc.On reaching threshold limits, you won"t be able khổng lồ send messages for the next 24 hours. Once this temporary suspension period is over, the counter gets reset automatically, & the user can resume sending emails.By default, any third-party apps/codes are not allowed to lớn send emails using your gmail account. And, hence there are few settings which need khổng lồ be done at your end:

How lớn Enable e-mail Sending In Gmail?

Make sure that2-Step-Verificationis disabled.If 2-step-verification is enabled, then you will have khổng lồ create app password for your application or device.For security measures, Google may require you khổng lồ complete this additional step while signing-in. Click here khổng lồ allow access to your Google account using the new device/app.

Note: It may take an hour or more khổng lồ reflect any security changes

Writing the PHP Code lớn Send e-mail using gmail SMTP

Step 1:Download PHPMailer library fromthis github link. To directly download the .zip file, usethislink.

Unzip the master.zip in your application directory and run following command from your application directory.

Xem thêm: Hiện Tại, Việc Kết Nối Đến Hệ Thống Viettel Đang Gặp Sự Cố. Chúng Tôi Xin Lỗi Vì Sự Bất Tiện Này

composer require phpmailer/phpmailerComposeris the recommended way lớn install PHPMailer.

Step 2:Writing the PHP Code to lớn make an SMTP connection

Using your email credentials, connect khổng lồ host"smtp.gmail.com"

Step 3:Include packages và files for PHPMailer và SMTP protocol:

use PHPMailerPHPMailerPHPMailer;use PHPMailerPHPMailerException;require "PHPMailer-master/src/Exception.php";require "PHPMailer-master/src/PHPMailer.php";require "PHPMailer-master/src/SMTP.php";

Step 4:Initialize PHP Mailer và set SMTP as mailing protocol:

$mail = new PHPMailer();$mail->IsSMTP();$mail->Mailer = "smtp";

Step 5:Set required parameters for making an SMTP connection like server, port và account credentials. SSL và TLS are both cryptographic protocols that provide authentication và data encryption between servers, machines & applications operating over a network. SSL is the predecessor to TLS.

$mail->SMTPDebug = 1; $mail->SMTPAuth = TRUE;$mail->SMTPSecure = "tls";$mail->Port = 587;$mail->Host = "smtp.gmail.com";$mail->Username = "";$mail->Password = "your-gmail-password";

Step 6:Set the required parameters for email header and body:

$mail->IsHTML(true);$mail->AddAddress("", "recipient-name");$mail->SetFrom("", "from-name");$mail->AddReplyTo("", "reply-to-name");$mail->AddCC("", "cc-recipient-name");$mail->Subject = "Test is Test thư điện tử sent via gmail SMTP server using PHP Mailer";$content = "This is a Test thư điện tử sent via tin nhắn SMTP server using PHP mailer class.

Xem thêm: Những Việc Có Thể Làm Thêm Tại Nhà Online "Hốt Bạc" Không Cần Vốn Nhiều

";

Step 7:Send the thư điện tử and catch required exceptions:

$mail->MsgHTML($content); if(!$mail->Send()) echo "Error while sending Email."; var_dump($mail); else echo "Email sent successfully";

Working PHP Code to Send e-mail Using SMTP Server

Click hereto tải về the complete working PHP code to lớn send e-mail using email SMTP server. You need khổng lồ just change a few values & it should work.

List of Possible Errors & Exceptions

Error 1:Password Command Failed: 534-5.7.9 Application-Specific Password Required