How to send plain text email using PHP and PHPMAILER?

WBOY
Release: 2023-07-21 21:40:02
Original
847 people have browsed it

How to send plain text emails using PHP and PHPMAILER?

Overview:
In web application development, it is often necessary to send emails, whether it is sending verification emails, notification emails, or marketing emails. PHPMAILER is a very popular email sending class library in PHP. It provides rich functions and a simple and easy-to-use interface. This article will introduce how to use PHP and PHPMAILER to send plain text emails and provide corresponding code examples.

Steps:

  1. Download the PHPMAILER class library
    First, we need to download the PHPMAILER class library and import it into our project. The latest version of PHPMAILER can be downloaded from the PHPMAILER official website (https://github.com/PHPMailer/PHPMailer).
  2. Reference PHPMAILER class library
    Extract the downloaded PHPMAILER class library into the project directory, and introduce the PHPMAILER class library into our PHP code. This can be achieved using the following code:
require 'path/to/PHPMailer/PHPMailerAutoload.php';
Copy after login
  1. Create a mail object and set attributes
    Before sending an email, we need to create an instance of PHPMAILER and set related attributes, such as recipient Person, sender, subject and email content, etc. The following is a sample code:
$mail = new PHPMailer;

$mail->setFrom('sender@example.com', 'Sender Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Example Subject';
$mail->Body = 'This is the body of the email.';
Copy after login
  1. Configure mail server information
    To send mail, we need to configure the relevant information of the SMTP server, including server address, user name, password, port, etc. The following is the sample code:
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@example.com';
$mail->Password = 'secret';
$mail->Port = 587;
Copy after login
  1. Send email
    Once the configuration is completed, you can send emails by calling the send() method of PHPMAILER. The following is a sample code:
if ($mail->send()) {
    echo 'Email sent successfully!';
} else {
    echo 'Email could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}
Copy after login

After completing the above steps, we can use PHP and PHPMAILER to send plain text emails. According to actual needs, you can customize email content, subject, recipients, senders and other information.

Summary:
This article introduces how to use PHP and PHPMAILER to send plain text emails. By downloading and referencing the PHPMAILER class library, setting email properties, configuring SMTP server information, and finally calling the send() method, you can send emails. I hope this article is helpful to your email sending needs in web application development.

Reference code:

require 'path/to/PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->setFrom('sender@example.com', 'Sender Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Example Subject';
$mail->Body = 'This is the body of the email.';

$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@example.com';
$mail->Password = 'secret';
$mail->Port = 587;

if ($mail->send()) {
    echo 'Email sent successfully!';
} else {
    echo 'Email could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}
Copy after login

Note: path/to/PHPMailer/PHPMailerAutoload.php in the above code should be changed to the actual file path.

The above is the detailed content of How to send plain text email using PHP and PHPMAILER?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!