Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the pue-sales domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/staging-poc/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the better-click-to-tweet domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/staging-poc/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the pue-amazon domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/staging-poc/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the pue-stats domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/staging-poc/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the wordpress-seo domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/staging-poc/public_html/wp-includes/functions.php on line 6114
Useful code: Send bulk email to attendees using mandrill | Event Espresso - Staging Server

Support

Home Forums Custom Files Useful code: Send bulk email to attendees using mandrill

Useful code: Send bulk email to attendees using mandrill

Posted: December 16, 2013 at 11:58 pm

Viewing 2 reply threads


Nicolas Alberto

December 16, 2013 at 11:58 pm

Hi,
I have had many issues because of timeout in sending emails to event attendees.
I can now send 800 emails in one api call using the solution below and no timeout in my application.

My solution was to use mandrill to send all email in one api call.
You just need to create an account with Mandrill (part of mailchimp).
You will be allowed to send 12 000 emails per month for free.

You also need to create an email template in Mandrill. That will also make all your email look nice (html) and have link tracking capabilities.

Then install the wpMandrill addon in wp and use the following custom function in your custom_function.php file in eventespresso (change the parameters as needed)

All your worpress email will then be sent through mandrill.

<code>
function espresso_event_reminder($event_id, $email_subject=&#039;&#039;, $email_text=&#039;&#039;, $email_id=0) {
	global $wpdb, $org_options;
	do_action(&#039;action_hook_espresso_log&#039;, __FILE__, __FUNCTION__, &#039;&#039;);
	$count = 0;
	$SQL = &#039;SELECT * FROM &#039; . EVENTS_ATTENDEE_TABLE . &#039; WHERE event_id =%d GROUP BY lname, fname&#039;;
	//echo ($SQL);
	//exit;
	
	$attendees = $wpdb-&gt;get_results( $wpdb-&gt;prepare( $SQL, $event_id ));
	echo (&quot;sending email... &lt;br&gt;&quot;);

	//initiate arrays for later use
	$tmv = array();
	$tto = array();

	if ($wpdb-&gt;num_rows &gt; 0) {
		foreach ($attendees as $attendee) {
			$attendee_id = $attendee-&gt;id;

			$attemail =	array(&#039;attendee_id&#039; =&gt; $attendee_id, &#039;send_admin_email&#039; =&gt; &#039;false&#039;, &#039;send_attendee_email&#039; =&gt; &#039;true&#039;, &#039;custom_data&#039; =&gt; array(&#039;email_type&#039; =&gt; &#039;reminder&#039;, &#039;email_subject&#039; =&gt; $email_subject, &#039;email_text&#039; =&gt; $email_text, &#039;email_id&#039; =&gt; $email_id));
			extract($attemail);
			$multi_reg = empty( $multi_reg ) ? FALSE :  $multi_reg;
			$custom_data = empty( $custom_data ) ? &#039;&#039; :  $custom_data;
			$data = espresso_prepare_email_data($attendee_id, $multi_reg, $custom_data);
			$usermergedata = array();
			$temail = espresso_prepare_email($data);

			//if email invalid skip sending
			if (!filter_var($temail[&#039;send_to&#039;], FILTER_VALIDATE_EMAIL)) {
				continue;
			}

			$usermergedata[] = array(&#039;name&#039; =&gt; &#039;USERCONTENT&#039;, &#039;content&#039; =&gt; nl2br($temail[&#039;email_body&#039;]));
			$tmv[] = array(
							&#039;rcpt&#039; =&gt; $temail[&#039;send_to&#039;],
							&#039;vars&#039; =&gt; $usermergedata
						);
			$tto[] = array(
							&#039;email&#039; =&gt; $temail[&#039;send_to&#039;],
							&#039;name&#039; =&gt; &#039;&#039;
						);
			$tsubject = $temail[&#039;email_subject&#039;];
			$count++;
		}

		//start preparing mandrill send
			$email_text = &#039;*|USERCONTENT|*&#039;;
			$message = array(
				&#039;subject&#039; =&gt; $tsubject,
				&#039;from_name&#039; =&gt; &#039;Sender Name&#039;,
				&#039;from_email&#039; =&gt; &#039;user@yourdomain.com&#039;,
				&#039;to&#039; =&gt; $tto,
				&#039;merge&#039; =&gt; true,
				&#039;merge_vars&#039; =&gt; $tmv,
				&#039;html&#039; =&gt; $email_text
			);
			
			$mandridresult = wpMandrill::sendEmail(
				$message,
				$tags = array(&#039;Email Event Attendees&#039;),
				$template_name = &#039;yourtemplatenameinmandrill&#039;
			);
			echo (&#039;&lt;pre&gt;&#039;);
			print_r($mandridresult);
			echo (&#039;&lt;/pre&gt;&#039;);

		?&gt;
		&lt;div id=&quot;message&quot; class=&quot;updated fade&quot;&gt;
			&lt;p&gt;&lt;strong&gt;
					&lt;?php echo printf( _n(&#039;Email Sent to %d person successfully.&#039;, &#039;Email Sent to %d people successfully.&#039;, $count, &#039;event_espresso&#039;), $count ); ?&gt;
				&lt;/strong&gt;&lt;/p&gt;
		&lt;/div&gt;
		&lt;?php
		return;
	} else {
		?&gt;
		&lt;div id=&quot;message&quot; class=&quot;error fade&quot;&gt;
			&lt;p&gt;&lt;strong&gt;
					&lt;?php _e(&#039;No attendee records available.&#039;, &#039;event_espresso&#039;); ?&gt;
				&lt;/strong&gt;&lt;/p&gt;
		&lt;/div&gt;
		&lt;?php
	}
}</code>

I hope this will be useful for someone.


Nicolas Alberto

December 17, 2013 at 12:00 am

<pre class=”brush: php; gutter: true; first-line: 1; highlight: []; html-script: false”>function espresso_event_reminder($event_id, $email_subject='', $email_text='', $email_id=0) {
global $wpdb, $org_options;
do_action('action_hook_espresso_log', __FILE__, __FUNCTION__, '');
$count = 0;
$SQL = 'SELECT * FROM ' . EVENTS_ATTENDEE_TABLE . ' WHERE event_id =%d GROUP BY lname, fname';
//echo ($SQL);
//exit;

$attendees = $wpdb->get_results( $wpdb->prepare( $SQL, $event_id ));
echo ("sending email… <br>");

//initiate arrays for later use
$tmv = array();
$tto = array();

if ($wpdb->num_rows > 0) {
foreach ($attendees as $attendee) {
$attendee_id = $attendee->id;

$attemail = array('attendee_id' => $attendee_id, 'send_admin_email' => 'false', 'send_attendee_email' => 'true', 'custom_data' => array('email_type' => 'reminder', 'email_subject' => $email_subject, 'email_text' => $email_text, 'email_id' => $email_id));
extract($attemail);
$multi_reg = empty( $multi_reg ) ? FALSE : $multi_reg;
$custom_data = empty( $custom_data ) ? '' : $custom_data;
$data = espresso_prepare_email_data($attendee_id, $multi_reg, $custom_data);
$usermergedata = array();
$temail = espresso_prepare_email($data);

//if email invalid skip sending
if (!filter_var($temail['send_to'], FILTER_VALIDATE_EMAIL)) {
continue;
}

$usermergedata[] = array('name' => 'USERCONTENT', 'content' => nl2br($temail['email_body']));
$tmv[] = array(
'rcpt' => $temail['send_to'],
'vars' => $usermergedata
);
$tto[] = array(
'email' => $temail['send_to'],
'name' => ''
);
$tsubject = $temail['email_subject'];
$count++;
}

//start mandrill send
$email_text = '*|USERCONTENT|*';
$message = array(
'subject' => $tsubject,
'from_name' => 'Joondalup Events',
'from_email' => 'noreply@joondalup.wa.gov.au',
'to' => $tto,
'merge' => true,
'merge_vars' => $tmv,
'html' => $email_text
);

$mandridresult = wpMandrill::sendEmail(
$message,
$tags = array('Email Event Attendees'),
$template_name = 'events.joondalup.wa.gov.au'
);
echo ('');
print_r($mandridresult);
echo ('’);

//stop mandrill send
?>
<div id=”message” class=”updated fade”>
<p>
<?php echo printf( _n(‘Email Sent to %d person successfully.’, ‘Email Sent to %d people successfully.’, $count, ‘event_espresso’), $count ); ?>
</p>
</div>
<?php
return;
} else {
?>
<div id=”message” class=”error fade”>
<p>
<?php _e(‘No attendee records available.’, ‘event_espresso’); ?>
</p>
</div>
<?php
}
}


Dean

  • Support Staff

December 17, 2013 at 12:37 am

Thanks for providing your solution!

Viewing 2 reply threads

The support post ‘Useful code: Send bulk email to attendees using mandrill’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso - Staging Server