php如何利用PHPMailer发送邮件

493 ℃

PHPMailer是一个开源的PHP邮件类库,可以方便快捷地在PHP程序中发送电子邮件。PHPMailer支持发送纯文本邮件、HTML格式邮件和带附件的邮件,并且使用PHPMailer代码,发送邮件的过程变得快捷、简单和安全。那么php如何利用PHPMailer发送邮件​?下面web建站小编给大家简单介绍一下!​

下载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语法如何利用SwiftMailer库发送HTML格式的邮件

php语法如何使用mail函数发送邮件

php如何实现邮件分发功能

php如何实现连接邮箱服务器

标签: PHPMailer, php发送邮件, PHP邮件类库

上面是“php如何利用PHPMailer发送邮件”的全面内容,想了解更多关于 php入门 内容,请继续关注web建站教程。

当前网址:https://m.ipkd.cn/webs_4588.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

帝国cms栏目导航调用之SQL语句调用一级栏目和二
js如何从2个数组中找到相同元素
el-table表头列动态加载后出现页面闪动
javascript赋值运算符介绍方法
vuejs点击重置报"TypeError: Cannot read properties of undefined (reading 'resetFields')"错误