Advanced e-mail functionality is provided by the Advanced Mail module.

The Advanced Mail module allows a much more fine-grained control of how emails are generated and what content is sent. Below the examples the available mail functions are listed.

Note

  • The mail context is only available in the current scripting context
  • When calling the mail_begin() function, any previously supplied mail configuration is cleared
  • The minimum required parameters to send a mail are fromAddress, subject, htmlContent and at least one recipient

Example

The following example creates an email from “support@structr.com” with three recipients, a reply-to address, a bounce address and five attachments. It also sets a custom header X-Mailer: Structr. The contents of the dynamic files are shown below.

${
	(
		mail_begin('support@structr.com', 'Structr Support', 'Support Mail', '<b>HTML Content</b>', 'Text Content'),
		
		mail_add_to('member1@structr.com', 'Structr Member 1'),
		mail_add_cc('member2@structr.com'),
		mail_add_bcc('member3@structr.com'),
		
		mail_add_reply_to('reply-to@structr.com', 'Reply-To'),

		mail_set_bounce_address('bounce@structr.com'),
		
		mail_add_attachment(first(find('File', 'name', 'static-file.txt'))),

		mail_add_attachment(first(find('File', 'name', 'static-file.txt')), 'renamed-static-file.txt'),
		
		mail_add_header('X-Mailer', 'Structr'),

		store('parameter1', 'Hello'),
		mail_add_attachment(first(find('File', 'name', 'dynamic-file.txt'))),
		
		each(
			merge('User', 'Page'),
			(
				store('fileType', data),
				mail_add_attachment(first(find('File', 'name', 'typeListing.xlsx')), concat(data, '-table.xlsx')),
			)
		),

		mail_send()
	)
}

Important: If you are prototyping your mail in a page environment make sure to conditionally send emails only if empty(request.edit) - otherwise dynamic files will not be interpreted because we are operating in edit-mode which always shows the source code. Simply use the following snippet and open the page itself in its own browser window/tab to send your mail.

	if (
		empty(request.edit),
		mail_send(),
		'not sending mail as we are in preview mode'
	)

dynamic-file.txt

${retrieve('parameter1')} World!

typeListing.xlsx (the usual settings for excel files are used - refer to to_excel() for more information)

${to_excel(find(retrieve('fileType')), 'public')}

Graph-Browser

Related Articles
About this article
Last change 2018-09-14
Topics