1

Ok, been trying for hours, looking at heaps of questions and documentation, having no luck.

I can send plain text emails fine from a gmail account using PEAR. No problems.

But I realised I needed a link, so I want to make it a HTML email. Tried questions and docs here, here, here and many more.

So far I have:

$em = new EmailObject;
$em->to = 'recipient@someplace.com';
$em->htmlbody = 'Some HTML msg with a <a href="goto.com?link=true">link</a>';
$em->body = 'Backup text';
$em->sendMail();

class EmailObject
{
    public $to = '';
    public $cc = '';
    public $bcc = '';
    public $subject = '';
    public $body = '';
    public $htmlbody = '';

    function sendMail()
    {
        $headers = array('From' => ***withheld*** ,
                  //'Return-Path' => ***withheld*** ,
                           'To' => $this->to,
/*                           'CC' => $this->cc,
                          'BCC' => $this->bcc, */
                      'Subject' => $this->subject);

        /*if ($this->cc <> '') { $headers['CC'] = $this->cc; }
        if ($this->bcc <> '') { $headers['BCC'] = $this->bcc; }          */  

        $server = array('host' => 'ssl://smtp.gmail.com',
                        'port' => '465',
                        'auth' => true,
                    'username' => ***withheld*** ,
                    'password' => ***withheld*** );
        $clrf = "\n";

        //Tried all sorts here:
        //$mime = new Mail_mime( array('eol' => $clrf, 'text_charset' => 'utf-8', 'html_charset' => 'utf-8') );
        $mime = new Mail_mime();

        $mime->setTXTBody = $this->body;
        $mime->setHTMLBody = $this->htmlbody;

        // Tried all sorts here:
        // $body = $mime->get();
        // $body = $mime->getMessageBody();
        // $body = $mime->getMessageBody(array('eol' => $clrf, 'text_charset' => 'utf-8', 'html_charset' => 'utf-8'));
        $body = $mime->get(array('eol' => $clrf, 'text_charset' => 'utf-8', 'html_charset' => 'utf-8'));
        $headers = $mime->headers($headers);
        $smtp =& Mail::factory('smtp', $server);
        $mail = $smtp->send($this->to, $headers, $body);

        print '<pre>';
        var_dump($body);
        var_dump($headers);        
        print '</pre>';

        /* Working snippet for plain text:
        $smtp = @Mail::factory('smtp', $server);
        $mail = @$smtp->send($this->to, $headers, $this->body);*/

        return PEAR::isError($mail);
    }
}

Now the email does send, however no body is created. It has the correct to and cc, but nothing in the body of the email. When I vardump the $body returned by $mime->get() or whatever one to use, it always returns NULL. What is going on? What am I doing wrong?

Community
  • 1
  • 1
Sophie
  • 103
  • 1
  • 1
  • 7
  • do you really need to use pear for sending html-based emails or it is just a wish to send via pear ? – Satya Oct 01 '12 at 02:10
  • There's other mail packages out there, or you could just use `mail` and build the headers yourself (I have, it's a little tricky but certainly doable). The best way to check what was sent is to go to Gmail and 'Show Original`. This will give you actually what was *given* to that mail client (Gmail). That's the entire message. You need to look at this verify all of the headers are just right, mimetypes or correct, the encoding isn't garbling something important, and your randomly generated section delimiter is appearing and correctly formatted. Use Show Original to do this. – Jared Farrish Oct 01 '12 at 02:15
  • For instance, there's [PHP Simple Mail](https://github.com/eoghanobrien/php-simple-mail). – Jared Farrish Oct 01 '12 at 02:17
  • I've chosen PEAR because I'm limited with what can be installed and PEAR is included nowadays with PHP. Also, I did try others and PEAR was the only one I could get a 'basic' example to work on. – Sophie Oct 01 '12 at 06:29
  • @Jared Farrish I can't use `mail` because I need to use SSL and SMTP authentication. The above works for plain text, I just can't seem to get _any_ body to appear for HTML emails. Thanks for the 'Show Original' trick, but as predicted it shows nothing for the body, as the `var_dump()` calls demonstrate the `$body` returned by `$mime->get()` etc are `NULL`. Any ideas? – Sophie Oct 01 '12 at 06:33

1 Answers1

0

I've had success using PHPMailer 5.2.0, it might be worth checking into. Some others were posted as comments. I would definitely try another email account or package before you pull too much hair out.

thewebguy
  • 1,510
  • 10
  • 15
  • I have already pulled my hair out too much with other packages. Installing them and configuring them is a pain. I just want to know what I am doing wrong with the PEAR package. People seem to say that it does what I want, it just seems I can't get it to work. It must be something simple. – Sophie Oct 01 '12 at 06:30