Home  >  Article  >  Backend Development  >  How to send mail using PHP and PHPMailer-SMTP

How to send mail using PHP and PHPMailer-SMTP

WBOY
WBOYOriginal
2023-05-11 08:15:051629browse

With the popularity of social networks and online communications, email has become an essential communication method in our daily work and life. As developers, we need to provide functionality for sending emails in our websites and applications. In this article, I will show you how to send mail using PHP and PHPMailer-SMTP.

First, we need to prepare some necessary components:

  1. Install PHPMailer

On the PHPMailer official website (https://github.com/PHPMailer/ PHPMailer) and extract it to the root directory of our website. Make sure the require_once('path/to/class.phpmailer.php'); statement is included before sending the email.

  1. Get SMTP server information

We need to know the host name, port number, email username and password of the SMTP server. This information can be obtained from your email service provider, such as Gmail.

With these components in hand, we can start writing code.

First, we need to instantiate the PHPMailer class:

$mail = new PHPMailer();

Then, we need to set the SMTP server information:

$mail->IsSMTP(); // Tell PHPMailer to use the SMTP server to send mail
$mail->Host = "smtp.gmail.com"; // SMTP server address
$mail-> ;Port = 465; // SMTP server port number
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = "your_email_address@gmail.com"; // SMTP user Name
$mail->Password = "your_email_password"; // SMTP password
$mail->SMTPSecure = "ssl"; // Enable SSL encryption, you can also set it to tls

Next, we set the basic information of the email:

$mail->From = "your_email_address@gmail.com"; // Sender's email address
$mail->FromName = "Your Name"; // Sender's name
$mail->AddAddress("recipient@example.com", "Recipient Name"); // Recipient's email address and name
$mail-> AddReplyTo("your_email_address@gmail.com", "Your Name"); // Reply email address and name
$mail->WordWrap = 50; // Set email text wrap
$mail->IsHTML (true); // Set the email format to HTML

Then, we set the email subject and body:

$mail->Subject = "Test Email"; // Email subject
$mail->Body = "This is a test email sent from PHPMailer using SMTP."; // Email text
$mail->AltBody = "This is the plain text version of the email content."; //Plain text version of the email body

Finally, we use the send() method to send the email:

if(!$mail->Send())
{
echo "Failed to send email:".$mail->ErrorInfo;
}
else
{
echo "Send email successfully! ";
}

In this example, we use Gmail as the SMTP server to send mail, but you can use any mail server that supports the SMTP protocol. Please note that some service providers may require you to Enable SMTP access on their website.

In summary, PHP and PHPMailer-SMTP are a reliable and convenient way to send emails, if you need to use email services in your website or application, please Be sure to try this method.

The above is the detailed content of How to send mail using PHP and PHPMailer-SMTP. 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