MailJet api设置HTML电子邮件文本

时间:2014-03-10 14:23:21

标签: php email smtp pear

我正在使用MailJet API。我可以通过MailJet发送纯文本电子邮件。我尝试发送HTML邮件但由于某种原因它无法正常工作。这是我的代码。

        include('Mail.php');

    $recipients = 'abc@gmail.com'; //CHANGE

    $headers['From']    = 'admin@abc.com'; //CHANGE
    $headers['To']      = 'abc@gmail.com'; //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);

此代码适用于纯文本。 我尝试将$params['auth'] = 'PLAIN';更改为$params['auth'] = 'HTML';,但没有效果。

我也试过

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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

这是工作代码。

        include('Mail.php');
    include_once("Mail/mime.php");

    $recipients = 'abc@gmail.com'; //CHANGE

    $headers['From']    = 'admin@abc.com'; //CHANGE
    $headers['To']      = 'abc@gmail.com'; //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);