Reputation: 148
I have a plugin that uses wp_mail()
function to send emails on form errors. I have WP Mail SMTP plugin installed as well to use my custom SMTP settings.
Everything was working great for months with WP Mail SMTP version 0.11.1. But ever since I updated the plugin to version 1.4.2, my emails have stopped working.
From what I have figured out, wp_mail()
is not working inside my plugin only. If I keep it anywhere like in theme files etc, the email is sent immediately. But from inside my plugin, I get this exception every time:
"errors": {
"wp_mail_failed": [
"Could not instantiate mail function."
]
},
"error_data": {
"wp_mail_failed": {
"to": [
"[email protected]"
],
"subject": "Form Error",
"message": "<dl><dt>Error Logged:<\/dt> <dd>{\"MembershipNumber\":null,\"Success\":false,\"Message\":\"The combination is incorrect\",\"contactInfo\":{\"PrimaryContactNumber\":null,\"AlternateNumber\":null,\"MobileNumber\":null,\"OtherPhone1\":null,\"Email\":null},\"membership\":{\"EffectiveDate\":null,\"ExpiryDate\":null,\"planInfo\":null,\"MembershipSubProgram\":null},\"address\":{\"HomeAddress\":null,\"MailingAddress\":null,\"BillingAddress\":null},\"slxConstantInfo\":[],\"Token\":null}<\/dd><dt>Timestamp:<\/dt> <dd>Monday, April 22nd, 2019 @ 03:16:30 PM<\/dd><dt>Referrer:<\/dt> <dd>renew\/step1<\/dd><dt>User:<\/dt> <dd> \n<br>\n<br>\n<br>\n<\/dd><\/dl>",
"headers": [
],
"attachments": [
],
"phpmailer_exception_code": 2
}
}
}
If I downgrade the WP Mail SMTP plugin, things start working fine again. So its a sure shot issue with the plugin. Maybe in my plugin where I call the wp_mail()
function, the settings of WP Mail SMTP are not already loaded or something like that.
Any quick help will be really appreciated since I have this code running on a production site. Thanks in advance!
Edit: Just to add some details, WP Mail SMTP test email is running fine !
Upvotes: 3
Views: 1993
Reputation: 148
Never mind. I posted on WP MAIL SMTP support forum but got no response. I figured out that the phpmailer
object is not holding the modified setting inside my plugin. So this is the work-around I implemented just to make things work, though I know this is not the best fix.
I placed the following action in my plugin's init:
/**
* Reconfigure SMTP setting to make WP MAIL SMTP plugin work
*/
add_action( 'phpmailer_init', 'reconfigure_smtp' );
function reconfigure_smtp( $phpmailer ) {
$SMTPhost = get_option('smtp_host');
$SMTPport = get_option('smtp_port');
$FromEmail = get_option('mail_from');
$FromName = get_option('mail_from_name');
$phpmailer->isSMTP();
$phpmailer->Host =$SMTPhost;
$phpmailer->Port = $SMTPport;
$phpmailer->From = $FromEmail;
$phpmailer->FromName = $FromName;
}
This just fetches the options stored by WP MAIL SMTP and then reconfigures the phpmailer instance. Its just a result of desperate need to fix things ASAP.
Upvotes: 2
Reputation: 37730
The could not instantiate mail function
error means that you don’t have a local mail server installed, but it will only happen if you’re using mail()
– it will not apply if you are using SMTP. So it sounds like your suspicions are correct – the config for wp_mail has not been loaded for some reason, or the SMTP plugin is not doing what it’s supposed to.
Upvotes: -1