使用SMTP发送HTML电子邮件

时间:2017-12-18 19:33:13

标签: php html email smtp

这是我发送电子邮件的PHP代码:

if($_SERVER["REQUEST_METHOD"] == "POST")
{ 
    $email_message = "Hello World\n\n";
    $email_message .= "<h2>hello</h2>";
    $to = $_POST['reci'];
    $from = $email;
    $subject = $_POST['subject'];
    $body = $email_message;
    $SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
    $SMTPChat = $SMTPMail->SendMail();
} else {
    header('Location: XX');
}

我想发送&#39;你好&#39; h2标签中的单词,但我只收到

<h2>hello</h2>

并且它不是HTML代码,它就像文本一样返回。

这里是smtp.php文件:

 class SMTPClient
{

function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body)
{
$this->SmtpServer = $SmtpServer;
$this->SmtpUser = base64_encode ($SmtpUser);
$this->SmtpPass = base64_encode ($SmtpPass);
$this->from = $from;
$this->to = $to;
$this->subject = $subject;
$this->body = $body;
    if ($SmtpPort == "") 
    {
    $this->PortSMTP = 25;
        }else{
    $this->PortSMTP = $SmtpPort;
    }
}
function SendMail ()
{
    if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP)) 
    {

           fputs ($SMTPIN, "EHLO ".$HTTP_HOST."\r\n");  
           $talk["hello"] = fgets ( $SMTPIN, 1024 ); 

           fputs($SMTPIN, "auth login\r\n");
           $talk["res"]=fgets($SMTPIN,1024);
            fputs($SMTPIN, $this->SmtpUser."\r\n");
            $talk["user"]=fgets($SMTPIN,1024);

            fputs($SMTPIN, $this->SmtpPass."\r\n");
            $talk["pass"]=fgets($SMTPIN,256);

           fputs ($SMTPIN, "MAIL FROM: <".$this->from.">\r\n");  
           $talk["From"] = fgets ( $SMTPIN, 1024 );  
           fputs ($SMTPIN, "RCPT TO: <".$this->to.">\r\n");  
           $talk["To"] = fgets ($SMTPIN, 1024); 

           fputs($SMTPIN, "DATA\r\n");
            $talk["data"]=fgets( $SMTPIN,1024 );
            fputs($SMTPIN, "To: <".$this->to.">\r\nFrom: <".$this->from.">\r\nSubject:".$this->subject."\r\n\r\n\r\n".$this->body."\r\n.\r\n");
            $talk["send"]=fgets($SMTPIN,256);
           fputs ($SMTPIN, "QUIT\r\n");  
           fclose($SMTPIN); 
    }
return $talk;
}
}

在这种情况下我必须更改smtp.php源代码,但是如何在标题中添加MIME编码和Content-Type:text / html?

1 个答案:

答案 0 :(得分:1)

尝试此代码:

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