snipworks / php-smtp
简单的PHP SMTP邮件发送脚本
v2.0.4
2020-09-06 10:10 UTC
Requires
- php: >=5.3
- ext-fileinfo: *
- ext-openssl: *
README
一个易于使用的SMTP(简单邮件传输协议)库,可帮助您发送电子邮件。
安装
composer require snipworks/php-smtp
示例
未加密
<?php use Snipworks\Smtp\Email; $mail = new Email('smtp.example.com', 25); $mail->setLogin('sender@example.com', 'password'); $mail->addTo('recipient@example.com', 'Example Receiver'); $mail->setFrom('example@example.com', 'Example Sender'); $mail->setSubject('Example subject'); $mail->setHtmlMessage('<b>Example message</b>...'); if($mail->send()){ echo 'Success!'; } else { echo 'An error occurred.'; }
加密(TLS)
<?php use Snipworks\Smtp\Email; $mail = new Email('smtp.example.com', 587); $mail->setProtocol(Email::TLS); $mail->setLogin('sender@example.com', 'password'); $mail->addTo('recipient@example.com', 'Example Receiver'); $mail->setFrom('example@example.com', 'Example Sender'); $mail->setSubject('Example subject'); $mail->setHtmlMessage('<b>Example message</b>...'); if($mail->send()){ echo 'Success!'; } else { echo 'An error occurred.'; }
不建议将SMTP登录凭据硬编码在如上示例中。建议将它们放在另一个文件中并加载,或设置为环境变量。
<?php // config.php define('SMTP_PRIMARY_EMAIL', 'sender@example.com'); define('SMTP_PRIMARY_PASSWORD', 'my very secret password');
<?php require_once('config.php'); // ... $mail->setLogin(SMTP_PRIMARY_EMAIL, SMTP_PRIMARY_PASSWORD); // ...
如果可能,还建议将配置放在公共Web根目录之外。例如,这可以防止人们通过错误配置远程包含您的PHP文件。