HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Please upgrade here. These earlier versions are no longer being updated and have security issues.

HTML Formatting and Emails

edited October 2010 in Vanilla 2.0 - 2.8
We've installed the CLEditor extension, and are using HTML Purifier on our site. As a result, nearly every post has at least basic HTML formatting in it. Additionally, we've set all the default notification settings to maximum, so every post results in emails being sent somewhere.

This is all fine, except that the emails are always sent in text format, rather than HTML format. I looked briefly at the code, and it seems that the entire Vanilla email design assumes that all emails will be plaintext, not HTML. Am I right?

Is there any way to configure this (or easily modify it) to allow these emails (especially notifications) to be sent in HTML format, or better yet, to use multipart to send both HTML and plaintext in the same message?

I know some purists might think this is a terrible thing that I'm asking for, but it really isn't. I don't want to send out massive highly stylized advertisement newsletter monoliths. I think the user experience would actually be greatly improved if any WYSIWYG formatting used in the post (including links, bold, italics, paragraphs) were preserved in the email. Right now it is just sending a jumbled glob of meaningless gobbledy gook.

Comments

  • OK, I found that I can set $Configuration['Garden']['Email']['MimeType'] = 'text/html'; but now it looks like I'll need to create a new translation in order to rewrite the email templates to support the new HTML default format. Yuck. It looks like the email will also not be multipart (just HTML). There's got to be a better way...
  • I've addressed this temporarily by adding the following to conf/locale.php:
    $Definition['EmailInvitation'] = '<div>Hello!</div>
    <div>&nbsp;</div>
    <div>%1$s has invited you to join %2$s. If you want to join, you can do so by clicking this link:</div>
    <div>&nbsp;</div>
    <div style="margin-left: 20px;"><a href="%3$s">%3$s</a></div>
    <div>&nbsp;</div>
    <div>Have a great day!</div>';
    $Definition['EmailMembershipApproved'] = '<div>Hello %1$s,</div>
    <div>&nbsp;</div>
    <div>You have been approved for membership. Sign in now at the following link:</div>
    <div>&nbsp;</div>
    <div style="margin-left: 20px;"><a href="%2$s">%2$s</a></div>
    <div>&nbsp;</div>
    <div>Have a great day!</div>';
    $Definition['EmailWelcome'] = '<div>Hello %1$s,</div>
    <div>&nbsp;</div>
    <div>%2$s has created an account for you at %3$s. Your login credentials are:
    <div>&nbsp;</div>
    <div style="margin-left: 20px;">
    <div>Email: %6$s</div>
    <div>Password: %5$s</div>
    <div>Url: <a href="%4$s">%4$s</a></div>
    </div>
    <div>&nbsp;</div>
    <div>Have a great day!</div>';
    $Definition['EmailPassword'] = '<div>Hello %1$s</div>,
    <div>&nbsp;</div>
    <div>%2$s has reset your password at %3$s. Your login credentials are now:</div>
    <div>&nbsp;</div>
    <div style="margin-left: 20px;">
    <div>Email: %6$s</div>
    <div>Password: %5$s</div>
    <div>Url: <a href="%4$s">%4$s</a></div>
    </div>
    <div>&nbsp;</div>
    <div>Have a great day!</div>';
    $Definition['PasswordRequest'] = '<div>Hello %1$s,</div>
    <div>&nbsp;</div>
    <div>Someone has requested to reset your password at %2$s. To reset your password, follow this link:</div>
    <div>&nbsp;</div>
    <div style="margin-left: 20px;"><a href="%3$s">%3$s</a></div>
    <div>&nbsp;</div>
    <div>If you did not make this request, disregard this email.</div>';
    $Definition['EmailNotification'] = '<div>%1$s</div>
    <div>&nbsp;</div>
    <div>Follow the link below to check it out:</div>
    <div><a href="%2$s">%2$s</a></div>
    <div>&nbsp;</div>
    <div>Have a great day!</div>';
    $Definition['EmailStoryNotification'] = '<div>%1$s</div>
    <div>&nbsp;</div>
    <div style="margin-left: 20px;">%3$s</div>
    <div>&nbsp;</div>
    <div>Follow the link below to check it out:</div>
    <div><a href="%2$s">%2$s</a></div>
    <div>&nbsp;</div>
    <div>Have a great day!</div>';
    
    
    Really, I think this (ideally) needs to be revised and handled differently.

    1. Jon Abernathy wrote an awesome bit of code (GPL license) that can build decent plaintext content from an HTML formatted input: http://www.chuggnutt.com/html2text.php (this could be used to process the user body, which, unless I'm mistaken, should have technically already been returned from the formatting functions in HTML format.)

    2. On the one hand, emails are pretty language specific, but on the other hand, it doesn't seem clean to have the email templates in the locale, because they are "personal" way beyond just a translation level. Would it be better to put these somewhere more immediately and obviously configurable?

    3. The email definitions should (ideally) have both HTML and non-HTML versions, so that the right one can be selected by the system when it is needed without people needing to rewrite the emails. Alternately only HTML versions + html2text would be sufficient, but not perfect.
  • Have you managed to send both HTML and plaintext in the same message since then?

  • CsabbencsCsabbencs
    edited October 2012

    I've just done it.

    It is PHPMailer who sends emails. PHPMailer has a property called 'AltBody'. When it is set PHPMailer knows that you would like to send a multipart message and you want the text/plain portion of the mail to be the value of 'AltBody'. In the current version of Garden there is no way for you to set the property, so unfortunately, you have to modify the core.

    Step 1: Add ability to set 'AltBody' by adding the following function to library/core/class.email.php

    public function AltMessage($AltMessage) {
        $this->PhpMailer->AltBody = htmlspecialchars_decode($AltMessage,ENT_QUOTES);
        return $this;
    }



    Step 2: Find all the places in Garden where email is sent by searching for the text '$Email->Message' in Garden files.


    Step 3: Set 'AltBody' whenever you want to send a text/plan part beside the text/html part by adding the line

        $Email->AltMessage("My text/plain message.");
    

    (with your own message) after the existing line with '$Email->Message'.


    You may need to use the same logic that '$Email->Message' uses in order to be able to use email templates from the locales which needs to be created then as well.

    peregrine
Sign In or Register to comment.