Download the PHP package leuffen/template-mailer without Composer
On this page you can find all versions of the php package leuffen/template-mailer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download leuffen/template-mailer
More information about leuffen/template-mailer
Files in leuffen/template-mailer
Package template-mailer
Short Description Send mails including template engine
License mit
Homepage https://leuffen.de
Informations about the package template-mailer
Template-Mailer
This project combines an powerfull but easy-to-use OOP interface to Multipart-Mime-Mails in conjunction with a easy to learn template-language for defining complete multipart-messages within one string. (Including subject, from, other headers, all kinds of message-parts).
Install
Install using composer:
Basic Example
See directory
/doc/template/tpl/
for a bunch of working examples. You can life-test the examples with/doc/template/SendTestMail.php
against your MailClient / MTA.
Define a template using text-template syntax
Notice: To display the html-content by default it's important, to put the text/html content to the end of the mail
Load the template and send the mail:
That's it
Adding MailParts to a template
Use the apply()
method to not just send the mail but return the MailBody
:
Add an attachment using the FileAttachment
helper class:
or the manual way:
send the mail:
That's it.
Programmatic multipart example
You can use template-mailer to send multipart-mail without any templating directly from your code:
Using the Template Engine
For standard purpose you should use the MailTemplateParser to generate your Mail. But template-mailer can also be used like an OOP Mail Frontend. You'll find mor Information about that topic in the next section.
Security enhancements when using Templates
-
Auto Escaping: TemplateParser will escape values according to your content-type. Values in text/html mailParts will be htmlspecialchars()'d
-
Mail injection: Values in the header-section will be automaticly trimmed to 255 bytes and LineBreaks are converted to spaces
-
Transparent Encoding: The templating-engine will set and encode any content in the best available transfer-encoding
- UTF-8 by default: Both, templates and values are expected in corret UTF-8 formatting. By default outgoing mails will be UTF-8 formated as well - unless you change the Charset: - Header. In this case template-mail will do tranparent conversion for you.
Defining Templates
Template-Mail uses Text-Template (http://github.com/dermatthes/text-template) Syntax to define your templates.
Template consist of two sections. The header-section...
... followed by one empty line, and the body-section...
... where you define each part of your mail between <mailPart>
and </mailPart>
.
You don't have to take care about boundaries, additional headers nor correct escaping.
When defining mailPart you can use the attributes defined below to fit your needs:
Attribute | Description | Allowed values |
---|---|---|
contentType | text/plain, text/html, application/pdf, ... | |
contentDisposition | attachment, inline | |
contentTransferEncoding | Default: 8Bit | 8Bit, quoted-printable, base64 |
charset | Default: UTF-8 | utf-8, iso-8895-1, ... |
fileName | Used with contentDispositon="attachment": The filename to display in attachements | |
token | ||
id | Used to reference attached files. If you name it somefile you can reference it <img src="cid:somefile"> |
|
skipEncoding | If skipEncoding="no" is present, Text-Template will do no own encoding to the contents | YES, NO |
The MailBody Content-Type
One word to the content-type of the container-mail:
-
multipart/mixed
: Default, if more than one mailPart isset- All Parts are shown in order of occurence
- Attachments possible
-
multipart/alternative
: Use this if you want to display only one part (HTML or TEXt) with inline Attachements- Last part has highest precedence
- Attachments will not be displayed
multipart/related
: Use this if you want to embed attachements into the mail- First part has highest precedence
- Inline-Attachments will not be displayed if they are displayed inside html-code.
By default, Template-Mailer will set the content-type in de header of the container-mail to multipart/mixed
, which
allows multiple attachments.
But it will display all attachments.
You can set it to use multipart/alternative
by adding the Header
Debugging the context
To see the structure of the data passed to the template just add
to your template. It will output the structure of the context.
Accessing the TextTemplate Parser
To access and configure the TextTemplate Parser use the MailTemplateParser::getTextTemplateParser()
method.
Example:
Changing the Deliveryagent to an non local SMTP Server
To change the DeliveryAgent, it must be set in the MailKernel, via the static function SetMailDeliveryAgent The SmtpDeliveryAgent uses the class PHPMailer to send an SMTP Message to the chosen Server
Example:
About
Template-Mailer was written by Matthias Leuffen http://leuffen.de