duanjiaolao1187 2018-09-01 04:47
浏览 93
已采纳

通过php到gmail的html电子邮件与邮件中的文本不兼容

I have a php script to send email from my web to gmail.

php code

    $str = $_POST['to'];

    $contacts=explode(",",$str);


    foreach($contacts as $contact) {

        $to      = $contact;
        $subject = $_POST['subject'];
        $message = $_POST['message'];


        $headers = "From: ".$_POST['from']. "
" .
        "Reply-To: ".$_POST['reply']."
" .
        "X-Mailer: PHP/" . phpversion();

        if(mail($to, $subject, $message, $headers)){


            echo "<SCRIPT LANGUAGE='JavaScript'>
                    window.alert('Email Sent Successfully')
                    window.location.href='dashboard.php';
                    </SCRIPT>";
        }else{

            echo "<SCRIPT LANGUAGE='JavaScript'>
                window.alert('Email Not Sent')
                window.location.href='dashboard.php';
                </SCRIPT>"; 

        }
    }

    }else{

    }
}

html code

<form method="POST" action="">
    <table style="width:100%">
        <tr>
            <td>
              <label >Your Email</label>   
            </td>
            <td style="width:50%">
                <input type="text" name="from" style="widht:100%" />
            </td>
        </tr>
        <tr>
            <td>
                <label>Receipant(s)</label>   
            </td>
            <td style="width:50%">
                <input type="text" style="widht:100%"name="to" />
            </td>
        </tr>
        <tr>
            <td>
               <label>Email for Reply</label>
            </td>
            <td style="width:80%">
                <input type="text" style="widht:100%" name="reply" />
            </td>
        </tr>
        <tr>
            <td>
               <label>Subject</label>
            </td>
            <td style="width:50%">
                  <input type="text" name="subject" />
            </td>
        </tr>
        <tr>
            <td>
                <label>Message</label>
            </td>
            <td style="width:80%">
                   <textarea style="width:100%;height:300px" name="message" ></textarea>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <br>
                <hr>
                <button type="submit" class="btn btn-primary">Cancel</button>
                <button id="send" type="submit" name="submit" class="btn btn-success">Submit</button>

            </td>
        </tr>
    </table>
</form>

message is being sent but in gmail text is not shown properly. I refer to the following image and kindly see message here tag are shown in "" and that's why they are not working. how is it possible to make it workable. enter image description here

  • 写回答

1条回答 默认 最新

  • dongzhunqiu4841 2018-09-01 04:56
    关注

    In your headers you have to tell that you are sending HTML message

    $headers = "Content-Type: text/html; charset=UTF-8
    ".
            "From: ".$_POST['from']. "
    " .
            "Reply-To: ".$_POST['reply']."
    " .
            "X-Mailer: PHP/" . phpversion();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来