Home >Backend Development >PHP Tutorial >How to send emails with attachments on the website through PHP and PHPMAILER?
How to send emails with attachments on the website through PHP and PHPMAILER?
In website development, we often encounter scenarios where we need to send emails. In some cases, we need to attach files to emails, such as sending an order confirmation email containing attachments. So how to achieve this function through PHP and PHPMAILER? Below we will introduce the relevant implementation steps in detail.
First, we need to install the PHPMAILER library, which can be installed through Composer or downloaded from the official website and introduced related files. Next, introduce the PHPMAILER library into the PHP file that needs to send emails, and create a PHPMAILER instance.
require 'PHPMailer/PHPMailer.php'; $mail = new PHPMailerPHPMailerPHPMailer();
Next, we need to configure the relevant information of the mail server, including SMTP server address, SMTP server port, sender email address, sender name, SMTP account, SMTP password, etc.
$mail->isSMTP(); $mail->Host = 'smtp.example.com'; $mail->SMTPAuth = true; $mail->Username = 'your_email@example.com'; $mail->Password = 'your_password'; $mail->SMTPSecure = 'tls'; $mail->Port = 587; $mail->setFrom('your_email@example.com', 'Your Name');
Then, we need to set the recipient’s email address and name, the subject and content of the email.
$mail->addAddress('recipient@example.com', 'Recipient Name'); $mail->Subject = '邮件主题'; $mail->Body = '邮件内容';
The next step is the core part, we need to use the addAttachment method to add attachments. The first parameter of this method is the file path, and the second parameter is the display name of the attachment in the email.
$mail->addAttachment('/path/to/file.pdf', 'file.pdf');
If you need to send multiple attachments, you can use the addAttachment method multiple times.
Finally, we call the send method to send the email. If the sending is successful, it returns true, if the sending fails, it returns false.
if($mail->send()){ echo '邮件发送成功!'; }else{ echo '邮件发送失败:' . $mail->ErrorInfo; }
At this point, we have completed the entire process of sending emails with attachments through PHP and PHPMAILER. With the above simple code example, we can easily implement the function of sending emails with attachments on the website.
To summarize, in order to send emails with attachments, we need to use the PHPMAILER library. First, you need to install and introduce the PHPMAILER library and create a PHPMAILER instance in the PHP file. Then configure the SMTP server information, including SMTP server address, SMTP server port, sender information, etc. Next, set the recipient's email address, email subject and content, and use the addAttachment method to add attachments. Finally, call the send method to send the email.
I hope that the introduction and code examples of this article can help everyone better understand and use PHP and PHPMAILER to realize the function of sending emails with attachments.
The above is the detailed content of How to send emails with attachments on the website through PHP and PHPMAILER?. For more information, please follow other related articles on the PHP Chinese website!