Often times you'll need to send an email in a CMS in something you're building, or a contact form, or whatever. Often times you just want to send emails via a script, but have to deal with PHP's messy mail() function, as well as other factors. No longer. Sending mail in in MODx Revolution is a cinch. Just use the modMail class.
I'm sure you'll come across a point in time where you'll want to send an email in a MODx snippet. Well, Revolution provides you with an assistance class to do just that: modMail. Loading it as a service, you can easily and quickly send email to anywhere.
What's nice about modMail is it automatically handles all your mail settings via System Settings. Running mail on an SMTP server? Just go to System Settings, configure the variables there, and it's then configured across the site. You don't need to pass the SMTP stuff into modMail at all. Here's a picture of all the neat settings you can tweak:
Okay, on to our snippet. Let's say I have an email template in a Chunk called 'myEmail':
<p>Hello [[+toName]],</p> <p>Thanks for being [[+adjective]]! It's really snazzy of you to be [[+adjective]]. Keep it up.</p> <p>Sincerely,<br /> [[+fromName]]</p>
Then, I'll make a Snippet called "MailMadLibs":
/* first, get the email with the placeholders in it replaced by the snippet call.
* Note the properties in the snippet call are in the array $scriptProperties.
*/
$message = $modx->getChunk('myEmail',$scriptProperties);
/* now load modMail, and setup options */
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY,$message);
$modx->mail->set(modMail::MAIL_FROM,$scriptProperties['fromEmail']);
$modx->mail->set(modMail::MAIL_FROM_NAME,$scriptProperties['fromName']);
$modx->mail->set(modMail::MAIL_SENDER,$scriptProperties['fromName']);
$modx->mail->set(modMail::MAIL_SUBJECT,$scriptProperties['subject']);
$modx->mail->address('reply-to',$scriptProperties['fromEmail']);
$modx->mail->setHTML(true);
/* specify the recipient */
$modx->mail->address('to',$scriptProperties['toEmail']);
/* send! */
$modx->mail->send();
$modx->mail->reset();
return 'Mail sent.';
Okay, so what happened here? Well, we grabbed our email template from the Chunk, interpreted it's placeholders with the properties from our Snippet call, and then sent it using modMail. Note: if you want to send to more than one address, just put another $modx->mail->address('to') call below that one.
Now I'll call my snippet with this call:
[[!MailMadLibs? &fromName=`Your Friend` &fromEmail=`shaun@splittingred.com` &toName=`Shaun` &toEmail=`shaun@modxcms.com` &adjective=`cool` &subject=`What Good Times We Had` ]]
Simple as that, email Mad Libs!


Comments (7)
MM:
Jul 21, 2010 at 06:05 PM
Shaun,
Great tip, is this on the forum anywhere ? Also would it be possible to use this to parse through an array of collected emails to use as a way of sending newsletters from within modx or is there a plugin for Revolution that would do that?
Regards,
Mark
alyainec:
Nov 06, 2010 at 10:04 AM
Hi Shaun,
Thanks for posting! This is the most easiest step i ever seen.
My problem:
I tried this a few minutes ago but i cannot receive any email on my mailbox.
i put my valid email address (gmail account) on the system settings and same with the calling of snippets.
I am running REVO 2.0.4pl2
I tried also install and use FormIt and just follow the documentation and did not work.
Is there any problem with server? or do i need to reinstall my revo?
Thank you,
Larry
Mac:
Jan 29, 2011 at 06:33 AM
'...&adjective=`cool`..'
this was a really cool tip. Thanks
Mac
Loxzibit:
Feb 04, 2011 at 11:32 AM
This is amazing, I need to learn more PHP and start coding good snippets. Thanks for this and all the great snippets you have wrote Shaun.
David Walker:
Jul 11, 2011 at 11:16 PM
Shaun, great stuff here.. Is it also possible to send generated content this way. Say you wanted to email the previous day's blog posts (headline and content fields). If you created a MODx resource that called them, could you email it?
Peter Allen:
Jan 13, 2012 at 01:04 PM
This is one of the first pages I found when trying to set up emails from modx. Once thing that tripped me up that I'd like to share is that on RHEL 6.2 (SE Linux), there is an SE setting that defaults to banning httpd from making outbound socket connections. You need to run
# setsebool -P httpd_can_network_connect 1
In order to enable the outbound fsockopen() call.
Peter Allen:
Jan 13, 2012 at 02:37 PM
I also noticed that when coding my sending routine, both MAIL_FROM and MAIL_SENDER needed to be the sending email address. In the example above, MAIL_SENDER is the name, whereas I could only get it to work if MAIL_SENDER was an email address. Not sure if this is a typo in the code example, or perhaps something a little odd with Go Daddy SMTP.
Add a Comment