使用php从html表单发送电子邮件 [英] Sending email from html form using php
问题描述
我一整天都在尝试,但是我不知道如何从我的html联系表单中发送一封电子邮件,其中包含表单中的信息到我的电子邮件地址。新到php
Ive been trying this out the whole day but I cant figure out how to send an email from my html contact form containing the information from the form to my email address. Im new to php.
Ive尝试通过将其上传到免费的网页托管运行。我得到了消息成功!当我按下我的HTML表单上的提交按钮,但没有发送电子邮件。
Ive tried running this by uploading it to free web hosting. I get the message "success!" when I press the submit button on my html form but no email is actually sent.
任何帮助都不胜感激。
PHP脚本:
<?php
//Subject
$subject ="Contact Form Submission";
// Name
$name =$_POST['InputName'];
// Message
$message =$_POST['InputMessage'];
//Mail of Sender
$email =$_POST['InputEmail'];
//From
$header = "From:$name<$email>";
$send_contact=mail("myemail@gmail.com",$subject,$message,$header);
//Check if mail was sent
if($send_contact){
echo "Success!";
}
else {
echo "Error!";
}
?>
编辑:在整整一天的试错之后计算出来。问题是我正在使用的免费网络主机。更改主机和代码开始工作正常。希望这能帮助未来的人。感谢所有的帮助。
Figured it out after one whole day of trial and error. The problem was with the free web host I was using. Changed hosts and the code started working fine. Hope this helps someone in the future. Thanks all for the help.
推荐答案
我有一个很好的主意,为什么你的代码不工作。很久以前我就发生了。您的代码不工作的原因是因为:
I have a pretty good idea why your code is not working. It happened to me a long time ago. The reason why your code is not working is because :
- 当您在标题中传递from时,php希望有一个现有的电子邮件帐户
服务器。 例如:$ headers ='From:emailacc@yourserver.com';
- 你必须在你的服务器上创建一个电子邮件帐户。然后将
从
头
中的 c $ c添加到刚刚创建的电子邮件地址。 -
$ headers
中的From
字段不是From
如你所想。 -
<?php
-
$ email = $ _POST [InputEmail];
-
$ subject = $ _POST [InputSubject];
-
$ message =From:$ email。,$ _ POST [InputMessage];
//你把这个电子邮件地址从这里输入到这里 -
$ headers ='From:emailacc@yourserver.com';
//这里是您要发送电子邮件的电子邮件地址(即您的服务器电子邮件地址) -
mail($ to,$
-
?>
- When you pass "from" in headers, php expects an existing email account of your
server. For example :$headers = 'From: emailacc@yourserver.com';
- So first thing you gotta do is create an email account on your server. And then put the
From
inheader
to the email address that you've just created. - The
From
field in the$headers
is not theFrom
as you think. <?php
$email = $_POST["InputEmail"];
$subject = $_POST["InputSubject"];
$message = "From: ".$email.", ".$_POST["InputMessage"];
// you put the email address from the input form here$headers = 'From: emailacc@yourserver.com';
// here is the email address specified from which u want to send the email.(i.e. your server email address)mail($to, $subject, $message, $headers)
?>
我相信这会做这个工作:)
I'm sure this will do the job :)
这篇关于使用php从html表单发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!