Home >Backend Development >PHP Tutorial >How to send HTML formatted emails with attachments using PHP and PHPMAILER?
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';
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;
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>';
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');
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'; }
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'; }
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!