Skip to content

Commit

Permalink
Contact Form: Send all emails through wrapper to ensure consistency
Browse files Browse the repository at this point in the history
Previously the two `wp_mail()` calls in `Grunion_Contact_Form::process_submission()` had extra filters around them in order to support HTML messages. The `wp_mail()` call in `grunion_ajax_spam()` was missing those filters, though. That resulted in ham messages being sent with a `plain-text` content type, but with HTML markup in the body.

Sending all `wp_mail()` calls fixes that particular issue, and also makes it much less likely that a similar bug will happen in the future, because it is DRY.

Fixes #7179
  • Loading branch information
iandunn committed May 19, 2017
1 parent 641796c commit 243619c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion modules/contact-form/admin.php
Expand Up @@ -696,7 +696,7 @@ function grunion_ajax_spam() {
*/
$subject = apply_filters( 'contact_form_subject', $content_fields['_feedback_subject'], $content_fields['_feedback_all_fields'] );

wp_mail( $to, $subject, $message, $headers );
Grunion_Contact_Form::wp_mail( $to, $subject, $message, $headers );
}
} elseif( $_POST['make_it'] == 'publish' ) {
if ( ! current_user_can($post_type_object->cap->delete_post, $post_id) ) {
Expand Down
31 changes: 25 additions & 6 deletions modules/contact-form/grunion-contact-form.php
Expand Up @@ -2150,8 +2150,6 @@ function process_submission() {
wp_schedule_event( time() + 250, 'daily', 'grunion_scheduled_delete' );
}

add_filter( 'wp_mail_content_type', __CLASS__ . '::get_mail_content_type' );
add_action( 'phpmailer_init', __CLASS__ . '::add_plain_text_alternative' );
if (
$is_spam !== true &&
/**
Expand All @@ -2166,7 +2164,7 @@ function process_submission() {
*/
true === apply_filters( 'grunion_should_send_email', true, $post_id )
) {
wp_mail( $to, "{$spam}{$subject}", $message, $headers );
self::wp_mail( $to, "{$spam}{$subject}", $message, $headers );
} elseif (
true === $is_spam &&
/**
Expand All @@ -2180,10 +2178,8 @@ function process_submission() {
*/
apply_filters( 'grunion_still_email_spam', false ) == true
) { // don't send spam by default. Filterable.
wp_mail( $to, "{$spam}{$subject}", $message, $headers );
self::wp_mail( $to, "{$spam}{$subject}", $message, $headers );
}
remove_filter( 'wp_mail_content_type', __CLASS__ . '::get_mail_content_type' );
remove_action( 'phpmailer_init', __CLASS__ . '::add_plain_text_alternative' );

if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return self::success_message( $post_id, $this );
Expand Down Expand Up @@ -2217,6 +2213,29 @@ function process_submission() {
exit;
}

/**
* Wrapper for wp_mail() that enables HTML messages with text alternatives
*
* @param string|array $to Array or comma-separated list of email addresses to send message.
* @param string $subject Email subject.
* @param string $message Message contents.
* @param string|array $headers Optional. Additional headers.
* @param string|array $attachments Optional. Files to attach.
*
* @return bool Whether the email contents were sent successfully.
*/
public static function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
add_filter( 'wp_mail_content_type', __CLASS__ . '::get_mail_content_type' );
add_action( 'phpmailer_init', __CLASS__ . '::add_plain_text_alternative' );

$result = wp_mail( $to, $subject, $message, $headers, $attachments );

remove_filter( 'wp_mail_content_type', __CLASS__ . '::get_mail_content_type' );
remove_action( 'phpmailer_init', __CLASS__ . '::add_plain_text_alternative' );

return $result;
}

/**
* Add a display name part to an email address
*
Expand Down

0 comments on commit 243619c

Please sign in to comment.