Sending emails

Last update by Elxis Team

Use elxis framework to easily send emails

Elxis uses the swift mailer for sending messages (emails). You can send either html or plain text emails easily via Elxis framework's by using sendmail method. Sendmail accepts the following parameters:

  • (string) $subject The email subject, required
  • (string) $body The message body (html or plain text, required)
  • (string) $alt_body='' In case $body is html you can provide an alternative plain text body (optional)
  • (mixed) $attachment=null The absolute path to a file to be attached or an array of files (optional)
  • (mixed) $type='plain' html or plain (optional, defauls to plain)
  • (mixed) $to=null Recipient email or array of recipients (optional, defaults to MAIL_EMAIL / MAIL_NAME configuration variables) [1]
  • (mixed) $cc=null Carbon copy (CC) recipient email or array of recipients (optional) [1]
  • (mixed) $bcc=null Blind carbon copy (BCC) recipient email or array of recipients (optional) [1]
  • (string) $from=null Sender's email and name (optional, defaults to MAIL_FROM_EMAIL / MAIL_FROM_NAME configuration variables) [2]
  • (int) $priority=3 Message priority (1: highest, 5: lowest)
  • (bool) $debug=false If true sendmail will return a string version of the message.

Sendmail will return true on success or false on failure.

Examples

1. Simple plain text message to the default site's recipient.

eFactory::getElxis()->sendmail ('Test', 'This is a sample message');

 

2. Simple plain text message to multiple recipients.

$to = array ('john@example.com', 'mary@example.com,Mary Golden');
eFactory::getElxis()->sendmail ('Test', 'This is a sample message', '', null, 'plain', $to);

 

3. Multi-part message with attachments, custom sender and mutiple recipients.

$subject = 'Sample message with attachments';
$html_body = '<p>Hello <strong>world</strong>!</p>'; 
$text_body = 'Hello world!'; 
$files = array( ELXIS_PATH.'/media/images/flower.jpg', ELXIS_PATH.'/media/images/dog.png' ); 
$to = array('john@example.com', 'mary@example.com,Mary Golden'); 
$from = 'tester@test.com,Joe Doe'; 
eFactory::getElxis()->sendmail ($subject, $html_body, $text_body, $files, 'html', $to, null, null, $from);

 

Notes

  1. The parameter accepts as values a string or an array of strings. Each string can be an email address, or an email address and a name separated with comma (,).
    $to = 'john.doe@example.com ';
    $to = 'john.doe@example.com,John Doe';
    $to = array('john.doe@example.com', 'jack.daniels@example.com,Jack daniels');
  2. The parameter accepts as values an email address, or an email address and a name separated with comma (,).
    $from = 'mary.poppins@example.com';
    $from = 'mary.poppins@example.com,Mary Poppins';
It has been read 4659 times
Modules development guide
Previous article
Modules development guide

You are free to copy, distribute and transmit the articles in this site for non-commercial purposes.
Creative Commons 3.0