【问题标题】:How to use phpmailer and send mail as html from ckeditor如何使用 phpmailer 并从 ckeditor 以 html 格式发送邮件
【发布时间】:2017-04-20 19:14:37
【问题描述】:

我正在使用phpmailer发送电子邮件,在ckeditor表单上插入html代码内容时出现问题,但数据仅发送到电子邮件文本。

这是我的代码:

require_once ('class.phpmailer.php');

$mail = new PHPMailer(true);
if (isset($_POST['btn_send'])) 
    {

    $smtp_username = strip_tags($_POST['username']);
    $smtp_password = strip_tags($_POST['password']);
    $ssl_port = strip_tags($_POST['port']);
    $my_smtp = strip_tags($_POST['host']);
    $my_ssl = strip_tags($_POST['type']);
    $realname = strip_tags($_POST['realname']);
    $subject = strip_tags($_POST['subject']);
    $body = strip_tags($_POST['editor']);
    $emaillist = strip_tags($_POST['emaillist']); 


//...now get on with sending...
try 
{
//$mail->isSendmail();
        $mail->isSMTP();
        $mail->Body = ($body);
        $mail->isHTML(true);
        $mail->SMTPDebug = 0;
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = "$my_ssl";
        $mail->Host = "$my_smtp";
        $mail->Port = "$ssl_port";
        $mail->AddAddress($emaillist);
        $mail->Username = "$smtp_username";
        $mail->Password = "$smtp_password";
        $mail->SetFrom("$smtp_username", "$realname");
        $mail->AddAddress($emaillist);
        $mail->epriority = "3";
        $mail->AddReplyTo("$smtp_username");
        $mail->Subject = "$subject";
        $mail->encode = ("yes");
        $mail->CharSet = "utf-8";
if($mail->Send())
{
$msg = "<div class='alert alert-success'>
Hi,<br /> bro mail terkirim ke ".$emaillist."
</div>";
}
}
catch(phpmailerException $ex)
{
$msg = "<div class='alert alert-warning'>".$ex->errorMessage()."</div>";
}}

我不知道出了什么问题

【问题讨论】:

  • 正如@Artem Ilchenko 提到的,您需要通过&lt;PHPMailer object&gt;.isHTML(true) 方法启用HTML 内容交付

标签: php html email phpmailer


【解决方案1】:

您需要编辑此行 $body = strip_tags($_POST['editor']);$body = $_POST['editor'];

并在邮件发送前添加这一行 $mail->isHTML(true);

函数 strip_tags 删除 html 标记。

但您需要另一种方式过滤值。

【讨论】:

    【解决方案2】:

    请编辑此行

    $body = strip_tags($_POST['editor']); to $body = $_POST['editor'];
    

    strip_tags() 函数将从您发布的值中删除所有标签。你无法获取 html 标签。

    请通过添加此行在您的 PHP 邮件中启用 html

    $mail->isHTML(true);
    

    所以请编辑代码并尝试这种方式。

    这在我的情况下工作正常。

    【讨论】:

      【解决方案3】:

      如果您使用 Codeigniter,您可能会遇到以下问题:

      $body = $this->input->post('some_field_with_html_body');
      

      您可以通过以下方式修复它:

      $body = $_POST['some_field_with_html_body'];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-19
        • 2017-04-13
        • 1970-01-01
        • 2010-09-26
        • 2013-11-29
        相关资源
        最近更新 更多