Sending Mail is accomplished with the Mail helper. This helper allows you to compose a message, specify one or multiple recipients, create a subject, load message text dynamically or from a template, and send the mail. Mail will automatically be sent using the built-in mail settings (which can specify a local mail server or an external SMTP server.) As of concrete5 5.3.3 the Mail Helper is a simple wrapper for the Zend_Mail object.
Loading the Helper
$mh = Loader::helper('mail');
Simple Example
$mh->setSubject(‘Simple Message’);
$mh->setBody(‘This is my simple message body.’);
$mh->to(‘andrew@this-is-a-test-do-not-email.com’, ‘Andrew Embler’)
$mh->to(‘second-recipient@second-domain.com’);
$mh->from(‘noreply@concrete5.org’);
$mh->sendMail();
Loading email content from an external template.
You may load email content from a template, which will need to be located in the local mail/ directory. These templates are simply a collection of PHP variables like $body, $subject, and $from. Additional PHP variables can be specified within these variables and then passed through the MailHelper::addParameter method.
Example of the forgot_password.php mail template
//php code
$subject = t("Forgot Password");
$body = t("
Dear %s,
You have requested a new password for the site %s
Your username is: %s
You may change your password at the following address:
%s
Thanks for browsing the site!
", $uName, SITE, $uName, $changePassURL);
Sending mail using this template
$mh->from(‘forgotpassword@concrete5.org’);
$mh->to('site-user@example.com');
$mh->addParameter(‘uName’, ‘aembler’);
$mh->addParameter(‘changePassURL’, ‘http://www.mysite.com/change/password’);
$mh->load(‘forgot_password’);
$mh->sendMail();
Methods
$r = $mh->getMailerObject()
Returns an associative array containing the Zend Mailer objects. The ‘mail’ key of the array contains the Zend_Mail object, and the ‘transport’ key of the array contains the Zend_Mail_Transport object.
$mh->addParameter($key, $value)
Adds a parameter to a mail template.
$mh->load($mailTemplate, $pkgHandle = null)
Loads a mail template file. First checks the local directory, then a packages directory (if pkgHandle is specified), then the core directory.
$mh->setBody($body)
Sets the body of an email message if you don’t want to load it using load() method.
$mh->setSubject($subject)
Sets the subject of an email message.
$subject = $mh->getSubject()
Gets the current subject of the email message.
$body = $mh->getBody()
Gets the current body of an email message.
$mh->enableEmailResponseProcessing()
Sets up mail importer validation and response processing for a given mail message. You can learn more about this in the _Mail Importers Section_ (link to mail importers in system.)
$mh->from($email, $name = null)
Sets the from email address and name for the email message.
$mh->to($email, $name = null)
Sets a recipient for the email message. to() may be called multiple times on the same instance of the mail helper.
$mh->sendMail()
Sends the mail message. Takes care of logging emails, setting up subjects from a loaded mail helper, etc…