gavroche / sitesearch
使用一个接口发送电子邮件到所有平台的开源PHP库。
Requires
- php: >=5.6
- ext-json: *
- paragonie/random_compat: ^2.0
- php-http/httplug: ^1.1
- psr/log: ^1.1
Requires (Dev)
- daniel-zahariev/php-aws-ses: ^0.9.2
- friendsofphp/php-cs-fixer: ^2.16
- mailgun/mailgun-php: ^2.4
- mailin-api/mailin-api-php: ^1.0
- mailjet/mailjet-apiv3-php: ^1.2
- mandrill/mandrill: ^1.0
- php-http/guzzle6-adapter: ^1.1
- phpcompatibility/php-compatibility: ^9.3
- phpmailer/phpmailer: ^6.1
- phpunit/phpunit: ^5.7
- sendgrid/sendgrid: ^6.0
- squizlabs/php_codesniffer: ^3.5
- wildbit/postmark-php: ^2.5
Suggests
- daniel-zahariev/php-aws-ses: Required if you use the AmazonSES sending system
- mailgun/mailgun-php: Required if you use the Mailgun sending system
- mailin-api/mailin-api-php: Required if you use the SendinBlue sending system
- mailjet/mailjet-apiv3-php: Required if you use the Mailjet sending system
- mandrill/mandrill: Required if you use the Mandrill sending system
- php-http/guzzle6-adapter: Required if you use the Mailgun sending system
- phpmailer/phpmailer: Required if you use the SMTP sending method
- sendgrid/sendgrid: Required if you use the Sendgrid sending system
- wildbit/postmark-php: Required if you use the Postmark sending system
This package is not auto-updated.
Last update: 2024-09-23 15:05:12 UTC
README
使用一个接口发送电子邮件到所有平台。
目录
要求
本库使用PHP 5.6及以上版本。
安装
建议您通过composer安装Omnimail库通过composer。为此,运行Composer命令安装Omnimail库的最新稳定版本。
composer require omnimail/omnimail
提供商
AmazonSES
安装
要使用AmazonSES邮件发送类,您需要通过composer安装daniel-zahariev/php-aws-ses
库。
composer require "daniel-zahariev/php-aws-ses:^0.9.2"
用法
use Omnimail\Email; use Omnimail\AmazonSES; $mailer = new AmazonSES($accessKey, $secretKey, $region, $verifyPeer, $verifyHost, $signatureVersion); $email = (new Email()) ->addTo('example@email.com') ->setFrom('example@email.com') ->setSubject('Hello, world!') ->setTextBody('Hello World! How are you?'); $mailer->send($email);
Mailgun
安装
要使用Mailgun邮件发送类,您需要通过composer安装mailgun/mailgun-php
库。您还需要安装一个发送消息的HTTP客户端。您可以使用任何提供虚拟包php-http/client-implementation的客户端
composer require mailgun/mailgun-php php-http/guzzle6-adapter
用法
use Omnimail\Email; use Omnimail\Mailgun; $mailer = new Mailgun($apiKey, $domain); $email = (new Email()) ->addTo('example@email.com') ->setFrom('example@email.com') ->setSubject('Hello, world!') ->setTextBody('Hello World! How are you?'); $mailer->send($email);
Mailjet
安装
要使用Mailjet邮件发送类,您需要通过composer安装mailjet/mailjet-apiv3-php
库。
composer require mailjet/mailjet-apiv3-php
用法
use Omnimail\Email; use Omnimail\Mailjet; $mailer = new Mailjet($apikey, $apisecret); $email = (new Email()) ->addTo('example@email.com') ->setFrom('example@email.com') ->setSubject('Hello, world!') ->setTextBody('Hello World! How are you?'); $mailer->send($email);
Mandrill
安装
要使用Mandrill邮件发送类,您需要通过composer安装mandrill/mandrill
库。
composer require mandrill/mandrill
用法
use Omnimail\Email; use Omnimail\Mandrill; $mailer = new Mandrill($apiKey); $email = (new Email()) ->addTo('example@email.com') ->setFrom('example@email.com') ->setSubject('Hello, world!') ->setTextBody('Hello World! How are you?'); $mailer->send($email);
Postmark
安装
要使用Postmark邮件发送类,您需要通过composer安装wildbit/postmark-php
库。
composer require wildbit/postmark-php
用法
use Omnimail\Email; use Omnimail\Postmark; $mailer = new Postmark($serverApiToken); $email = (new Email()) ->addTo('example@email.com') ->setFrom('example@email.com') ->setSubject('Hello, world!') ->setTextBody('Hello World! How are you?'); $mailer->send($email);
Sendgrid
安装
要使用Sendgrid邮件发送类,您需要通过composer安装sendgrid/sendgrid
库。
composer require sendgrid/sendgrid
用法
use Omnimail\Email; use Omnimail\Sendgrid; $mailer = new Sendgrid($apiKey); $email = (new Email()) ->addTo('example@email.com') ->setFrom('example@email.com') ->setSubject('Hello, world!') ->setTextBody('Hello World! How are you?'); $mailer->send($email);
SendinBlue
安装
要使用SendinBlue邮件发送类,您需要通过composer安装mailin-api/mailin-api-php
库。
composer require mailin-api/mailin-api-php
用法
use Omnimail\Email; use Omnimail\SendinBlue; $mailer = new SendinBlue($accessKey); $email = (new Email()) ->addTo('example@email.com') ->setFrom('example@email.com') ->setSubject('Hello, world!') ->setTextBody('Hello World! How are you?'); $mailer->send($email);
SMTP
安装
要使用SMTP邮件发送类,您需要通过composer安装phpmailer/phpmailer
库。
composer require phpmailer/phpmailer
用法
use Omnimail\Email; use ShahariaAzam\SMTPMailer\SMTPMailer; $mailer = new SMTPMailer("SMTP HOSTNAME", "SMTP USERNAME", "SMTP PASSWORD"); $email = (new Email()) ->addTo('example@email.com') ->setFrom('example@email.com') ->setSubject('Hello, world!') ->setTextBody('Hello World! How are you?'); $mailer->send($email);
Gmail
安装
要使用Gmail邮件发送类,您需要通过composer安装phpmailer/phpmailer
库。
composer require phpmailer/phpmailer
用法
use Omnimail\Email; use Omnimail\Gmail; $mailer = new Gmail("you@gmail.com", "password", []); $email = (new Email()) ->addTo('example@email.com') ->setFrom('example@email.com') ->setSubject('Hello, world!') ->setTextBody('Hello World! How are you?'); $mailer->send($email);
电子邮件
Email
对象实现了EmailInterface
接口。您可以创建自己的Email
类,并将其发送到任何实现了EmailInterface
接口的邮件发送器。
收件人
电子邮件的To
属性用于定义电子邮件的收件人。您可以设置多个收件人。
$email = new Email(); $email->addTo('recipent1@email.com', 'Recipient1 Name'); $email->addTo('recipent2@email.com', 'Recipient2 Name');
发件人
电子邮件的From
属性用于定义电子邮件的发件人。
$email = new Email(); $email->setFrom('sender@email.com', 'Sender Name');
抄送
与To
属性类似,CC
属性也可以有多个收件人。
$email = new Email(); $email->addCc('recipent1@email.com', 'Recipient1 Name'); $email->addCc('recipent2@email.com', 'Recipient2 Name');
密送
与To
属性类似,BCC
属性也可以有多个收件人。
$email = new Email(); $email->addBcc('recipent1@email.com', 'Recipient1 Name'); $email->addBcc('recipent2@email.com', 'Recipient2 Name');
回复到
电子邮件的Reply To
属性用于定义应接收响应的电子邮件。
$email = new Email(); $email->setReplyTo('sender@email.com', 'Sender Name');
主题
电子邮件的Subject
属性用于定义电子邮件的主题。
$email = new Email(); $email->setSubject('Hello, World!');
文本正文
电子邮件的Text Body
属性用于定义电子邮件的文本正文。
$email = new Email(); $email->setTextBody('This is plain text.');
HTML正文
电子邮件的HTML Body
属性用于定义电子邮件的HTML正文。
$email = new Email(); $email->setHtmlBody('<h1>Hi!</h1><p>This is HTML!</p>');
附件
电子邮件的Attachments
属性用于将附件添加到电子邮件。
使用字符串作为内容的示例
use Omnimail\Email; use Omnimail\Attachment; $attachment = new Attachment(); $attachment->setName('my_file.txt'); $attachment->setMimeType('text/plain'); $attachment->setContent('This is plain text'); $email = new Email(); $email->addAttachment($attachment);
使用文件路径作为内容的示例
use Omnimail\Email; use Omnimail\Attachment; $attachment = new Attachment(); $attachment->setMimeType('text/plain'); $attachment->setPath(__DIR__ . '/my_file.txt'); $email = new Email(); $email->addAttachment($attachment);
内联附件
use Omnimail\Email; use Omnimail\Attachment; $attachment = new Attachment(); $attachment->setPath(__DIR__ . '/image.png'); $attachment->setContentId('image.png'); $email = new Email(); $email->setHtmlBody('<p>Hello!</p><img src="cid:image.png">'); $email->addAttachment($attachment);
工厂
或者,您可以使用工厂方法创建邮件发送器。以下是一个创建AmazonSES邮件发送器的示例:
use Omnimail\Omnimail; $mailer = Omnimail::create(Omnimail::AMAZONSES, ['accessKey' => $accessKey, 'secretKey' => $secretKey]);
群发邮件
群发邮件组件用于与群发邮件服务提供商交互。目前代码主要关注数据检索,但未来还应定义创建和发送群发邮件的功能。
目前对群发邮件接口描述了2个函数:getMailings
和 getRecipients
。
getMailings
$mailer = Omnimail::create('Silverpop', array('credentials' => new Credentials(array('username' => $userName...)))->getMailings(); $mailer->setStartTimeStamp(strtotime('7 days ago')); $mailer->setEndTimeStamp(strtotime('now')); // Instead of using set methods a Credentials object can be passed in ie. // Omnimail::create('Silverpop', array('credentials' => new Credentials(array('username' => $userName...))); // The advantage of using the Credentials object is that the object will not disclose // the credentials when var_dump or similar is called, helping to make the code // more secure. $mailings = $mailer->getResponse(); for ($i = 0; $i < 15; $i++) { if (!$mailings->isCompleted()) { sleep(15); } else { foreach (\Omnimail\Common\Responses\BaseResponse $mailings as \Omnimail\Common\Responses\Mailing $mailing) { $detail => array( 'subject' => $mailing→getSubject(), 'external_identifier' => $mailing->getMailingIdentifier(), 'name' => $mailing->getName(), 'scheduled_date' => $mailing->getScheduledDate(), 'start_date' => $mailing->getSendStartDate(), 'number_sent' => $mailing->getNumberSent(), 'body_html' => $mailing->getHtmlBody(), 'body_text' => $mailing→getTextBody(), // Note that in the case of Silverpop these statistics are not retrieved by the // same api call. This is invisible to the calling function, and unless // stats are requested they are not retrieved. 'number_bounced' => $mailing->getNumberBounces(), 'number_opened_total' => $mailing->getNumberOpens(), 'number_opened_unique' => $mailing->getNumberUniqueOpens(), 'number_unsubscribed' => $mailing->getNumberUnsubscribes(), 'number_suppressed' => $mailing->getNumberSuppressedByProvider(), // 'forwarded' 'number_blocked' => $mailing->getNumberBlocked(), // 'clicked_total' => $stats['NumGrossClick'], 'number_abuse_complaints' => $mailing->getNumberAbuseReports(), ); } } }
getMailings
$mailer = Omnimail::create('Silverpop')->getRecipients(); $mailer->setUserName($userName); $mailer->setPassword($password); $mailer->setStartTimeStamp(strtotime('7 days ago')); $mailer->setEndTimeStamp(strtotime('now')); $mailer->setMailingIdentifier(123); $recipients = $mailer->getResponse(); if (!$recipients->isCompleted()) { // sleep or exit & retry later. } foreach (\Omnimail\Responses\RecipientsResponse $recipients as \Omnimail\Responses\Recipient $$recipient) { $detail => array( 'external_identifier' => $mailing->getMailingIdentifier(), 'email' => $mailing->getEmail(), 'provider_contact_id' => $mailing->getContactIdentifier(), 'contact_id' => $mailing->GetContactReference(), // Const ACTION_SENT = ‘Sent’, ACTION_OPENED = ‘Open’, ... 'action' => $mailing->getRecipientAction(), 'timestamp' => getRecipientActionTimestamp(), );
异常
发送邮件失败将抛出异常。
异常
- Omnimail\Exception\Exception
- Omnimail\Exception\EmailDeliveryException
- Omnimail\Exception\InvalidRequestException
- Omnimail\Exception\UnauthorizedException
- Omnimail\Exception\MailerNotFoundException
要捕获所有异常,请考虑以下。
try { $mailer->send($email); } catch (\Omnimail\Exception\Exception $e) { echo 'Something went wrong: ' . $e->getMessage(); }
要捕获特定异常,请考虑以下。
try { $mailer->send($email); } catch (\Omnimail\Exception\UnauthorizedException $e) { echo 'Your credentials must be incorrect'; } catch (\Omnimail\Exception\InvalidRequestException $e) { echo 'The request is badly formatted, check that all required fields are filled.'; } catch (\Omnimail\Exception\EmailDeliveryException $e) { echo 'The email did not go out.'; }
日志记录
所有邮件发送器的构造函数都接受一个PSR-3兼容的日志记录器。
发送的电子邮件(包括邮件内容)在INFO级别记录。错误(包括邮件内容)在ERROR级别报告。
使用Monolog的示例
use Monolog\Logger; use Monolog\Handler\StreamHandler; use Omnimail\Mailgun; $logger = new Logger('name'); $logger->pushHandler(new StreamHandler('path/to/your.log', Logger::INFO)); $mailer = new Mailgun($apiKey, $domain, $logger); $mailer->send($email);
许可
Omnimail遵循MIT许可协议(MIT)。