onlime / sendmail-wrapper
一个强大的sendmail包装器,用于记录和限制PHP发送的电子邮件
Requires
- php: ^7.4 || ^8.0
- ext-imap: *
- ext-json: *
- ext-mbstring: *
- ext-pdo: *
Requires (Dev)
- laravel/pint: ^1.7
README
一个强大的sendmail包装器,用于记录和限制PHP发送的电子邮件
优点
- 允许您监控PHP脚本中的任何邮件流量
- 允许限制PHP的
mail()
函数发送的电子邮件 - 按每天发送的电子邮件数量和/或收件人数量进行限制
- 将消息元数据记录到syslog和数据库中
- 记录常见的邮件标题,如
From
、To
、Cc
、Bcc
、Subject
- 对未正确设置的用户动态修复
Return-Path
标题 - 高度安全的环境,客户无法访问日志/限制数据库
- 无任何外部库依赖的独立PHP应用程序
- 专为在PHP-FPM(FastCGI进程管理器)模式下运行的共享托管环境构建
- 无需cron作业,sendmail-wrapper将自动每天重置计数器
要求
- PHP 7.4+
- 兼容sendmail的MTA:Exim、Postfix等
- sudo 1.8+
目前,sendmail-wrapper已在PHP 8.0、8.1和8.2的共享托管环境中由Onlime GmbH测试并积极使用。
安装
初始设置
从GitHub克隆仓库
$ git clone https://github.com/onlime/sendmail-wrapper.git /opt/sendmail-wrapper
为sendmail-wrapper设置系统用户
$ adduser --system --home /no/home --no-create-home --uid 6000 --group --disabled-password --disabled-login sendmailwrapper $ adduser sendmailwrapper customers
快速安装
安装脚本install.sh将正确设置权限并创建包装脚本的符号链接
$ cd /opt/sendmail-wrapper/
$ ./install.sh
如果您想手动运行,请检查以下说明...
手动安装
设置正确的权限
$ chown sendmailwrapper:sendmailwrapper *.php *.ini $ chmod 400 config.private.ini $ chmod 444 config.ini config.local.ini $ chmod 555 sendmail-wrapper.php prepend.php $ chmod 500 sendmail-throttle.php $ chmod 400 schema/*.sql
创建符号链接
$ ln -sf /opt/sendmail-wrapper/sendmail-wrapper.php /usr/sbin/sendmail-wrapper $ ln -sf /opt/sendmail-wrapper/sendmail-throttle.php /usr/sbin/sendmail-throttle $ /bin/cp -a prepend.php /var/www/shared/
设置sudo
将以下行添加到您的 /etc/sudoers
www-data ALL = (sendmailwrapper) NOPASSWD:/usr/sbin/sendmail-throttle [0-9]*
%customers ALL = (sendmailwrapper) NOPASSWD:/usr/sbin/sendmail-throttle [0-9]*
设置PHP
在您的php.ini中添加/修改以下内容
sendmail_path = /usr/sbin/sendmail-wrapper -t -i auto_prepend_file = /var/www/shared/prepend.php
注意:建议将默认的
-t -i
选项放在php.ini中的sendmail_path
指令中,而不是直接追加到config.local.ini
中的sendmailCmd
配置选项。这样,它就不会破坏使用Symfony Mailer组件的项目(Laravel的Mail组件也使用Symfony Mailer作为底层!)。
设置MySQL
导入sendmailwrapper数据库模式
$ mysql -u root -p < schema/schema.mysql.sql
创建具有以下权限的MySQL用户
GRANT USAGE ON *.* TO sendmailwrapper@'localhost' IDENTIFIED BY '********'; GRANT SELECT, INSERT, UPDATE ON sendmailwrapper.throttle TO sendmailwrapper@'localhost'; GRANT INSERT ON sendmailwrapper.messages TO sendmailwrapper@'localhost';
配置
默认配置可在config.ini中找到
[global] defaultTZ = Europe/Zurich adminTo = hostmaster@example.com adminFrom = hostmaster@example.com [wrapper] sendmailCmd = "/usr/sbin/sendmail" throttleCmd ="sudo -u sendmailwrapper /usr/sbin/sendmail-throttle" throttleOn = true defaultHost = "example.com" syslogPrefix = sendmail-wrapper-php xHeaderPrefix = "X-Example-" [throttle] countMax = 1000 rcptMax = 1000 syslogPrefix = sendmail-throttle-php adminSubject = "Sendmail limit exceeded" [db] dsn = "mysql:host=localhost;dbname=sendmailwrapper" user = sendmailwrapper pass = "xxxxxxxxxxxxxxxxxxxxx"
您不应更改上述任何值。相反,创建自己的config.local.ini
以覆盖某些值,例如。
[global] adminTo = hostmaster@mydomain.com adminFrom = hostmaster@mydomain.com [wrapper] defaultHost = "mydomain.com" xHeaderPrefix = "X-MyCompany-"
永远不要在上述任何配置文件中放置您的数据库密码。使用名为config.private.ini
的另一个配置文件,例如。
[db] pass = "mySuper-SecurePassword/826.4287+foo"
升级
从1.0.x升级到1.1.x
为了避免使用Symfony Mailer(Laravel的Mail组件也使用Symfony Mailer作为底层!)的项目出现问题,我们将默认的sendmail命令行选项-t -i
从Sendmail-wrapper的配置config.ini
移动到推荐放在php.ini中的sendmail_path
指令。如果您坚持使用我们的默认配置,您需要更新您的php.ini
。
- sendmail_path = /usr/sbin/sendmail-wrapper + sendmail_path = /usr/sbin/sendmail-wrapper -t -i
如果您不关心Symfony Mailer或其他检查sendmail_path
中-t
存在的邮件组件,您可以保留旧的php.ini
配置,并将其添加到您的config.local.ini
sendmailCmd = "/usr/sbin/sendmail -t -i"