コンテキストの限界を突破する
コンテキストの限界を突破する —— 「読む」AIから、「文脈を創る」AIへの進化 ...
Z. Xingjie
2026年02月27日
Contact Form 7 (CF7) is one of the most widely used WordPress plugins for creating contact forms. However, there are times when emails sent via CF7 may fail due to various reasons like mail server issues or misconfigurations. In such cases, it is important to be notified so that we can take timely action.
By default, CF7 doesn’t provide any direct notification when an email fails to send. Fortunately, if we’re using PostSMTP to handle email sending, we can hook into CF7’s failure events and send an email notification using PostSMTP itself. In this guide, I’ll show how to set up a custom function to notify whenever Contact Form 7 fails to send an email using PostSMTP’s email service.
While WordPress has its built-in wp_mail() function to send emails, PostSMTP is a more robust solution that offers better reliability and reporting for email delivery. It integrates with various third-party email services such as Gmail, SendGrid, or any SMTP server, making it more versatile for handling email notifications.
Below is a step-by-step guide to implementing this functionality, along with the updated code that ensures a notification email is sent when CF7 fails to send one.
WordPress provides an action hook wpcf7_mail_failed that is triggered whenever Contact Form 7 fails to send an email. We can execute a custom function to send a notification email by hooking into this event.
Instead of using the default wp_mail(), we’ll use the PostmanWpMail class provided by PostSMTP to send the email notification. This ensures the email notification will be sent using the same reliable method as other emails sent through PostSMTP.
Here’s the updated code to achieve the desired functionality:
add_action('wpcf7_mail_failed', 'send_email_using_post_smtp_on_cf7_failure', 10, 1);
function send_email_using_post_smtp_on_cf7_failure($contact_form) {
// Set the recipient address
$recipient_email = 'admin@email.com'; // Replace with email address
$subject = 'Contact Form 7 Failed to Send Email from ' . get_bloginfo('name');
$message = 'A Contact Form 7 submission failed to send an email. Please check the system for issues.';
// Prepare the email data
$email_data = array(
'to' => $recipient_email,
'subject' => $subject,
'body' => $message,
'headers' => array('Content-Type: text/html; charset=UTF-8'),
);
// Use PostSMTP's email sending mechanism
$mailer = new PostmanWpMail();
$mailer->send($email_data['to'], $email_data['subject'], $email_data['body'], $email_data['headers']);
}
add_action() function hooks into wpcf7_mail_failed. This action is triggered whenever CF7 fails to send an email.$recipient_email variable should be replaced with our email address where we want to receive failure notifications.$subject and $message. We can customize the message to include more details if needed. Here, we append the site’s name to the email subject using get_bloginfo('name').wp_mail() function, we utilize the PostmanWpMail class from PostSMTP. The method $mailer->send() is used to send the email via PostSMTP’s configured service.Before using this solution, make sure that the PostSMTP plugin is installed and active on our WordPress site. This plugin is responsible for sending emails and must be correctly configured with our SMTP or third-party email service (like Gmail, SendGrid, etc.).
With just a few lines of code, we can enhance our website’s email reliability by ensuring we are notified when a Contact Form 7 email fails to send. By leveraging the power of PostSMTP, we can avoid using the default wp_mail() and instead rely on a more robust email solution.
Make sure to test this solution in our development environment before deploying it to our live site!
コンテキストの限界を突破する —— 「読む」AIから、「文脈を創る」AIへの進化 ...
Z. Xingjie
2026年02月27日
本記事は、英語で公開されている弊社ブログ記事の日本語翻訳版です。
英語元記事:Implement Basic AUth using lambda function for S3 Website Hosting
Muntasir Ahmed MUFTI
2026年02月06日
AIによる認知能力低下? —— MITの研究結果とこれからのエンジニアリング ...
Z. Xingjie
2026年02月02日