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.

v2.2.0 2018-05-03 12:58 UTC

This package is auto-updated.

Last update: 2024-11-15 04:52:53 UTC


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);