PHPMailer
是一个开源的PHP
邮件类库,可以方便快捷地在PHP程序中发送电子邮件。PHPMailer
支持发送纯文本邮件、HTML格式邮件和带附件的邮件,并且使用PHPMailer
代码,发送邮件的过程变得快捷、简单和安全。那么php如何利用PHPMailer
发送邮件?下面编程教程网小编给大家简单介绍一下!
下载PHPMailer
PHPMailer
的源代码可以从官方网站https://github.com/PHPMailer/PHPMailer
下载到。下载后,将PHPMailer
文件夹拷贝到需要使用的项目目录下,并引入PHPMailerAutoload.php
文件。
PHPMailer
发送邮件的php代码示例
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;
require '/path/to/phpmailer/src/Exception.php';
require '/path/to/phpmailer/src/PHPMailer.php';
require '/path/to/phpmailer/src/SMTP.php';
$mail = new PHPMailer(true);
try {
// 设置邮件发送的服务
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@example.com';
$mail->Password = 'password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
// 邮件发送人和接收人
$mail->setFrom('from@example.com', 'Sender');
$mail->addAddress('to@example.com', 'Receiver');
$mail->addReplyTo('info@example.com', 'Information');
// 邮件内容
$mail->isHTML(true);
$mail->Subject = 'PHPMailer Test';
$mail->Body = 'This is a test email sent by PHPMailer.';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
// 添加附件
$mail->addAttachment('/tmp/image.jpg');
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
以上是编程学习网小编为您介绍的“php如何利用PHPMailer发送邮件”的全面内容,想了解更多关于 php入门 内容,请继续关注编程基础学习网。
沃梦达教程
本文标题为:php如何利用PHPMailer发送邮件
猜你喜欢
- PHP使用QR Code生成二维码实例 2023-06-13
- PHP匹配连续的数字或字母的正则表达式 2024-01-11
- php中大厂的面试题整理 2023-05-09
- php实现微信公众平台发红包功能 2022-11-01
- Thinkphp自定义美化success和error提示跳转页面代码实例 2023-05-20
- PHP调用接口API封装的例子 2023-02-22
- php curl模拟post请求和提交多维数组的示例代码 2024-01-13
- PHP Class self 与 static 异同与使用详解 2022-09-02
- PHP中散列密码的安全性分析 2023-01-31
- php遍历目录输出目录及其下的所有文件示例 2024-02-27