如何在 PHP 中使用 SMTP 发送 HTML 电子邮件

2022-01-17 00:00:00 email smtp php html

我已经能够在 PHP 中使用 SMTP 发送电子邮件,但是当我尝试将内容类型更改为 HTML 时,电子邮件没有送达.这是我正在尝试使用的代码:

I've been able to send an email using SMTP in PHP, but when I try to change the Content Type to HTML, the email doesn't get delivered. This is the code I'm trying to use:

    require_once "Mail.php";

    $from = "FPM <forms@fpmofames.com>";
    $from_name = "FPM";

    $host = "localhost";
    $username = "username";
    $password = "password";

    $subject = "Subject";
    $message = "Message";

    $to = "<example@example.com>";
    $headers = array ('From' => $from,
        'To' => $to,
        'Subject' => $subject,
        'MIME-Version' => '1.0',
        'Content-Type' => "text/html; charset=ISO-8859-1"
    );

    $smtp = Mail::factory('smtp',
        array ('host' => $host,
            'auth' => true,
            'username' => $username,
            'password' => $password));

    $mail = $smtp->send($to, $headers, $message);

如果我从标头中取出Content-Type"参数,它会很好地发送消息.我不知道为什么添加它会导致问题.

If I take the 'Content-Type' argument out of the headers, it sends the message just fine. I don't know why adding that causes a problem.

推荐答案

问题很可能出在 Mail 类上,但由于我们不知道您使用的是什么 Mail 类,所以很难回答.如果您还没有这样做,我真的会考虑使用 PHPMailer:https://github.com/PHPMailer/PHPMailer

The problem most likely lies in the Mail class, but since we don't know what Mail class you're using, it's difficult to answer. If you're not already doing so, I'd really think about using PHPMailer: https://github.com/PHPMailer/PHPMailer

相关文章