Closed remcotolsma closed 8 years ago
We've recently fixed some issues with the text/plain
format and we've updated the documentation on the Wordpress website for our plugin : https://wordpress.org/plugins/sendgrid-email-delivery-simplified/faq/
Firstly, you should set the content type as text/plain in the plugin's settings page which sets it globally for all emails sent with wp_mail, unless it's overwritten by another plugin or by you before calling wp_mail.
You should be aware that by default, plaintext emails are converted to HTML, this is an option that you can disable in Mail Settings by logging into your SendGrid account.
You should also be aware that there are some issues with other plugins like WP Better Emails that override the email's content type. Quote from our FAQ:
Using the default templates from WP Better Emails will cause all emails to be sent as HTML (i.e. text/html content-type). In order to send emails as plain text (i.e. text/plain content-type) you should remove the HTML Template from WP Better Emails settings page. This is can be done by removing the '%content%' tag from the HTML template.
Also, regarding the plugin you mentioned, we try to avoid vendoring other plugins along with ours since it will cause collisions for users that already have that plugin installed. We do our best to only vendor our own libraries like sendgrid-php
.
Thanks, but is it possible to add a extra filter to allow other plugins to convert HTML to text?
// send HTML content
if ( 'text/plain' !== $content_type ) {
$mail->setHtml( Sendgrid_Tools::remove_all_tag_urls( $message ) );
}
Source: https://github.com/sendgrid/wordpress/blob/master/lib/sendgrid/sendgrid-wp-mail.php#L347-L352
Change to:
// send HTML content
if ( 'text/plain' !== $content_type ) {
$mail->setText( apply_filters( 'sendgrid_mail_text', $message, $content_type ) );
$mail->setHtml( Sendgrid_Tools::remove_all_tag_urls( $message ) );
}
Or change to:
// send HTML content
if ( 'text/plain' !== $content_type ) {
$mail->setText( apply_filters( 'sendgrid_mail_text', $message ) );
$mail->setHtml( apply_filters( 'sendgrid_mail_html', $message ) );
}
I noticed the sendgrid_override_template
filter, but this filter doesn't allow us to set different message for text and HTML mail. With a sendgrid_mail_text
and sendgrid_mail_html
users can easily adjust the text or HTML mails.
You could also use the sendgrid_mail_html
filter to execute the remove_all_tag_urls
filter:
function sendgrid_remove_all_tag_urls( $message ) {
return Sendgrid_Tools::remove_all_tag_urls( $message );
}
add_filter( 'sendgrid_mail_html', 'sendgrid_remove_all_tag_urls' );
We'll look into adding this feature in the next versions. Thank you for the suggestion.
What is the status of this? After each update we have to manual change the /lib/sendgrid/sendgrid-wp-mail.php
, it would be nice if the suggested filters are added in the plugin so we can use them.
Hi.
We will release this requested feature soon. There are more things to come. Since this plugin is one of many integrations that we need to keep up to date, this feature has been added to our backlog and it is prioritized accordingly. We thank our users for their patience and appreciate any input.
@remcotolsma The fix for this issue has not been included in version 1.9.0
. However it is scheduled to be included in the next patch, version 1.9.1
, as we are currently working on it. If this affects you, we recommend that you wait until the release of 1.9.1
and skip this update.
@remcotolsma We have added the filters that you suggested to version 1.9.1
.
Changing the text content of all emails before they are sent:
function change_sendgrid_text_email( $message ) {
return $message . ' changed by way of text filter ';
}
add_filter( 'sendgrid_mail_text', 'change_sendgrid_text_email' );
Changing the HTML content of all emails before they are sent:
function change_sendgrid_html_email( $message ) {
return $message . ' changed by way of html filter ';
}
add_filter( 'sendgrid_mail_html', 'change_sendgrid_html_email' );
Thanks, finally had some time to adjust the "Pronamic SendGrid HTML to plain text" plugin to the new sendgrid_mail_text
filter: https://github.com/pronamic/wp-pronamic-sendgrid-html2text/commit/f7cdd83c1bd919021943e3243f4de5dd0872123f
if ( array_key_exists( 'sendgrid_mail_text' , $GLOBALS['wp_filter'] ) ) {
$text_content = apply_filters( 'sendgrid_mail_text', $text_content );
}
https://github.com/sendgrid/wordpress/blob/master/lib/sendgrid/sendgrid-wp-mail.php#L333-L335
if ( array_key_exists( 'sendgrid_mail_html' , $GLOBALS['wp_filter'] ) ) {
$html_content = apply_filters( 'sendgrid_mail_html', $html_content );
}
https://github.com/sendgrid/wordpress/blob/master/lib/sendgrid/sendgrid-wp-mail.php#L357-L359
As far as i know it's very unusual to check if a filter exists, WordPress already checks if there are filters and will early return if the filter is not set.
https://github.com/WordPress/WordPress/blob/4.6.1/wp-includes/plugin.php#L211-L215
I also think you should avoid using the global $wp_filter
variable directly, also see the upcoming changes in WordPress 4.7
: https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/.
If your plugin directly accesses the
$wp_filter
global rather than using the public hooks API, you might run into compatibility issues.
When the content type is
text/html
it will still set the HTML $message as text:setText( $message )
: https://github.com/sendgrid/wordpress/blob/master/lib/sendgrid/sendgrid-wp-mail.php#L285-L288Maybe you can convert the HTML to plain text? We already wrote a simple plugin for this: https://github.com/pronamic/wp-pronamic-sendgrid-html2text
Unfortunately this requires a small adjustment in the SendGrid plugin.
There are a few libraries to convert HTML to text:
Is this something you can add, or is there another work around? We use a WordPress form plugin which sends HTML notifications to support system Zendesk. Currently Zendesk doesn't support HTML e-mails so we see the plain text e-mail in Zendesk which contains the complete HTML source.