Home > Backend Development > PHP Tutorial > How to send email in PHP

How to send email in PHP

WBOY
Release: 2023-05-22 10:42:01
Original
3495 people have browsed it

With the rapid development of modern technology, email has become an indispensable part of people's daily life and work. As website application developers, we need to send various types of emails to the users of our website. PHP is a widely used server-side scripting language with powerful email sending capabilities. This article will introduce how to send emails using PHP.

Basic principles of sending emails in PHP

The main steps of sending emails in PHP can be summarized as the following points:

1. Construct the email header
2 .Construct the email content
3.Connect to the mailbox server
4.Verify identity information
5.Send the email

Construct the email header

Constructing the email header means using PHP header function to specify the header information of the email. Email header information includes sender, recipient, email subject, and other information.

For example:

$to = "receiver@example.com";
$subject = "Email subject";
$headers = "From: sender@example.com ";

In this example, the $to variable specifies the recipient of the email, the $subject variable specifies the subject of the email, and the "From" item specifies the sender of the email. The value of $headers is a string used to specify various email header information. These messages include "From", "Reply-To", "To", "Cc", "Bcc", etc.

Constructing email content

Constructing email content mainly specifies the body content of the email. When sending emails in PHP, you can use two methods to structure email content: text mode and HTML mode.

Text mode:

$message="Email text content";

HTML mode:

$message="Email titleEmail content";

Connect to the email server

Sending emails requires connecting to the mailbox server. In PHP, you can use the following function to connect to the server:

$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpusername,$smtppassword);

Among them, $smtpserver specifies The address of the SMTP server; $smtpserverport specifies the port number of the SMTP server; $smtpusername specifies the authentication user name of the SMTP server; $smtppassword specifies the authentication password of the SMTP server.

Verify identity information

Verification of identity information refers to using the specified user name and password to log in to the SMTP server so that emails can be sent. In PHP, you can use the following code to verify identity information:

$smtp->auth($smtpusername,$smtppassword);

Send email

After completion After the above steps, you can use PHP functions to send emails. The function to send emails in PHP is mail(). It can receive three parameters:

mail($to,$subject,$message,$headers);

Among them, $to specifies the recipient of the email; $subject specifies the email The subject; $message specifies the content of the email; $headers specifies the header file information of the email.

For example:

mail($to,$subject,$message,$headers);

Complete code example

The following is a complete PHP Email sending example. In this example, we will use the PHPMailer class to send emails.

require_once("phpmailer/class.phpmailer.php");
require_once("phpmailer/class.smtp.php");

// Create a PHPMailer instance
$mail = new PHPMailer();

//Specify SMTP server
$mail->IsSMTP();
$mail->Host = "smtp .example.com";

//Specify username and password verification
$mail->SMTPAuth = true;
$mail->Username = "username@example.com";
$mail->Password = "password";

//Specify the encoding format for sending emails
$mail->CharSet = "utf-8";

/ /Specify the header of the email to be sent
$mail->From = "username@example.com";
$mail->FromName = "Sender's name";
$mail-> AddAddress("receiver@example.com", "recipient's name");
$mail->AddReplyTo("username@example.com", "sender's name");

//Specify the subject and body of the email
$mail->Subject = "Email subject";
$mail->Body = "Email body content";
$mail->AltBody = " Email body content, if the email client does not support HTML, this content will be displayed";

//Specify the attachment of the email
$mail->AddAttachment("./attachment.zip");

//Send the email and check whether it was sent successfully
if ($mail->Send()) {

echo "发送成功";
Copy after login

} else {

echo "发送失败:" . $mail->ErrorInfo;
Copy after login

}
? >

Summary

This article introduces you to how to use PHP to send emails. PHP's mail() function provides the basic function of sending emails, while the PHPMailer class provides more powerful email sending functions. Sending emails using PHP is very convenient and can be done with just a few lines of code. Hope this article can be helpful to you.

The above is the detailed content of How to send email in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template