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

问题描述 投票:1回答:1

我有一个PHP脚本从我的网络发送电子邮件到Gmail。

PHP代码

    $str = $_POST['to'];

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


    foreach($contacts as $contact) {

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


        $headers = "From: ".$_POST['from']. "\r\n" .
        "Reply-To: ".$_POST['reply']."\r\n" .
        "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代码

<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>

邮件正在发送但gmail文本未正确显示。我参考下面的图片,并在此处看到消息标签显示在“”中,这就是为什么它们不起作用。怎么可能让它变得可行。 enter image description here

php html gmail
1个回答
0
投票

在你的标题中你必须告诉你发送HTML消息

$headers = "Content-Type: text/html; charset=UTF-8\r\n".
        "From: ".$_POST['from']. "\r\n" .
        "Reply-To: ".$_POST['reply']."\r\n" .
        "X-Mailer: PHP/" . phpversion();
© www.soinside.com 2019 - 2024. All rights reserved.