Skip to content
Wesley Overdijk edited this page Aug 12, 2013 · 9 revisions

Using this module has been made ridiculously simple. After configuring the module you're ready to roll.

Basic usage

These are the basic methods you'll be using most of the time.

<?php

// This is the service. It only has two methods.
$mail = $serviceManager->get('SxMail\Service\SxMail');

// This method fetches the merged configuration for the specified $configKey.
// If left empty, defaults to default. Normally, you won't ever need this method.
$configuration = $mail->getConfig($configKey = null);

// This returns the SxMail instance you'll be working with, configured and ready to go.
$sxMail = $mail->prepare($configKey = null);

// Set the layout to use. 
// you'd use this if you decide to not use the layout from the config, or none has been set in the config.
// This method accepts thee input types:
//  - null                          Tries to use the layout from the config.
//  - Zend\View\Model\ViewModel     Sets the viewModel as layout
//  - string                        Uses string to locate (resolve to) a layout.
// The body will be set using property "content". So to get the body in the layout use $this->content
$sxMail->setLayout($template = null);

// Get the layout. You might want to set some variables on the layout ViewModel.
$sxMail->getLayout();

// This returns A Zend\Mail\Message. It accepts thee input types:
//  - null                          Converts body to an empty string
//  - Zend\View\Model\ViewModel     Renders the ViewModel
//  - string                        Uses string as body
// If you have configured a layout, the content of $body will be passed to the layout
// in key content. So in your layout, you can use $this->content to get the body.
$message = $sxMail->compose($body = null);

// Setting the recipient can be done as follows:
$message->setTo($emailAddress);

// This sends the email. Pretty simple.
$sxMail->send($message);

Advanced usage

These methods are a bit more advanced.

Composing with a mimePart.

By default, SxMail will detect which MimePart is needed for your content. If it's text, it will apply text/plain. It will also verify that your text doesn't contain html. If it does, it will set the type to text/html. If it's a ViewModel, it will apply text/html.

Note: This example doesn't have to be used. There is a built in functionality for this.

However, you can define which type you'd like to use by passing it as the argument to the compose method:

<?php
$message = $sxMail->compose('This is not html. Yet, I want it to be sent as if it were.', 'text/html');

This means that calling getBody on the returned $message will return a Zend\Mime\Message. This is cool, because that way you can send multi-part content. To add an extra MimePart, just get the body and add one.

<?php
$message = $sxMail->compose('<strong>Plain text version</strong>', 'text/html');

$mimePart       = new Zend\Mime\Part('Plain text version');
$mimePart->type = 'text/plain';

$message->getBody()->addPart($mimePart);
Clone this wiki locally