Home >Backend Development >PHP Tutorial >How to send plain text email with attachments using PHP and PHPMAILER?

How to send plain text email with attachments using PHP and PHPMAILER?

PHPz
PHPzOriginal
2023-07-22 21:13:051176browse

How to send plain text email with attachments using PHP and PHPMAILER?

In the Internet age, email is still an integral part of our daily lives and work. While developing a website or application, sometimes we need to use PHP to send emails with attachments. This article will introduce how to use PHP and PHPMAILER to send plain text emails with attachments, and attach practical code examples.

First, we need to install the PHPMAILER library. You can download the latest version of PHPMAILER from the official website (https://github.com/PHPMailer/PHPMailer) and extract it into your project folder.

The following is an example of PHP code for sending plain text emails:

<?php
require 'PHPMailer/src/PHPMailer.php'; // 需要引入PHPMAILER库的主文件

// 实例化PHPMAILER对象
$mail = new PHPMailerPHPMailerPHPMailer();

// 设置SMTP邮箱服务器的相关信息
$mail->isSMTP(); // 使用SMTP协议发送邮件
$mail->Host = 'smtp.example.com'; // 设置SMTP服务器地址
$mail->SMTPAuth = true; // 使用SMTP验证
$mail->Username = 'your-email@example.com'; // 设置邮箱用户名
$mail->Password = 'your-email-password'; // 设置邮箱密码
$mail->SMTPSecure = 'tls'; // 启用TLS加密
$mail->Port = 587; // 设置SMTP服务器端口号

// 设置邮件接收人
$mail->setFrom('your-email@example.com', 'Your Name'); // 设置发件人邮箱和姓名
$mail->addAddress('recipient@example.com', 'Recipient Name'); // 设置收件人邮箱和姓名

// 添加附件
$mail->addAttachment('/path/to/attachment1.pdf', 'Attachment 1'); // 添加附件1
$mail->addAttachment('/path/to/attachment2.txt', 'Attachment 2'); // 添加附件2

// 设置邮件内容
$mail->isHTML(false); // 将邮件内容设置为纯文本
$mail->Subject = 'Example Subject'; // 设置邮件主题
$mail->Body = 'This is the body of the email.'; // 设置邮件正文

// 发送邮件
if ($mail->send()) {
    echo '邮件发送成功!';
} else {
    echo '邮件发送失败:' . $mail->ErrorInfo;
}
?>

In the above code, the following information needs to be modified according to the actual situation:

  1. Set the SMTP server address, Email username, email password, encryption method and port number;
  2. Set the sender’s email and name;
  3. Set the recipient’s email and name;
  4. Add attachments Path and file name;
  5. Set the email subject and body content.

Before running the above code, make sure you have correctly configured the SMTP mailbox server information and installed the PHPMAILER library.

With the above code example, we can easily send plain text emails with attachments using PHP and PHPMAILER. Whether it is email notifications in daily life, or email functions when developing websites or applications, PHPMAILER is a very convenient and powerful tool that can meet our needs. Hope this article helps you!

The above is the detailed content of How to send plain text email with attachments using PHP and PHPMAILER?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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