Search code examples
phpemailsmtppear

MailJet api set HTML email text


I am using MailJet API. I am able to send the plain text email via MailJet. I tried for sending HTML mail but for some reason its not working. Here is my code.

        include('Mail.php');

    $recipients = '[email protected]'; //CHANGE

    $headers['From']    = '[email protected]'; //CHANGE
    $headers['To']      = '[email protected]'; //CHANGE
    $headers['Subject'] = 'Test message';

    $body = 'Test message';

    // Define SMTP Parameters

    $params['host'] = 'in.mailjet.com';
    $params['port'] = '25';
    $params['auth'] = 'PLAIN';
    $params['username'] = 'xxxxxx'; //CHANGE
    $params['password'] = 'xxxxxx'; //CHANGE

    // Create the mail object using the Mail::factory method

    $mail_object =& Mail::factory('smtp', $params);

    // Send the message

    $mail = $mail_object->send($recipients, $headers, $body);

This code works perfect for plain text. I tried changing $params['auth'] = 'PLAIN'; to $params['auth'] = 'HTML'; but didn't worked.

I have also tried

    $message = new Mail_mime();
    $html = "<p>Test message</p>";  
    $message->setHTMLBody($html);
    $body = $message->get();

Any help is really appreciated.


Solution

  • Here is the working code.

            include('Mail.php');
        include_once("Mail/mime.php");
    
        $recipients = '[email protected]'; //CHANGE
    
        $headers['From']    = '[email protected]'; //CHANGE
        $headers['To']      = '[email protected]'; //CHANGE
        $headers['Subject'] = 'Test message';
            $message = new Mail_mime(array('eol' => $crlf));
            $html = "<p>Test message</p>";
            $message->setHTMLBody($html);
            $body = $message->get();
    
        // Define SMTP Parameters
    
        $headers = $message->headers($headers);
    
        $params['host'] = 'in.mailjet.com';
        $params['port'] = '25';
        $params['auth'] = TRUE;
        $params['isHTML'] = TRUE;
        $params['username'] = 'xxxxxxx'; //CHANGE
        $params['password'] = 'xxxxxxx'; //CHANGE
    
        // Create the mail object using the Mail::factory method
    
        $mail_object =& Mail::factory('smtp', $params);
    
        // Send the message
    
        $mail = $mail_object->send($recipients, $headers, $body);