Sugar Mailer
This is a little mail plugin that contains:, (*1)
- SMTP plugin wrapper
- easily send plain text, html or both text & html hybrid content mails
- convenient methods to add one or multiple recipients
- encode special chars for mails with ISO charset
- ping and jump methods for tracking read and click events in your html mails
- save mails as files to disk
Getting started
This plugin is configurable via config file:, (*2)
[mailer]
; smtp config
smtp.host = smtp.domain.com
smtp.port = 25
smtp.user = info@domain.com
smtp.pw = 123456789!
; scheme could be SSL or TLS
smtp.scheme =
; optional mail settings
from_mail = noreply@domain.com
from_name = Mario Bros.
; mail to receive bounced mails
errors_to = bounce@domain.com
; used mail for replies to the sent mail
reply_to = info@domain.com
; handler for SMTP errors
on.failure = \Controller\Mail::logError
; handler for tracing opened mails
on.ping = \Controller\Mail::traceMail
; handler for redirecting jump links
on.jump = \Controller\Mail::traceClick
; automatically create jump links in all <a> tags
jumplinks = true
; path for storing mail dumps
storage_path = logs/mail/
Usage
A little sample looks like this:, (*3)
function send_test($email, $title=null) {
$mail = new \Mailer();
$mail->addTo($email, $title);
$mail->setText('This is a Test.');
$mail->setHTML('This is a <b>Test</b>.');
$mail->send('Test Mail Subject');
}
If you want, you can change the encoding type that is used for the email body and header when instantiating the mail object with a constructor argument:, (*4)
$mail = new \Mailer('UTF-8'); // default
$mail = new \Mailer('ISO-8859-1');
$mail = new \Mailer('ISO-8859-15');
Tracking
To initialize the tracking routes, call this before $f3->run()
:, (*5)
$f3->config('mailer_config.ini');
// ...
Mailer::initTracking();
// ...
$f3->run();
To add the ping tracking pixel (1x1 transparent 8bit PNG), put this in your html mail body:, (*6)
<img src="http://mydomain.com/mailer-ping/AH2cjDWb.png" />
The file name should be a unique hash you can use to identify the recipient who read your mail., (*7)
The tracking methods could look like this:, (*8)
static public function logError($mailer, $log) {
$logger = new \Log('logs/smtp_'.date('Y_m_d').'.log');
$logger->write($log);
}
static public function traceMail($hash) {
// your mail $hash is being read
}
static public function traceClick($target) {
// someone clicked $target link
}
Mock & Storage
In case you don't want to actually send the email, but just want to run a test flight and save the mail in a text file, you can mock the server dialog:, (*9)
$mail->send($subject, TRUE); // mock call
$mail->save('newsletter.eml'); // save to file in 'mailer.storage_path' directory
$mail->reset();
If you want to keep using the object after a mock call, you need to reset the mailer and add recipients, content and attachments again., (*10)
The mail file includes all file attachments., (*11)
Logging
You can log the full SMTP server dialog after sending the email. This could be useful for debugging purposes or as a sending confirmation., (*12)
$success = $mailer->send($subject);
$f3->write('SMTP_mail.log', $this->mailer->log());
Notice: By default, the log level is verbose
, which means it also contains the mail body and attachments, which might eat up a lot of memory.
To reduce the log level, set $log
to TRUE
(dialog only) or FALSE
(disabled) in:, (*13)
$mailer->send($subject, $mock, $log);
Keep in mind that when you write down mails to files, it can only store what was found in the SMTP log, hence it only works when logging level is verbose
., (*14)
Demo & Testing
There's a test bench available here: https://github.com/ikkez/f3-mailer/tree/test, (*15)
API
addBcc
Adds a blind carbon copy recipient., (*16)
addBcc($email, $title=null)
, (*17)
addCc
Adds a carbon copy recipient., (*18)
addCc($email, $title=null)
, (*19)
addTo
Adds a direct recipient.
addTo($email, $title=null)
, (*20)
attachFile
Adds a file attachment., (*21)
attachFile($path, $alias=null, $cid=null)
, (*22)
initSMTP
Initializes SMTP plugin. Useful if you want to reuse the Mailer object but with a fresh SMTP adapter beneath. It's possible to change options before, i.e. to use a different smtp server., (*23)
initTracking
This registers the required routes to F3, (*24)
log
Returns SMTP log, (*25)
reset
Reset recipients if key was given, or restart whole smtp plugin., (*26)
($key=null)
, (*27)
save
Save the send mail to disk, (*28)
save($filename)
, (*29)
send
Send message, (*30)
send($subject [, $mock = false [, $log = 'verbose']])
, (*31)
log level options: FALSE
, TRUE
, 'verbose'
, (*32)
set
Set encoded header value, (*33)
set($key, $val)
, (*34)
setContent
Set message contents by mime type, (*35)
setContent($data [, $mime [, $charset=NULL ]])
, (*36)
I.e. for AMP mails:, (*37)
$mailer->setContent($amp,'text/x-amp-html');
, (*38)
setErrors
Set receipient for bounce error mails, (*39)
setErrors($email [, $title=null])
, (*40)
setFrom
Set message sender, (*41)
setFrom($email [, $title=null])
, (*42)
setHTML
set message in HTML text format, (*43)
setHTML($message)
, (*44)
setReply
set reply-to field respected by most email clients, (*45)
setReply($email [, $title=null])
, (*46)
setText
set message in plain text format, (*47)
setText($message)
, (*48)
License
You are allowed to use this plugin under the terms of the GNU General Public License version 3 or later., (*49)
Copyright (C) 2022 Christian Knuth [ikkez], (*50)