DokuWiki

It's better when it's simple

User Tools

Site Tools


devel:mail

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 = [
    'NAME' => 'Some Guy',
    'WEB'  => 'https://www.dokuwiki.org'
];
 
// the HTML placeholder input that differ from the text ones
$hrep = [
    'WEB'  => '<a href="https://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, $trep, $hrep);
$mail->send();

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.txt · Last modified: 2024-02-29 14:06 by 190.153.116.145

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