laswitchtech/php-smtp

这个PHP SMTP电子邮件库提供了一种简单易用的解决方案,用于通过SMTP服务器发送电子邮件。

v2.1.13 2024-05-02 12:21 UTC

README

GitHub repo logo

phpSMTP - [已弃用] - 请使用coreSMTP代替

License GitHub repo size GitHub top language Version

描述

这个库是SMTP(简单邮件传输协议)的PHP实现,允许使用SMTP服务器发送电子邮件。该库提供了一个名为phpSMTP的类,该类封装了与SMTP服务器建立连接、验证用户、设置电子邮件消息和发送电子邮件的功能。

该类提供了多种方法来配置电子邮件消息,例如设置发件人、收件人、主题、正文和附件。它还提供了一个方法来加载电子邮件模板并将消息正文插入其中。

此外,该类允许通过将客户端和服务器之间的通信记录到文件中来进行SMTP会话的调试。它还提供了错误处理和日志记录机制,以帮助识别和解决发送过程中可能出现的任何问题。

特性

  • 使用SMTP协议发送电子邮件
  • 支持TLS和SSL加密
  • 使用用户名和密码验证SMTP连接
  • 支持HTML和纯文本消息
  • 支持电子邮件附件
  • 使用占位符自定义电子邮件模板
  • 可选发送到多个收件人
  • 可选添加CC和BCC收件人
  • 可选指定回复电子邮件地址
  • 自动将电子邮件正文从HTML转换为纯文本
  • 调试模式用于调试SMTP连接
  • 将事件记录到文件
  • Composer包,便于安装和与其他PHP项目集成

你可能为什么需要它?

这个PHP SMTP电子邮件库提供了一种简单易用的解决方案,用于通过SMTP服务器发送电子邮件。它提供了一系列功能,如支持多个收件人、附件和HTML电子邮件。使用这个库,你可以轻松地将电子邮件功能集成到PHP应用程序中,并简化电子邮件通信。它还包括错误处理和日志记录,以确保任何问题都能迅速识别和解决。总的来说,这个库是任何希望将电子邮件功能纳入其项目的PHP开发人员的宝贵工具。

我能使用这个吗?

当然可以!

许可证

本软件根据GNU通用公共许可证v3.0分发。请阅读LICENSE以获取有关软件可用性和分发的信息。

要求

PHP >= 5.5.0

安全性

请负责任地披露发现的任何漏洞 - 私下向维护者报告安全问题。

安装

使用Composer

composer require laswitchtech/php-smtp

如何使用它?

示例

连接到SMTP服务器

//Import SMTP class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\SMTP\phpSMTP;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance of phpSMTP
$phpSMTP = new phpSMTP();

//Connect to your server
$phpSMTP->connect("username@domain.com","*******************","mail.domain.com","465","ssl");

对SMTP服务器进行用户验证

//Import SMTP class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\SMTP\phpSMTP;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance of phpSMTP
$phpSMTP = new phpSMTP();

if($phpSMTP->login("username@domain.com","*******************","mail.domain.com","465","ssl")){
  echo "User Authenticated!\n";
}

发送电子邮件

//Import SMTP class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\SMTP\phpSMTP;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance of phpSMTP
$phpSMTP = new phpSMTP();

//Connect to your server
$phpSMTP->connect("username@domain.com","*******************","mail.domain.com","465","ssl");

if($phpSMTP->send([
  "to" => "username@domain.com",
  "subject" => "Lorem",
  "body" => "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
])){
  echo "Message Sent!\n";
}

创建模板

您可以在HTML或PLAIN TEXT中创建模板。

以下是可以使用的占位符列表

  • %FROM%
  • %REPLY-TO%
  • %TO% (列表中的第一个)
  • %CC% (列表中的第一个)
  • %BCC% (列表中的第一个)
  • %SUBJECT%
  • %BODY%

纯文本示例

phpSMTP
============================================================================================================

%SUBJECT%
------------------------------------------------------------------------------------------------------------
Hi %TO%,
%BODY%

Sincerely,
phpSMTP

添加模板

//Import SMTP class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\SMTP\phpSMTP;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance of phpSMTP
$phpSMTP = new phpSMTP();

//Add your template
$phpSMTP->addTemplate('html','tmp/templates/default.html');

选择模板

//Import SMTP class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\SMTP\phpSMTP;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance of phpSMTP
$phpSMTP = new phpSMTP();

//Select your template
$phpSMTP->setTemplate('html');