定制 mouf/utils.mailer.mail-interface 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mouf/utils.mailer.mail-interface

最新稳定版本:v2.2.0

Composer 安装命令:

composer require mouf/utils.mailer.mail-interface

包简介

This package contains interfaces and classes describing mails. It also contains the MailerService interface that should be implemented by any service that can send mails. You should use a package implementing this interface (like the SmtpMailer package.

README 文档

README

Latest Stable Version Latest Unstable Version License Scrutinizer Code Quality

Mouf Mail system

The Mouf framework is only an IOC framework. As such, it does not provide any means for managing any kind of cache. Hopefully, the Mouf team provides also a range of packages to manage sending mails.

The mail architecture

In Mouf, emails are sent using MailServices.
Mouf provides 4 implementations of mail services. You can provide your own if you want.

By default, Mouf provides these 3 implementations:

  • SwiftMailService: a mail service that uses a SMTP server to send mails (this is a wrapper using the Swift mail library).
  • SmtpMailService: a mail service that uses a SMTP server to send mails (this is a wrapper using the Zend_Mail library).
  • DBMailService: a mail service that does not send any mails. Instead, it writes the mail in a MySQL database. It can forward the mail later to another mail service that will actually send the mail. This mail server is available in the package utils/mailer/db-mail-service.

Each mail service must extend the MailServiceInterface interface that is part of the package utils/mailer/mail-interface.

The mail service interface

Each class implementing the MailServiceInterface provides one simple method to send mails:

interface MailServiceInterface {
	
	/**
	 * Sends the mail passed in parameter.
	 *
	 * @param MailInterface $mail The mail to send.
	 */
	function send(MailInterface $mail);
}

The mail passed in parameter must implement the MailInterface interface. Hopefully, Mouf provides a Mail class that does just that!

For instance, to send a mail, you just need to write:

$mailService = Mouf::getSmtpMailService();

$mail = new 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"));
$mail->setTitle("My mail");

$mailService->send($mail);

The code above assumes that you configured an instance in Mouf called "smtpMailService".

The MailInterface interface supports:

  • Text/Html mails (setBodyText and setBodyHtml methods)
  • Multiple recipients (addToRecipient method)
  • Multiple Cc recipients (addCcRecipient method)
  • Multiple Bcc recipients (addBccRecipient method)
  • File attachments (addAttachment method)

Mail addresses are passed as a MailAddress object, that contains 2 strings: the mail address and the alias.

Automatic text body generation

When sending mails, it is a good practice to send 2 bodies: one in plain text and one in HTML. Forget the plain text and your mail could be flagged as spam. However, most of the time, your users will look at the mail in HTML. Mail clients that can only read plain text are really rare those days.

The Mail class can help you here. Indeed, it will automatically generate the plain text version of your mail from the HTML, by stripping all tags.

$mail = new Mail();
$mail->setBodyHtml("This is my <b>mail</b>!");
// No need to call $mail->setBodyText()

If you want to explicitly disable the plain text generation you can call this method:

$mail = new Mail();
$mail->setBodyHtml("This is my <b>mail</b>!");
// Disable plaing text generation from HTML. The mail will NOT contain the plain text part.
$mail->autoCreateBodyText(false);

统计信息

  • 总下载量: 47.43k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 12
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-05-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固