Archive

Archive for the ‘swift_mailer’ Category

Simple html to text converter

May 3, 2012 2 comments

This is follow-up post on sending html email using swift mailer [ see previous post here ].

Since it’s never fun to have to write the same (or similar) stuff twice, the other morning I started to look for something than can do some basic html to text conversion, that is, turn br, p into newline, at the minimum. Through googling I discovered this handy php script to do the job. For example, html2text is able to convert the following html

    Dear Customer,
    <p>blah blah...</p>
    <p />
    Sincerely,<br />
    ABC Company Service Dept<br />
    <a href="http://example.com">A link</a>

into plain text

    Dear Customer,

    blah blah...


    Sincerely,
    ABC Company Service Dept
    [A link](http://example.com)

To use the code can’t be easier:

...
require_once 'path/to/html2text.php';
$html="<p>some html text here</p>";  // original html message
$plain=convert_html_to_text($html);   // converted plain text
...

Here’s a complete example on how to use it with swift mailer to send out html email (at a low SPAM score):

<?php
        require_once 'lib/swift_required.php'; // or absolute path if swift mailer library is not under current directory
        require_once 'path/to/html2text.php';
        define('mailhost', 'localhost'); // change if using external smtp server
        $to='some_recipient@email.address';
        $subject='HTML msg sending test';
        $body_html='<p>This is a <strong>test</strong>.</p>';
        $plain=convert_html_to_text($body_html); 
        $transport=Swift_SmtpTransport::newInstance(mailhost, 25);
        $mailer=Swift_Mailer::newInstance($transport);
        $message=Swift_Message::newInstance($subject)
            ->setFrom( array('noreply@yourcompany.com'=>'Your Company Name') )
            ->setTo( array($to) )
            ->setBody($plain)
            ->addPart($body_html, 'text/html');
        $result = $mailer->send($message);
Advertisements
Categories: html2text, php, swift_mailer

How-to: Lower html email spam score using swift mailer

April 19, 2012 Leave a comment

Swift mailer is yet another great PHP emailing library. Working as a web developer, I am faced quite often with the tasks of sending out html email. One particular area I always pay attention to is try to lower the emails’ spam score so they won’t end up in recipient’s junk or spam box. Here’s the ‘magic’ code that does the trick:

<?php
        require_once 'lib/swift_required.php'; // or absolute path if swift mailer library is not under current directory
        define('mailhost', 'localhost'); // or external smtp server
        $to='some_recipient@email.address';
        $subject='HTML msg sending test';
        $body='This is a test.';
        $body_html='<p>This is a <strong>test</strong>.</p>';
        $transport=Swift_SmtpTransport::newInstance(mailhost, 25);
        $mailer=Swift_Mailer::newInstance($transport);
        $message=Swift_Message::newInstance($subject)
            ->setFrom( array('noreply@yourcompany.com'=>'Your Company Name') )
            ->setTo( array($to) )
            ->setBody($body)
            ->addPart($body_html, 'text/html');
        $result = $mailer->send($message);

In real-life examples, I’ve seen score dropping by as much as 2.1 by sending html message along with its plain-text counterpart. The reasoning behind this is well explained in this stackoverflow question:
http://stackoverflow.com/questions/5787185/is-it-sensible-to-send-html-only-email-these-days.

Categories: php, spam score, swift_mailer