简体   繁体   English

尝试使用PHP从html表单发送电子邮件

[英]Trying to send e-mail from html form with PHP

I'm trying to make a form that takes name, email and message and sends it to me. 我正在尝试制作一个包含名称,电子邮件和消息并将其发送给我的表格。

I have this form: 我有这个表格:

<form id="form" action="form.php" method="post" enctype="text/plain">

    Namn:<br>
    <input id="box1" type="text" name="name"><br>

    E-post:<br>
    <input id="box2" type="text" name="mail"><br>

    Meddelande:<br>
    <textarea id="messagebox" type="text" name="message"></textarea><br><br>

    <input id="submit" type="submit" value="Send">

</form>

This is my form.php: 这是我的form.php:

<?PHP
$name = $_POST["name"];
$mail = $_POST["mail"];
$message = $_POST["message"];

mail("name@example.com", "Message from $name", $message); 

?>

I do receive an email but it does not have the subject or message. 我确实收到了电子邮件,但没有主题或消息。 It says the mail is empty. 它说邮件是空的。 If I replace the variables with text in the mail function I get that correct text, which must means the variables don't get their values. 如果在mail函数中用文本替换变量,则会得到正确的文本,这必须表示变量未获取其值。

Is this the case and how should I solve it? 是这种情况,我应该如何解决? If not, what else could be wrong? 如果没有,还有什么不对的地方?

The problem is that you have set the enctype attribute to "text/plain", which means that only spaces are converted to + characters, and no special characters are encoded. 问题是您已将enctype属性设置为“ text / plain”,这意味着仅空格被转换为+字符,而没有特殊字符被编码。 This means that when PHP receives the submitted form, it will not put any of your form parameters in the $_POST auto global array, because they are being received in an unexpected format. 这意味着,当PHP收到提交的表单时,它不会将任何表单参数放入$ _POST自动全局数组中,因为它们以意外的格式被接收。 To fix this, simply remove the enctype attribute entirely, or replace it with "application/x-www-form-urlencoded". 要解决此问题,只需完全删除enctype属性,或将其替换为“ application / x-www-form-urlencoded”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM