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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵