[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/inc/ -> Mailer.class.php (summary)

A class to build and send multi part mails (with HTML content and embedded attachments). All mails are assumed to be in UTF-8 encoding. Attachments are handled in memory so this shouldn't be used to send huge files, but then again mail shouldn't be used to send huge files either.

Author: Andreas Gohr
File Size: 759 lines (26 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

Mailer:: (24 methods):
  __construct()
  attachFile()
  attachContent()
  autoembed_cb()
  setHeader()
  setParameters()
  setBody()
  setHTML()
  setText()
  to()
  cc()
  bcc()
  from()
  subject()
  getCleanName()
  cleanAddress()
  prepareAttachments()
  prepareBody()
  cleanHeaders()
  wrappedHeaderLine()
  prepareHeaders()
  dump()
  prepareTokenReplacements()
  send()


Class: Mailer  - X-Ref

Mail Handling

__construct()   X-Ref
Constructor

Initializes the boundary strings, part counters and token replacements

attachFile($path, $mime, $name = '', $embed = '')   X-Ref
Attach a file

param: string $path  Path to the file to attach
param: string $mime  Mimetype of the attached file
param: string $name The filename to use
param: string $embed Unique key to reference this file from the HTML part

attachContent($data, $mime, $name = '', $embed = '')   X-Ref
Attach a file

param: string $data  The file contents to attach
param: string $mime  Mimetype of the attached file
param: string $name  The filename to use
param: string $embed Unique key to reference this file from the HTML part

autoembed_cb($matches)   X-Ref
Callback function to automatically embed images referenced in HTML templates

param: array $matches
return: string placeholder

setHeader($header, $value, $clean = true)   X-Ref
Add an arbitrary header to the mail

If an empy value is passed, the header is removed

param: string $header the header name (no trailing colon!)
param: string|string[] $value  the value of the header
param: bool   $clean  remove all non-ASCII chars and line feeds?

setParameters($param)   X-Ref
Set additional parameters to be passed to sendmail

Whatever is set here is directly passed to PHP's mail() command as last
parameter. Depending on the PHP setup this might break mailing alltogether

param: string $param

setBody($text, $textrep = null, $htmlrep = null, $html = null, $wrap = true)   X-Ref
Set the text and HTML body and apply replacements

This function applies a whole bunch of default replacements in addition
to the ones specified as parameters

If you pass the HTML part or HTML replacements yourself you have to make
sure you encode all HTML special chars correctly

param: string $text     plain text body
param: array  $textrep  replacements to apply on the text part
param: array  $htmlrep  replacements to apply on the HTML part, null to use $textrep (with urls wrapped in <a> tags)
param: string $html     the HTML body, leave null to create it from $text
param: bool   $wrap     wrap the HTML in the default header/Footer

setHTML($html)   X-Ref
Set the HTML part of the mail

Placeholders can be used to reference embedded attachments

You probably want to use setBody() instead

param: string $html

setText($text)   X-Ref
Set the plain text part of the mail

You probably want to use setBody() instead

param: string $text

to($address)   X-Ref
Add the To: recipients

param: string|string[]  $address Multiple adresses separated by commas or as array
see: cleanAddress

cc($address)   X-Ref
Add the Cc: recipients

param: string|string[]  $address Multiple adresses separated by commas or as array
see: cleanAddress

bcc($address)   X-Ref
Add the Bcc: recipients

param: string|string[]  $address Multiple adresses separated by commas or as array
see: cleanAddress

from($address)   X-Ref
Add the From: address

This is set to $conf['mailfrom'] when not specified so you shouldn't need
to call this function

param: string  $address from address
see: cleanAddress

subject($subject)   X-Ref
Add the mail's Subject: header

param: string $subject the mail subject

getCleanName($name)   X-Ref
Return a clean name which can be safely used in mail address
fields. That means the name will be enclosed in '"' if it includes
a '"' or a ','. Also a '"' will be escaped as '\"'.

param: string $name the name to clean-up
see: cleanAddress

cleanAddress($addresses)   X-Ref
Sets an email address header with correct encoding

Unicode characters will be deaccented and encoded base64
for headers. Addresses may not contain Non-ASCII data!

If @$addresses is a string then it will be split into multiple
addresses. Addresses must be separated by a comma. If the display
name includes a comma then it MUST be properly enclosed by '"' to
prevent spliting at the wrong point.

Example:
cc("föö <foo@bar.com>, me@somewhere.com","TBcc");
to("foo, Dr." <foo@bar.com>, me@somewhere.com");

param: string|string[]  $addresses Multiple adresses separated by commas or as array
return: false|string  the prepared header (can contain multiple lines)

prepareAttachments()   X-Ref
Prepare the mime multiparts for all attachments

Replaces placeholders in the HTML with the correct CIDs

return: string mime multiparts

prepareBody()   X-Ref
Build the body and handles multi part mails

Needs to be called before prepareHeaders!

return: string the prepared mail body, false on errors

cleanHeaders()   X-Ref
Cleanup and encode the headers array


wrappedHeaderLine($key, $val)   X-Ref
Returns a complete, EOL terminated header line, wraps it if necessary

param: string $key
param: string $val
return: string line

prepareHeaders()   X-Ref
Create a string from the headers array

returns: string the headers

dump()   X-Ref
return a full email with all headers

This is mainly intended for debugging and testing but could also be
used for MHT exports

return: string the mail, false on errors

prepareTokenReplacements()   X-Ref
Prepare default token replacement strings

Populates the '$replacements' property.
Should be called by the class constructor

send()   X-Ref
Send the mail

Call this after all data was set

triggers: MAIL_MESSAGE_SEND
return: bool true if the mail was successfully passed to the MTA