Skip to content

Send Messages from Templates

Justin Kimbrell edited this page Jan 20, 2015 · 7 revisions

Overview

Postmaster gives developers the ability to send emails programmatically right from your templates. The craft.postmaster.send method will create and send a new Postmaster_TransportModel. The send method accepts two methods.

Request

{
	// This is unique id of the service you wish to use to send the message. 
	// By default `testing` will be used.
	service: 'mandrill',

	// A JSON object of settings that will be passed to the service.
	// The settings the are required are dependent on the service you
	// use.	
	serviceSettings: {
		apiKey': 'your api key',
		trackOpens: true,
		trackClicks: true,
		autoText: false,
		stripQuery: false,
		preserveRecipients: false
	},

	// A JSON object of settings that will be passed to the settings model, 
	// which by default is a `Postmaster_EmailModel` object. This parameter is
	// required.
	settings: {
		toName: 'Some Name',
		toEmail: test@test.com',
		fromName: 'From Name',
		fromEmail: 'test@test.com',
		replyTo: 'test@test.com',
		cc: 'test@test.com',
		bcc: 'test@test.com',
		subject: 'Some subject...',
		htmlBodyTemplate: 'emails/htmlTemplate',
		bodyTemplate: 'email/plainTemplate',
	}

	// A change the type of model used to instantiate the settings. 
	// The default value is `Craft\Postmaster_EmailModel`.
	settingsModel: 'Craft\Postmaster_EmailModel',

	// A JSON object of additional data passed for programmatic 
	// purposes to the `Postmaster_TransportModel`.
	data: {},

	// An optional setting to pass a senderId, also used for programmatic purposes. 
	// Omit this option if you have no use for it.
	senderId: false,

	// Set a date in which to send the message. If the message is post-dated 
	// for the future, it will be sent to the queue. Omit this option if you 
	// wish to send the message immediately.
	sendDate: false,

	// If are wanting to send an item from the queue, enter the id here. 
	// Most of the time this option does not need to be set.
	queueId: false
}

Variables

Enter a JSON in the second parameter of the send method to define variables to parse in your template. You can pass any variables you need and name them however you see fit.


Example

{% set request = {
	service: 'mandrill',
	serviceSettings: {
		apiKey': 'your api key',
		trackOpens: true,
		trackClicks: true,
		autoText: false,
		stripQuery: false,
		preserveRecipients: false
	},
	settings: {
		toName: 'Some Name',
		toEmail: test@test.com',
		fromName: 'From Name',
		fromEmail: 'test@test.com',
		replyTo: 'test@test.com',
		cc: 'test@test.com',
		bcc: 'test@test.com',
		subject: 'Some subject...',
		htmlBodyTemplate: 'emails/htmlTemplate',
		bodyTemplate: 'email/plainTemplate',
	},
	sendDate: '2015-01-01 12:00:00'
} %}

{% set response = craft.postmaster.send(request, {
	'someVar': 'Some Value',
	'entry': entry
}) %}