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` &fromEmailemail@example.com` &toName=`Shaun` &toEmailfirstname.lastname@example.org` &adjective=`cool` &subject=`What Good Times We Had` ]]
Simple as that, email Mad Libs!