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

Init

parents
CiviCRM Mailer for WordPress
============================
Once enabled, this plugin sends all WordPress emails through CiviCRM.
Why?
* 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.
Support
-------
Please post bug reports in the issue tracker of this project on Gitlab:
https://lab.civicrm.org/extensions/wp-civicrm-mailer
Commercial support via Coop SymbioTIC:
https://www.symbiotic.coop/en
License
-------
(C) 2020 Mathieu Lutfy <mathieu@symbiotic.coop>
(C) 2020 Coop SymbioTIC <info@symbiotic.coop>
Distributed under the terms of the GNU Affero General public license (AGPL).
See LICENSE.txt for details.
<?php
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();
}
}
}
<?php
namespace WPCiviMailer;
class CivicrmMailerHTTPMailer extends \PHPMailer
{
public $wp_mail_args;
/**
* Constructor.
* @param boolean $exceptions Should we throw external exceptions?
*/
function __construct($exceptions = false)
{
parent::__construct($exceptions);
}
/**
* Send mail using CiviCRM
*/
function send()
{
$recipients = $this->get_recipients();
foreach ($recipients as $recipient) {
$this->mailSingle($recipient);
}
return true;
}
private function mailSingle(string $recipient) {
$contact = null;
$uf_id = null;
civicrm_initialize();
// 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,
])['label'];
list($sent, $activityId) = \CRM_Activity_BAO_Activity::sendEmail(
$formattedContactDetails,
$this->Subject,
$this->AltBody, // text message (often empty)
$this->Body, // html body
NULL,
$default_org_id, // used for the "from"
$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;
}
}
<?php
namespace WPCiviMailer;
/*
Plugin Name: CiviCRM Mailer
Plugin URI: https://lab.civicrm.org/extensions/wp_civicrm_mailer
Description: Sends WordPress emails as CiviCRM emails
Version: 1.0.0
Author: Mathieu Lutfy
Author URI: https://www.symbiotic.coop/en
Copyright 2020 Mathieu Lutfy (mathieu@symbiotic.coop)
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
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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 <http://www.gnu.org/licenses/>.
*/
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