How to send HTML formatted emails with attachments using PHP and PHPMAILER?

WBOY
Release: 2023-07-22 15:14:01
Original
1447 people have browsed it

How to send emails with attachments in HTML format using PHP and PHPMAILER?

In modern society, email is an integral part of our daily lives and work. Sending emails in HTML format not only makes the email content richer and more diverse, but also adds some exquisite styles and layouts to the email. PHPMAILER is a very popular and convenient PHP library that provides the function of sending emails and easily adding attachments.

Below, I will introduce to you how to use PHP and PHPMAILER to send emails with attachments in HTML format. We will first install PHPMAILER and write a sample code to demonstrate the entire process.

Step One: Install PHPMAILER

First, we need to download the PHPMAILER library and include it into our project. You can download the latest version of the library file from PHPMAILER's official GitHub repository (https://github.com/PHPMailer/PHPMailer) and unzip it into your project directory.

Step 2: Introduce the PHPMAILER class file and create an email instance

At the beginning of your PHP file, use the require_once command to introduce the PHPMAILER class file:

require_once 'PHPMailer/PHPMailerAutoload.php';
Copy after login

Next, create A PHPMAILER instance and set SMTP parameters:

$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_email@example.com';
$mail->Password = 'your_email_password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
Copy after login

In the above code, we specify the SMTP server address, SMTP authentication user name and password, SMTP connection encryption method and port number and other information. Please modify it according to your own actual situation.

Step 3: Set the email content

Next, we set the sender, recipient, subject and content of the email. The code is as follows:

$mail->setFrom('your_email@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'This is the subject of the email';
$mail->isHTML(true);
$mail->Body = '<h1>This is the HTML body of the email</h1>';
Copy after login

In the above code, we set the sender's email address and name through the setFrom() method. The recipient's email address and recipient's name are set through the addAddress() method. The Subject variable is the subject of the email, the isHTML() method specifies the email content as HTML format, and the Body variable is the HTML body of the email.

Step 4: Add Attachment

To add an attachment, use the addAttachment() method. The code is as follows:

$mail->addAttachment('path_to_attachment_file.pdf', 'Attachment Name');
Copy after login

In the above code, the first parameter is the path of the attachment file, and the second parameter is the name of the attachment.

Step 5: Send the email

The last step is to send the email. Just use the send() method:

if ($mail->send()) {
    echo 'Email sent successfully';
} else {
    echo 'Email sending failed';
}
Copy after login

The complete sample code is as follows:

require_once 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_email@example.com';
$mail->Password = 'your_email_password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;

$mail->setFrom('your_email@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'This is the subject of the email';
$mail->isHTML(true);
$mail->Body = '<h1>This is the HTML body of the email</h1>';
$mail->addAttachment('path_to_attachment_file.pdf', 'Attachment Name');

if ($mail->send()) {
    echo 'Email sent successfully';
} else {
    echo 'Email sending failed';
}
Copy after login

The above is how to use PHP and PHPMAILER to send emails with attachments in HTML format. the whole process. In this way, you can easily send richer and more diverse emails, adding some beautiful styling and layout to the email content. Hope this article helps you!

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