landlib / simplemail
使用gmail.com的PHP mail()功能实现可靠且舒适的邮件发送。支持附件。使用gmail.com进行可靠的邮件发送。支持附件。
Requires
- php: ^5.4 || ^7.0 || ^7.1 || ^7.2. || ^7.2.1 || ^7.3 || ^7.4
This package is auto-updated.
Last update: 2024-09-14 22:52:38 UTC
README
内容 / 内容
En
内容
关于
这是一个可靠的邮件发送类,支持附件,使用PHP mail()函数。我使用这个PHP类已经有十年了,我发现它比Swift_Mailer和Symfony Mailer等邮件工具更可靠。
我不谦虚,但我会写。2019年11月,我创建了Symfony 3.4项目,它在我的本地机器上使用Gmail服务,在生产网站上使用托管提供商的邮箱。
2020年1月3日,我发现我的本地主机(我使用Gmail账户和ssmtp)突然停止发送邮件。Symphonies 3.4的日志中没有错误。
我没有修改我的Symfony脚本。
我尝试从Symfony 5.0.2项目中发送邮件,但得到了错误:“Exception occurred while flushing email queue: Connection could not be established with host smtp.gmail.com :stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed”。
我尝试使用我的SimpleMail类发送邮件,并成功发送。
我已在不同的PHP主机上使用过这个脚本,它确实有效(例如,我在这里的一个提交中使用它的类 https://github.com/lamzin-andrey/gz.loc/blob/master/www/lib/classes/mail/SampleMail.php)。
因此,我将这个PHP类公开,希望它也能帮助到你。
新年快乐,圣诞快乐。
安装
composer require landlib/simplemail
或者
git clone https://github.com/lamzin-andrey/simplemail
在产品服务器上使用
在您的托管提供商的ISP Manager上配置邮箱
使用ISP Manager界面创建邮箱。您将确信,您可以使用RoundCude或其他界面发送邮件。
示例
use Landlib\SimpleMail; //Simple email $sender = 'yoursendmailbox@yoursite.com'; $recipient = 'yourothermailbox@gmail.com'; $mailer = new SimpleMail(); $mailer->setSubject('It test package landlib/simplemail'); $mailer->setFrom($sender, 'Your name'); $mailer->setTo($recipient); $mailer->setBody('Hello, my friend', 'text/html', 'UTF-8'); $r = $mailer->send(); var_dump($r); //Mail with attach $mailer->setSubject('It test package landlib/simplemail - mail with inline attachment'); $mailer->setTextWithImages('Hello, my friend, {smile}!' . "\nI am a very satisfied person!", ['{smile}' => __DIR__ . '/smile.png']); $r = $mailer->send(); var_dump($r);
在本地Linux Ubuntu和Gmail服务上使用
这是为Linux Ubuntu桌面用户准备的。
创建Gmail账户并允许不安全应用程序访问
在2020年1月3日,可以在https://myaccount.google.com/lesssecureapps链接中找到
如果链接不起作用,配置您的ssmtp(见配置ssmtp),然后尝试运行示例脚本app.php。
php app.php
您将看到类似这样的文本
ssmtp: Authorization failed (535 5.7.8 https://support.google.com/mail/?p=BadCredentials h7sm24406885lfj.29 - gsmtp)
/opt/lampp/htdocs/mh.loc/www/q/q/simplemail/example/app.php:31:
bool(false)
转到消息中的链接并查看支持页面 - 它包含设置页面的链接,您可以在其中设置允许不安全应用程序访问。
安装和配置ssmtp服务器
这是为Linux Ubuntu桌面用户准备的。
sudo apt-get install ssmtp
让您的Gmail地址为testshop@gmail.com。
打开文件/etc/ssmtp/revaliases
sudo gedit /etc/ssmtp/revaliases
设置字符串
root:testshop@gmail.com:smtp.gmail.com:587
打开文件/etc/ssmtp/ssmtp.conf
sudo gedit /etc/ssmtp/ssmtp.conf
设置内容
root=testshop@gmail.com mailhub=smtp.gmail.com:587 hostname=smtp.gmail.com:587 UseSTARTTLS=YES AuthUser=testshop@gmail.com AuthPass=***** #your password must be here FromLineOverride=YES
打开您的php.ini文件(我使用XAMPP,我的php.ini位置是/opt/lampp/etc/php.ini)
并添加(或替换)字符串
sendmail_path = /usr/sbin/ssmtp -t
重新启动您的Apache服务器(我使用XAMPP,因此运行/opt/lampp/lampp restart)
运行示例脚本app.php(见创建gmail账户部分)
Ru
内容
这是什么
这是一个可靠的且方便的类,用于通过PHP mail()函数发送支持附件的电子邮件。我已经使用这个PHP类十年了,并发现它比Swift_Mailer和Symfony Mailer等电子邮件工具更可靠。
这可能听起来有点自大,但我将解释为什么我认为如此。
2019年11月,我创建了一个Symfony 3.4项目,该项目在我的本地主机上使用Gmail服务,在生产环境中使用主机提供商的邮箱。
2020年1月3日,我发现从我的本地主机(我使用Gmail账户和ssmtp)发送邮件突然停止了。在Symfony 3.4的日志中没有错误。
我没有编辑过我的Symfony脚本。
我尝试从Symfony 5.0.2项目中发送邮件,但得到了错误:“Exception occurred while flushing email queue: Connection could not be established with host smtp.gmail.com :stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed”。
然后我尝试使用我的SimpleMail类发送电子邮件,结果成功。
我之前在很多PHP主机上使用过这个脚本。它确实有效(例如,使用这个类的一个早期提交在这里 https://github.com/lamzin-andrey/gz.loc/blob/master/www/lib/classes/mail/SampleMail.php)。
因此,我发布了这个PHP类,希望它能对你有所帮助。
新年快乐和圣诞快乐。
安装
composer require landlib/simplemail
或者
git clone https://github.com/lamzin-andrey/simplemail
在产品上使用
在主机提供商的ISP Manager上配置邮箱
使用ISP Manager界面创建邮箱。请确保您可以使用RoundCude或其他界面发送邮件。
代码示例
use Landlib\SimpleMail; //Простой email $sender = 'yoursendmailbox@yoursite.com'; $recipient = 'yourothermailbox@gmail.com'; $mailer = new SimpleMail(); $mailer->setSubject('It test package landlib/simplemail'); $mailer->setFrom($sender, 'Your name'); $mailer->setTo($recipient); $mailer->setBody('Hello, my friend', 'text/html', 'UTF-8'); $r = $mailer->send(); var_dump($r); //Письмо с вложением $mailer->setSubject('It test package landlib/simplemail - mail with inline attachment'); $mailer->setTextWithImages('Hello, my friend, {smile}!' . "\nI am a very satisfied person!", ['{smile}' => __DIR__ . '/smile.png']); $r = $mailer->send(); var_dump($r);
在本地Linux Ubuntu和Gmail服务上使用
这是针对Linux Ubuntu桌面的用户。
创建Gmail账户并允许不安全的应用程序访问
在2020年1月3日可以通过以下链接实现:https://myaccount.google.com/lesssecureapps
如果链接无效,请设置ssmtp(见配置ssmtp),然后尝试运行example文件夹中的app.php示例脚本。
php app.php
您可能会看到类似以下文本的内容
ssmtp: Authorization failed (535 5.7.8 https://support.google.com/mail/?p=BadCredentials h7sm24406885lfj.29 - gsmtp)
/opt/lampp/htdocs/mh.loc/www/q/q/simplemail/example/app.php:31:
bool(false)
点击消息中的链接并查看支持页面——它包含指向设置页面的链接,您可以在其中允许不安全的应用程序访问。
安装和配置ssmtp服务器
这是针对Linux Ubuntu桌面的用户。
sudo apt-get install ssmtp
例如您的地址 testshop@gmail.com。
打开文件 /etc/ssmtp/revaliases
sudo gedit /etc/ssmtp/revaliases
添加以下行
root:testshop@gmail.com:smtp.gmail.com:587
打开文件 /etc/ssmtp/ssmtp.conf
sudo gedit /etc/ssmtp/ssmtp.conf
替换内容
root=testshop@gmail.com mailhub=smtp.gmail.com:587 hostname=smtp.gmail.com:587 UseSTARTTLS=YES AuthUser=testshop@gmail.com AuthPass=***** #здесь должен быть ваш пароль, кавычки не нужны FromLineOverride=YES
打开您的php.ini文件(我使用XAMPP,我的php.ini位于/opt/lampp/etc/php.ini)
并添加或编辑以下行
sendmail_path = /usr/sbin/ssmtp -t
重新启动Apache(我使用XAMPP,因此运行sudo /opt/lampp/lampp restart)
运行示例脚本app.php(见创建Gmail账户并允许不安全的应用程序访问)