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:
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;
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; }
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; }
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!