slm/mail

Integration of various email service providers in the Laminas\Mail

v5.0.2 2024-11-22 11:38 UTC

README

Build Status Latest Stable Version Scrutinizer Code Quality

SlmMail is a module that integrates with various third-parties API to send mails. Integration is provided with the API of those services. It does not handle SMTP.

Here are the currently supported services:

Installation

  1. First install the repo:

    composer require slm/mail

    • For Laminas MVC add SlmMail in your application.config.php file.
    • For Mezzio it should prompt whether we want to autoconfigure. Accept this.
  2. In order to use a mail service, you now need to configure it. We have provided a sample configuration file per mail server.

    Copy the sample configuration file to your autoload directory. For example for Mandrill one would use

    cp vendor/slm/mail/config/slm_mail.mandrill.local.php.dist config/autoload/slm_mail.mandrill.local.php

    Please tweak the dummy contents in this file. This file will contain the credentials.

Usage

One can now fetch the dependencies from the service manager. And now compose a message:

$message = new \Laminas\Mail\Message();
$message
    ->setTo('send@to')
    ->setFrom('send@by')
    ->setSubject('Subject')
    ->setBody('Contents');

$mandrillService = $container->get(\SlmMail\Service\MandrillService::class);
$mandrillService->send($message);

Documentation

Documentation for SlmMail is splitted for each provider:

Cook-book

How to send an HTML email ?

Every email providers used in SlmMail allow to send HTML emails. However, by default, if you set the mail's content using the setBody content, this content will be considered as the plain text version as shown below:

$message = new \Laminas\Mail\Message();

// This will be considered as plain text message, even if the string is valid HTML code
$message->setBody('Hello world');

To send a HTML version, you must specify the body as a MimeMessage, and add the HTML version as a MIME part, as shown below:

$message = new \Laminas\Mail\Message();

$htmlPart = new \Laminas\Mime\Part('<html><body><h1>Hello world</h1></body></html>');
$htmlPart->type = "text/html";

$textPart = new \Laminas\Mime\Part('Hello world');
$textPart->type = "text/plain";

$body = new \Laminas\Mime\Message();
$body->setParts(array($textPart, $htmlPart));

$message->setBody($body);

For accessibility purposes, you should always provide both a text and HTML version of your mails.

multipart/alternative emails with attachments

The correct way to compose an email message that contains text, html and attachments is to create a multipart/alternative part containing the text and html parts, followed by one or more parts for the attachments. See the Laminas Documentation for a full example.

How to configure HttpClient with http_options and http_adapter

By default the adapter is Laminas\Http\Client\Adapter\Socket but you can override it with other adapter like this in your slm_mail.*.local.php

'slm_mail' => array(
    // Here your email service provider options

    'http_adapter' => 'Laminas\Http\Client\Adapter\Proxy' // for example
)

If you want to change some options of your adapter please refer to you adapter class in var $config here and override these in your slm_mail.*.local.php like this :

'slm_mail' => array(
    // Here your email service provider options

    // example for Socket adapter
    'http_options' => array(
        'sslverifypeer' => false,
        'persistent' => true,
    ),
)

Which provider should I choose?

We won't answer you :-)! Each provider has their own set of features. You should carefully read each website to discover which one suits your needs best.

Who to thank?

Jurian Sluiman and Michaël Gallego did the initial work on creating this repo, and maintained it for a long time.

Currently it is maintained by: