DokuWiki

It's better when it's simple

User Tools

Site Tools


devel:mail

This is an old revision of the document!


Handling E-Mails in DokuWiki

DokuWiki contains a wrapper class around PHP's mail function that takes care of the proper encoding of headers and body and also implements sending multipart mails (eg. with HTML body or attachements)

Please refer to the Mailer class documentation for implementation details.

All strings are expected to be valid UTF-8 strings. Email addresses should be given in the form Real Name <mail@example.com> or just mail@example.com. Multiple addresses can be passed comma separated.

Sending Mail

Sending a mail is simple. Here's the most basic call:

$mail = new Mailer();
$mail->to('Some Guy <mail@example.com>');
$mail->subject('A test mail');
$mail->setBody("Hi\n\nthis is your test mail");
$mail->send();

DokuWiki does send Text and HTML mails by default. When you only supply a the first parameter to setBody(), the class will create the HTML body automatically.

Using Replacements

The setBody() function accepts key-value pairs as 3rd and 4th parameter to handle placeholder replacements. Supplying HTML replacements is optional. The HTML replacements will be merged with the text replacements, so you only need to specify those that differ from plain text.

// the text with some placeholders, usually loaded from a language file
$text = 'Hello @NAME@, check out @WEB@!';
 
// the plain text placeholder input
$trep = array(
    'NAME' => 'Some Guy',
    'WEB'  => 'http://www.dokuwiki.org'
);
 
// the HTML placeholder input that differ from the text ones
$hrep = array(
    'WEB'  => '<a href="http://www.dokuwiki.org">DokuWiki</a>'
);
 
// standard mail sending as seen above
$mail = new Mailer();
$mail->to('Some Guy <mail@example.com>');
$mail->subject('A test mail');
$mail->setBody($text, null, $trep, $hrep);
$mail->send();

setBody() parameters changed?

According to setBody documentation, the order of the parameters has changed to

function setBody($text, $textrep = null, $htmlrep = null, $html = null, $wrap = true)

So, in the above example the call of setBody should be

$mail->setBody($text, $trep, $hrep);

Dralli72 2018-03-03 01:01

Checking for valid E-Mail

Use the simple utility function mail_isvalid to check if a given address is a valid email address. The function makes use of the php-email-address-validation library.

devel/mail.1520035719.txt.gz · Last modified: 2018-03-03 01:08 by Dralli72

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki