mouf / utils.mailer.swift-mail-service
本包包含一个用于Mouf框架的邮件发送器,该发送器使用SMTP服务器发送邮件。本包是Swift库中Swift_Mailer类的包装。
Requires
- php: >=5.3.0
- mouf/utils.common.mouf-helpers: ~2.0
- mouf/utils.mailer.mail-interface: ~2.0
- psr/log: ~1.0
- swiftmailer/swiftmailer: ~5.2
This package is auto-updated.
Last update: 2024-09-15 04:52:31 UTC
README
SwiftMailService
是 Mouf PHP 框架 Swift 库的包装。它用于通过 SMTP 邮件服务器发送邮件。
SwiftMailService
被设计得易于使用。在幕后,SwiftMailService
使用了更复杂(更强大)的 Swift Mailer 邮件服务。
安装
本包是 Mouf PHP 框架 的一部分。因此,它附带了一个很好的图形化安装程序。
您可以根据需要配置连接到您的 SMTP 服务器的设置。
有一个必填参数:host,即服务器地址。
默认情况下,在 Linux 系统上,您可能需要使用本地邮件服务器(host=127.0.0.1)。您的机器上将安装 "sendmail" 或 "postfix" 服务器。如果您在 Windows 机器上执行开发,那么您的机器上可能没有 SMTP 服务器。因此,您将不得不使用远程服务器。要访问远程服务器,您可能必须使用登录名/密码等...
当此包安装后,它将创建 2 个实例
- a
swiftMailService
实现了 Mouf 的MailServiceInterface
- a
swiftMailer
是经典的 Swift mailer。
安装后,您会发现许多常量已添加到您的 config.php
文件中。当在其他服务器上部署时,您可以当然更改这些常量以适应服务器的设置。
什么?2 个实例?
既有 swiftMailService
也有 swiftMailer
。您应该使用哪个?
好吧,这取决于...
-
swiftMailService
实现 Mouf 的MailServiceInterface
。这是一个创建邮件的简单接口。如果您有简单需求,请使用它。如果您使用此实例,您将能够轻松地将您的 Swift mailer 替换为任何与MailServiceInterface
兼容的其他邮件发送器,例如SmtpMailService
(使用 ZendMail 作为后端)或 DBMailService(将邮件存储在数据库中而不是发送)。 -
swiftMailer
如果您想对邮件有更多控制,或者您已经习惯了直接使用 Swift,那么它非常棒。它在网上也有很好的文档。
提示:使用您的 Gmail 账户发送邮件
在开发环境中,使用您的 Gmail 账户可能很有用。以下是设置
- host => 'smtp.gmail.com'
- ssl => 'tls'
- port => 587
- auth => 'login'
- username => 您的 Gmail 邮箱地址
- password => 您的密码
示例使用
以下是您可以使用来发送邮件的示例代码。
use Mouf\Utils\Mailer\Mail; use Mouf\Utils\Mailer\SwiftMailService; // First, let's create the mail object $mail = new Mail(); $mail->setTitle("My mail"); $mail->setBodyText("This is my mail!"); $mail->setBodyHtml("This is my <b>mail</b>!"); $mail->setFrom(new MailAddress("my@server.com", "Server")); $mail->addToRecipient(new MailAddress("david@email.com", "David")); // Let's get the instance of the service $mailService = Mouf::getSwiftMailService(); // Finally, we send the mail $mailService->send($mail);
带有附件发送邮件
use Mouf\Utils\Mailer\Mail; use Mouf\Utils\Mailer\SwiftMailService; // The mail object $mail = new Mail(); $mail->setTitle("My mail"); $mail->setBodyHtml("A nice image: <img src='cid:my@img.resource'/>"); $mail->setFrom(new MailAddress("my@server.com", "Server")); $mail->addToRecipient(new MailAddress("david@email.com", "David")); // The attachment $attachment = new MailAttachment(); $attachment->setFileContent(file_get_contents('attachment.pdf')); $attachment->setMimeType('application/pdf'); $mail->addAttachement($attachment); // Let's get the instance of the service $mailService = Mouf::getSwiftMailService(); // Finally, we send the mail $mailService->send($mail);
带有嵌入图像的邮件发送
use Mouf\Utils\Mailer\Mail; use Mouf\Utils\Mailer\SwiftMailService; // The image we will embed: $embeddedImage = new MailAttachment(); $embeddedImage->setFileContent(file_get_contents('img.png')); $embeddedImage->setMimeType('image/png'); $embeddedImage->setAttachmentDisposition('inline'); // Note: the format of the ID should be in the syntax of a mail: xxx@yyy.zzz $embeddedImage->setContentId('my@img.resource'); // The mail object $mail = new Mail(); $mail->addAttachement($embeddedImage); $mail->setTitle("My mail"); $mail->setBodyHtml("A nice image: <img src='cid:my@img.resource'/>"); $mail->setFrom(new MailAddress("my@server.com", "Server")); $mail->addToRecipient(new MailAddress("david@email.com", "David")); // Let's get the instance of the service $mailService = Mouf::getSwiftMailService(); // Finally, we send the mail $mailService->send($mail);
要了解如何发送邮件的更多信息,请参阅 Mouf 邮件架构介绍。