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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序