【发布时间】:2017-12-18 19:33:12
【问题描述】:
这是我发送包含 HTML 的 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');
}
我想在 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?
【问题讨论】:
-
您的电子邮件需要 MIME 编码和
Content-Type: text/html才能显示 html。请参阅:en.wikipedia.org/wiki/MIME 这可能发生在“smtp.php”中,但我们看不到。 -
这段代码运行良好,但我可以发送普通的文本电子邮件,我想在其中包含 HTML,该函数位于名为 smtp.php 的包含文件中
-
@KIKOSoftware 我添加了我的 smtp.php 源
-
谢谢。是的,缺少 MIME。所以添加它,或者使用一个有它的库,比如 phpmailer:github.com/PHPMailer/PHPMailer
-
谢谢@KIKOSoftware,但我不知道如何从库中添加 MIME?