jdz/mailer
最新稳定版本:1.0.9
Composer 安装命令:
composer require jdz/mailer
包简介
JDZ mailer
README 文档
README
Flexible PHP mailer with SMTP (via PHPMailer), Mailchimp Transactional, and native mail() support. Automatic fallback between senders, HTML/plain-text content handling, DKIM signing, and attachments.
Installation
composer require jdz/mailer
Optional senders (install as needed):
composer require phpmailer/phpmailer # SMTP / DKIM / Sendmail composer require mailchimp/transactional # Mailchimp Transactional API
Optional HTML-to-text converters for the alternative body:
composer require ph-7/html-to-text # PH7 converter composer require soundasleep/html2text # Soundasleep converter
Requirements
- PHP >= 8.2
Configuration
Copy .env.dist to .env and fill in your credentials:
cp .env.dist .env
The .env file is gitignored and never committed.
Usage
SMTP Email
use Symfony\Component\Dotenv\Dotenv; use JDZ\Mailer\Mailer; use JDZ\Mailer\AltBody\BasicAltBody; (new Dotenv())->loadEnv(__DIR__ . '/.env'); $mail = new Mailer(); $mail->setProperties([ 'domain' => 'example.com', 'language' => 'fr', 'charset' => 'utf-8', ]); $mail->setContent([ 'isHtml' => true, 'content' => '<h1>Hello</h1><p>This is a test email.</p>', 'template' => '<html><body>{{BODY}}</body></html>', 'altBodyFormatter' => new BasicAltBody(), ]); $mail->setSMTP([ 'host' => $_ENV['SMTP_HOST'], 'port' => (int)$_ENV['SMTP_PORT'], 'secure' => $_ENV['SMTP_SECURE'], 'auth' => true, 'user' => $_ENV['SMTP_USER'], 'pass' => $_ENV['SMTP_PASS'], ]); $mail->setFrom($_ENV['MAIL_FROM_EMAIL'], $_ENV['MAIL_FROM_NAME']); $mail->addRecipient('recipient@example.com', 'Recipient'); $mail->set('subject', 'Hello from JDZ Mailer'); $mail->send();
Mailchimp Transactional
$mail = new Mailer(); $mail->setContent([ 'isHtml' => true, 'content' => '<p>Newsletter content</p>', 'altBodyFormatter' => new BasicAltBody(), ]); $mail->setMailchimp([ 'apiKey' => $_ENV['MAILCHIMP_API_KEY'], 'track_opens' => true, 'track_clicks' => true, ]); $mail->addRecipient('subscriber@example.com', 'Subscriber'); $mail->set('subject', 'Monthly Newsletter'); $mail->send();
Native mail()
If no SMTP or Mailchimp is configured, the mailer falls back to PHP's native mail() function:
$mail = new Mailer(); $mail->domain = 'example.com'; $mail->setFrom('sender@example.com', 'Sender'); $mail->addRecipient('recipient@example.com'); $mail->set('subject', 'Simple email'); $mail->content->setProperties([ 'content' => 'Plain text message', 'altBodyFormatter' => new BasicAltBody(), ]); $mail->send();
Fallback
Enable useFallback to automatically fall back to a simpler sender if the preferred one is unavailable:
$mail->useFallback = true; // mailchimp -> smtp -> mail()
Recipients
$mail->addRecipient('to@example.com', 'To'); $mail->addCc('cc@example.com', 'CC'); $mail->addBcc('bcc@example.com', 'BCC'); $mail->addReplyTo('reply@example.com', 'Reply To'); // Or bulk: $mail->addRecipients([ ['email' => 'alice@example.com', 'name' => 'Alice'], ['email' => 'bob@example.com', 'name' => 'Bob'], ]); // No-reply (prevents further addReplyTo calls): $mail->setNoReply('noreply@example.com');
Attachments
$mail->addAttachment('/path/to/file.pdf', 'report.pdf', 'base64', 'application/pdf');
Content Replacements
$mail->setContent([ 'isHtml' => true, 'content' => '<p>Hello {{NAME}}, your order #{{ORDER}} is ready.</p>', 'replacements' => [ '{{NAME}}' => 'John', '{{ORDER}}' => '12345', ], 'altBodyFormatter' => new BasicAltBody(), ]);
Template-Only Mode
Use a full template without separate content:
$mail->setContent([ 'templateOnly' => true, 'template' => '<html><body><h1>Full HTML template</h1></body></html>', 'altBodyFormatter' => new BasicAltBody(), ]);
Alt Body Formatters
Three HTML-to-plain-text converters are available:
BasicAltBody- Built-in, usesstrip_tags()(no extra dependency)Ph7AltBody- Usesph-7/html-to-textSoundasleepAltBody- Usessoundasleep/html2text
Exceptions
ConfigException- Invalid configuration (missing fields, bad values)SmtpException- SMTP send failureMailchimpException- Mailchimp API failureException- General mailer errors
try { $mail->send(); } catch (\JDZ\Mailer\Exception\ConfigException $e) { // Bad configuration } catch (\JDZ\Mailer\Exception\SmtpException $e) { // SMTP error } catch (\JDZ\Mailer\Exception\MailchimpException $e) { // Mailchimp error } catch (\JDZ\Mailer\Exception\Exception $e) { // General error }
Testing
composer test # or vendor/bin/phpunit
License
This project is licensed under the MIT License - see the LICENSE file for details.
统计信息
- 总下载量: 38
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-13