FormIt. Multiple (mass) personally email sending

Hello,
How to parse emails from resources those has TV’s “email_request” and send personally email?

I have 1 contact form:

  1. Location (TV in resource) - select with 2 cities (ex., London, Brussels).
  2. Name
  3. Phone
  4. You request
  5. Submit button

This is emails parser hook “parseEmails”:
<?php

//$modx->cacheManager->refresh();
/and $doc->getTVValue(‘city’) == $_GET[‘location’]/

$array_ids = $modx->getChildIds(2, 1, array(‘context’ => ‘web’,));

$docs = $modx->getCollection(‘modResource’, array(
‘id:IN’ => $array_ids,
‘template’ => 2,
));

foreach ($docs as $doc) {
if ($doc->getTVValue(‘email_req’)) {
$parsedEmails = $doc->getTVValue(‘email_req’) . ‘,’;
}
$hook->formit->config[‘emailTo’] = $parsedEmails;

}
return true;

Call snippet:
{$_modx->runSnippet(’!AjaxForm’, [
‘snippet’ => ‘FormIt’,
‘form’ => ‘@FILE chunks/forms/emailForm.tpl’,
‘preHooks’ => ‘’,
‘hooks’ => ‘parseEmails,email’,
‘emailTpl’ => ‘@FILE chunks/forms/emailFormReport.tpl’,
‘emailSubject’ => ‘Message at [[+test]]’,
‘validate’ => ‘’,
])}

Questions:

  1. How to parse all resources by Location (by London) and get email from TV “email_request”?
  2. How to send personally this form to all parsed email?

Tnx

Here is some example code, how you could query resources by the value of a TV:

//read the submitted value 
$location = $hook->getValue('location');

$q = $modx->newQuery('modResource', array(
    'template' => 2
));
//change 'location' to the actual name of your TV
$q->leftJoin('modTemplateVar', 'tvDefault', array(
    "tvDefault.name" => 'location'
));
$q->leftJoin('modTemplateVarResource', 'tv', array(
    "tv.contentid = modResource.id",
    "tv.tmplvarid = tvDefault.id",
));
$q->where(array('tv.value' => $location));

$docs = $modx->getCollection('modResource',$q);
foreach ($docs as $doc) {
    ...
}

To send the emails you could use modMail. There is some example code under this link:
https://docs.modx.com/current/en/extending-modx/services/modmail

1 Like

You might want to take a look at the mailgunX class that comes packaged with Notify.

Rather than sending a separate email to each selected user, it packages them and sends them off to MailGun, which sends them on the users. The cost is trivial unless you’re sending thousands of them (three free months, then 80 cents for each 1,000 emails). In many months of sending emails at Bob’s Guides, I think they’ve charged me a dollar or two.

It takes some time to set up, but you’ll get much better deliverability (they won’t end up in the spam folder), and MailGun will give you some great analytics on hard and soft bounces, opens, and clicks.

1 Like

Hello Bob and halftrainedharry,
Thank you very much! It’s very helpful information for me.

Grts