Commit 35864520 authored by bgm's avatar bgm Committed by Aegir user
Browse files


CiviCRM Mailer for WordPress
Once enabled, this plugin sends all WordPress emails through CiviCRM.
* It logs all user emails to their contact record, which can be useful (welcome emails, password resets).
* If using a third-party email service, you can configure only it once (in CiviCRM) and ignore WordPress settings.
Not impemented:
* Emails in cc/bcc are ignored.
* Attachments are ignored.
The main use-case is for fairly simple WordPress sites that mostly send user welcome emails, password resets
or simple receipts.
Please post bug reports in the issue tracker of this project on Gitlab:
Commercial support via Coop SymbioTIC:
(C) 2020 Mathieu Lutfy <>
(C) 2020 Coop SymbioTIC <>
Distributed under the terms of the GNU Affero General public license (AGPL).
See LICENSE.txt for details.
namespace WPCiviMailer;
class CivicrmMailer
public function civicrm_mailer_init($args)
$new_wp_mail = array(
'to' => $args['to'],
'subject' => $args['subject'],
'message' => $args['message'],
'headers' => $args['headers'],
'attachments' => $args['attachments'],
global $phpmailer;
if (!$phpmailer instanceof CivicrmMailerHTTPMailer) {
$phpmailer = new CivicrmMailerHTTPMailer();
namespace WPCiviMailer;
class CivicrmMailerHTTPMailer extends \PHPMailer
public $wp_mail_args;
* Constructor.
* @param boolean $exceptions Should we throw external exceptions?
function __construct($exceptions = false)
* Send mail using CiviCRM
function send()
$recipients = $this->get_recipients();
foreach ($recipients as $recipient) {
return true;
private function mailSingle(string $recipient) {
$contact = null;
$uf_id = null;
// First try to fetch the WP user by email
$user = get_user_by('email', $recipient);
if ($user && $user->ID) {
$uf_id = $user->ID;
if ($uf_id) {
try {
$uf = civicrm_api3('UFMatch', 'getsingle', [
'uf_id' => $uf_id,
$contact = civicrm_api3('Contact', 'getsingle', [
'id' => $uf['contact_id'],
catch (Exception $e) {
// The uf_match might not exist yet.
// For example: user account creation through a CiviCRM profile.
// This is OK, the code below will fetch by email.
// The user might not exist.
// Find a matching email in CiviCRM, prioritize primary emails.
if (empty($contact)) {
$dao = CRM_Core_DAO::executeQuery('SELECT contact_id FROM civicrm_email WHERE email = %1 AND contact_id IS NOT NULL ORDER BY is_primary DESC', [
1 => [$recipient, 'String'],
if ($dao->fetch()) {
$contact = civicrm_api3('Contact', 'getsingle', [
'id' => $dao->contact_id,
// Let's create a contact record
if (empty($contact)) {
$contact = civicrm_api3('Contact', 'create', [
'contact_type' => 'Individual',
'email' => $recipient,
// Only a failsafe, should not happen because of the above.
if (empty($contact['id'])) {
throw new Exception("Could not find user for {$recipient}");
$formattedContactDetails = [];
$formattedContactDetails[] = $contact;
// Fetch the default organisation for the domain
$domain_id = \CRM_Core_Config::domainID();
$default_org_id = \CRM_Core_DAO::singleValueQuery('SELECT contact_id FROM civicrm_domain WHERE id = %1', [
1 => [$domain_id, 'Positive'],
// Send message from CiviCRM default from
// We prefer not to use the domain email, because that is the public-facing email
// and might not be an allowed email for sending.
$from = civicrm_api3('OptionValue', 'getsingle', [
'option_group_id' => 'from_email_address',
'is_default' => 1,
list($sent, $activityId) = \CRM_Activity_BAO_Activity::sendEmail(
$this->AltBody, // text message (often empty)
$this->Body, // html body
$default_org_id, // used for the "from"
return TRUE;
public function isMail()
$this->Mailer = 'CiviCRM WP Mailer';
protected function get_recipients()
$recipients = [];
foreach ($this->to as $to) {
// use only address
$recipients[] = $to[0];
/* Not implemented
// include bcc to recipients
$recipients = array_merge($recipients, $this->get_bcc($recipients_header_to));
// include cc to recipients, they need to included in recipients and in headers (refer to get_headers method)
$recipients = array_merge($recipients, $this->get_cc($recipients_header_to));
return $recipients;
namespace WPCiviMailer;
Plugin Name: CiviCRM Mailer
Plugin URI:
Description: Sends WordPress emails as CiviCRM emails
Version: 1.0.0
Author: Mathieu Lutfy
Author URI:
Copyright 2020 Mathieu Lutfy (
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <>.
if (!defined('ABSPATH')) exit();
define('WPCIVIMAILER_PLUGIN_DIR', plugin_dir_path(__FILE__));
require_once ABSPATH . WPINC . '/class-phpmailer.php';
require_once(plugin_dir_path(__FILE__) . 'civicrm_mailer.class.php');
require_once(plugin_dir_path(__FILE__) . 'civicrm_mailer.http.class.php');
$mailer = new CivicrmMailer();
add_filter('wp_mail', array($mailer, 'civicrm_mailer_init'), 100);
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment