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…


Recent Discussions on this Topic

Need to send multiple emails

Hi Everybody, I need to send multiple emails after form submission, so i have tried the code given below. But its not working and i don't know why this is not working. If anyone came across this problem before or anyone having a better solution for sen…

Form to Email

I'm looking for where I can change how the data from a form submitted to an email can be formatted. I need the value to be on one line. Example: currently the value shows as the following... Name: TJ I need it to show up in the email for ACT li…

sendMail() return value

The sendMail() function does a pretty good job of logging its use, but it seems like it should return a a boolean response indicating if the mail sent, like mail() does. thoughts?

Zend send mail attachments

I have followed this http://bit.ly/J4Ae3c and other examples about using Zend_Mail in Concrete5 for extending mail support, but I have problem sending attachments. Mail is sent, attachment is displayed as icon/name but size is either 0 or "unknown" if li…

Sending email notice to more than one email recipient

I appreciate this tutorial but it is incomplete in it's description assuming the reader knows what files you are talking about and where in the files to put the code. I am using the template user_register_approval_required.php. The email is only sent to t…