Cannot test Contribution Form "Thank You" page in Development Mode (or with Outbound Mail disabled)
Overview
When you test a contribution form that is configured to send an email receipt and also display a "Thank You" page with CiviCRM in Development mode, the "Thank You" page is never displayed because of an error attempting to send the receipt email.
The mail error occurs because Development mode changes the Outbound Mail setting to “Disable Outbound Email”.
By contrast, if sendmail
is misconfigured in the Civi admin settings, the "Thank You" page is still displayed but with a short "Mailing Error sendmail returned error code 127" message at the top.
Reproduction steps
- Configure a donation form in Test Mode to send an email receipt and display a “Thank You” page.
- Configure Authorize.Net as a Payment Processor. (Alternatively, you may be able to use any other synchronous Payment Processor— e.g. Stripe. I did not test this. However, I saw that this error does not occur with the PayPal Payment Processor because of its different flow.)
- Put CiviCRM into Development mode. (Alternatively, change the Outbound Mail setting to “Disable Outbound Email”.)
- Make a test donation.
- Result: no “Thank You” page is displayed. Instead:
- On my website, I’m redirected to the site home page. No error message is displayed to the user. (This may depend on the WordPress environment. Our site is built using Elementor for most theme elements.)
- An error is logged to Civi’s log: [info] Outbound mail has been disabled. Click Administer >> System Setting >> Outbound Email to set the OutBound Email.
- Within Civi, change the Outbound Mail settings to an invalid sendmail path such as “/usr/sbin/no_such_sendmail” and sendmail argument “-i”.
- Make another test donation.
- Result: the “Thank You” page is displayed with a useful "Mailing Error sendmail returned error code 127" message at the top.
Current behaviour
After completely filling out the Test Mode contribution form and clicking to submit the donation:
- The WordPress site's "home page" is displayed.
- Within CiviCRM, the test contribution is recorded as successful.
- An error message is logged to Civi's log: [info] Outbound mail has been disabled. Click Administer >> System Setting >> Outbound Email to set the OutBound Email.
Expected behaviour
To facilitate testing, Civi should display the "Thank You" page normally, with some kind of an error or warning message indicating no receipt was emailed because outbound mail is disabled.
As mentioned, if you intentionally misconfigure sendmail
within Civi (for example, by configuring it with a nonexistent “/usr/sbin/no_such_sendmail” entry), then Civi does display a more helpful "Mailing Error sendmail returned error code 127" message, and does display the "Thank You" page.
Environment information
- Browser: Chrome Version 94.0.4606.71
- CiviCRM: CiviCRM 5.41.0.
- PHP: PHP Version 7.3.28-1.
- CMS: WordPress 5.8.1.
- Database: mysql Ver 14.14 Distrib 5.7.34
- Web Server: Apache/2.4.46 (Ubuntu)
Where does the actual error occur?
wp-content/plugins/civicrm/civicrm/CRM/Utils/Mail.php, line 102
public static function createMailer() {
...
CRM_Core_Error::debug_log_message(ts('Outbound mail has been disabled. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')]));
===> CRM_Core_Error::statusBounce(ts('Outbound mail has been disabled. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')]));
...
What code can be modified to fix this?
wp-content/plugins/civicrm/civicrm/CRM/Contribute/BAO/Contribution.php, around line 4217
Change the following: fixNB20210923.patch
if (self::isEmailReceipt($input, $contributionID, $recurringContributionID)) {
civicrm_api3('Contribution', 'sendconfirmation', [
'id' => $contributionID,
'payment_processor_id' => $paymentProcessorId,
]);
\Civi::log()->info("Contribution {$contributionParams['id']} Receipt sent");
}
to
if (self::isEmailReceipt($input, $contributionID, $recurringContributionID)) {
$mailingInfo = Civi::settings()->get('mailing_backend');
if ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED) {
CRM_Core_Error::debug_log_message(ts('Outbound mail has been disabled. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')]));
CRM_Core_Error::debug_log_message(ts('Because outbound mail has been disabled, no contribution email receipt has been sent.'));
} else {
civicrm_api3('Contribution', 'sendconfirmation', [
'id' => $contributionID,
'payment_processor_id' => $paymentProcessorId,
]);
\Civi::log()->info("Contribution {$contributionParams['id']} Receipt sent");
}
}
It would be better still if the API v3 call threw an exception rather than redirecting. (It’s contrary to expectation that an API call produces a redirect upon failure!) Better logic might look like:
if (self::isEmailReceipt($input, $contributionID, $recurringContributionID)) {
try {
civicrm_api3('Contribution', 'sendconfirmation', [
'id' => $contributionID,
'payment_processor_id' => $paymentProcessorId,
]);
\Civi::log()->info("Contribution {$contributionParams['id']} Receipt sent");
} catch (Exception $ex) {
CRM_Core_Error::debug_log_message(ts('Failed to send contribution email receipt. Is Outbound Email disabled? Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to check the settings. Exception detail: %2', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1'), 2 => $ex->getMessage() ]));
}
}
Alternatively, the error message from misconfiguring sendmail is raised by:
wp-content/plugins/civicrm/civicrm/vendor/pear/mail/Mail/sendmail.php lines 191-195.
if ($result != 0) {
return PEAR::raiseError('sendmail returned error code ' . $result,
$result);
}
If the API v3 call displayed an error message like the misconfigured sendmail scenario does, it would still be possible to test the "Thank You" page with Development Mode turned on (or equivalently Outbound Mail turned off).