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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器