Home > Backend Development > PHP Tutorial > PHP Mail Usage Guide: Simple and easy-to-understand tutorial for sending emails

PHP Mail Usage Guide: Simple and easy-to-understand tutorial for sending emails

王林
Release: 2024-03-28 12:14:01
Original
1149 people have browsed it

PHP Mail 用法指南:简单易懂的邮件发送教程

PHP is a scripting language widely used to develop web applications. It provides many functions for handling email sending. This article will introduce you to how to use the email sending function in PHP and provide specific code examples.

1. Preparation

Before using PHP to send emails, you first need to ensure that your server has been configured to send emails. Generally speaking, you need an SMTP server to send emails. You can use the SMTP server provided by your email provider. For example, Gmail's SMTP server address is: smtp.gmail.com.

Next, you need a valid email account and password for verification. Make sure your account is not set to restrict sending emails.

2. Set email information

Before you start sending an email, you need to set various information of the email, including recipients, subject, content, etc. The following is a simple sample code for setting email information:

$to = "recipient@example.com";
$subject = "这是邮件的主题";
$message = "这是邮件的内容";
$headers = "From: sender@example.com
";
Copy after login

3. Send email

After having the email information, we can use mail()## in PHP #Function to send email. The following is a complete sample code for sending an email:

$to = "recipient@example.com";
$subject = "这是邮件的主题";
$message = "这是邮件的内容";
$headers = "From: sender@example.com
";

if (mail($to, $subject, $message, $headers)) {
    echo "邮件发送成功!";
} else {
    echo "邮件发送失败!";
}
Copy after login

4. Send attachments

If you need to send attachments, you can use the fifth function of

mail() parameters to add attachments. The following is a sample code for sending emails containing attachments:

$to = "recipient@example.com";
$subject = "这是邮件的主题";
$message = "这是邮件的内容";
$headers = "From: sender@example.com
";

$attachment = chunk_split(base64_encode(file_get_contents("path/to/file.pdf")));
$headers .= "MIME-Version: 1.0
";
$headers .= "Content-Type: multipart/mixed; boundary="boundary"
";
$headers .= "--boundary
";
$headers .= "Content-Type: application/pdf; name="file.pdf"
";
$headers .= "Content-Transfer-Encoding: base64
";
$headers .= "Content-Disposition: attachment; filename="file.pdf"

";
$headers .= $attachment . "
";

if (mail($to, $subject, $message, $headers)) {
    echo "邮件发送成功!";
} else {
    echo "邮件发送失败!";
}
Copy after login
5. Summary

Through the introduction of this article, I believe you have understood how to send emails in PHP, including setting email information, sending Simple email and send email with attachments. In actual projects, email sending is a very common function. I hope this article can help everyone successfully implement the email sending function. If you have any questions, please leave a message!

The above is the detailed content of PHP Mail Usage Guide: Simple and easy-to-understand tutorial for sending emails. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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