How to override the Send() function in PHPMailer

I want to override the send() function in SugarPHPMailer.

This is what I have:

custom/modules/Administration/JJW_MSGraphMailer/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ TokenStore.php          # Manages OAuth tokens in database
β”‚   β”œβ”€β”€ GraphMailer.php         # Sends emails via Microsoft Graph API
β”‚   β”œβ”€β”€ Helpers.php             # Utility functions (config, logging)
β”‚   └── Installer.php           # Creates database tables
└── autoload.php                # Class autoloader
custom/modules/Administration/views/
└── view.msgraphmailer.php      # Admin configuration page
                                # - OAuth setup
                                # - User management  
                                # - Test emails
                                # - Send logs
custom/include/SugarPHPMailer/
└── SugarPHPMailer.php          # Overrides SuiteCRM's email system
                                # - Intercepts all system emails
                                # - Routes through GraphMailer
                                # - No SMTP fallback
custom/Extension/application/Ext/Include/
└── CustomMailerLoader.php      # Forces SuiteCRM to load custom mailer

jjw_msgraph_tokens              # Stores OAuth access/refresh tokens
jjw_msgraph_send_log           # Logs all email attempts
config (category: JJW_MSGraphMailer)  # Settings storage
Admin β†’ MSGraphMailer           # Configuration interface
System emails β†’ GraphMailer    # Automatic routing
Test emails β†’ GraphMailer      # Direct API calls

SuiteCRM Email β†’ SugarPHPMailer β†’ GraphMailer β†’ Microsoft Graph API β†’ Recipient
                      ↓
                 Send Log Database


I have everythign working great. I still have to set up a cron job to send keepalive emails. I came up with this idea with my webpage when I created a WP plugin to use MS GraphMail and it has been working great for months. I can send test emails from my admin settings page but I cant send system default emails as I cannot seem to override the SugarPHPMailer Send function. I have my own send function that users GraphMailer.

How do I override Send in SugarPHPMailer.

  I thought that the class_override.php that I have in custom/include/SugarPHPMailer would do this. it contains: <?php
$GLOBALS['SugarPHPMailerOverride'] = 'SugarPHPMailer'; 

I also have SugarPHPMailer.php in custom/include.SugarPHPMailer. that has this: 

<?php
// File: custom/include/SugarPHPMailer/SugarPHPMailer.php
// Custom SugarPHPMailer that routes emails through Microsoft Graph


require_once 'include/SugarPHPMailer.php';
require_once 'custom/modules/Administration/JJW_MSGraphMailer/src/TokenStore.php';
require_once 'custom/modules/Administration/JJW_MSGraphMailer/src/GraphMailer.php';

use JJW_MSGraphMailer\TokenStore;
use JJW_MSGraphMailer\Src\GraphMailer;  

class SugarPHPMailer extends PHPMailer
{
    public function Send()
    { ...

Can anyone help me out?

Your formatting is a bit off… Hard to understand

My question is: you want to override it where, for what function?

That class is used in like 20 places, and the answer is not the same for all of them…

Hey @johnwreford
I think your custom send function is almost ready β€” but SuiteCRM might not be loading it due to how it handles overrides.

Can you:

  • Confirm that custom/include/SugarPHPMailer/SugarPHPMailer.php is extending PHPMailer?
  • Make sure custom/Extension/application/Ext/Include/CustomMailerLoader.php sets $GLOBALS['SugarPHPMailerOverride'] = 'SugarPHPMailer';
  • Run Quick Repair and Rebuild

Also try logging or dying inside your custom Send() method to confirm it’s being triggered.

Thank you for the feedback. The override is not working. This is what I have:

custom/Extension/application/Ext/Include/CustomMailerLoader.php

<?php

if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

// Force SuiteCRM to use our custom mailer instead of the default

require_once('custom/include/SugarPHPMailer/CustomPHPMailer.php');

// Tell SuiteCRM to use our custom class instead of the original

$GLOBALS['SugarPHPMailerOverride'] = 'SugarPHPMailer';

custom/include/SugarPHPMailer/CustomPHPMailer.php


<?php
if (!defined('sugarEntry') || !sugarEntry) {
    die('Not A Valid Entry Point');
}

// Load the original SugarPHPMailer first
require_once('include/SugarPHPMailer.php');

// Load our Graph Mailer components
require_once('custom/modules/Administration/JJW_MSGraphMailer/src/GraphMailer.php');
require_once('custom/modules/Administration/JJW_MSGraphMailer/src/TokenStore.php');

use JJW_MSGraphMailer\Src\GraphMailer;
use JJW_MSGraphMailer\TokenStore;

/**
 * Custom PHPMailer that routes emails through Microsoft Graph
 */
class CustomPHPMailer extends SugarPHPMailer
{
    public function send()  // Note: lowercase 'send' to match parent
    {

I think part of the issues is this:

SuiteCRM 7.11.XX’s SugarPHPMailer does not support class overrides natively, even if you define:

$GLOBALS['SugarPHPMailerOverride'] = 'CustomPHPMailer';

There’s no logic in SugarPHPMailer or its consumers (like EmailHandler or OutboundEmail) that checks for that variable when instantiating the mailer.

@johnwreford
Yeah, you’re right β€” SuiteCRM 7.11.x doesn’t actually support overriding SugarPHPMailer through $GLOBALS['SugarPHPMailerOverride']. That variable isn’t checked anywhere when the mailer is created, so it gets ignored.

One way around this is to patch the core files where SugarPHPMailer is used (like EmailHandler, etc.) to load your custom class instead. Just replace new SugarPHPMailer() with something like:

$mailerClass = $GLOBALS['SugarPHPMailerOverride'] ?? 'SugarPHPMailer';
$mailer = new $mailerClass();

If patching core isn’t ideal, you could also hook into the email send process and manually reroute it to your class β€” not perfect, but it works.