PHP电子邮件表单无法正常工作 [英] PHP Email form not working properly

查看:201
本文介绍了PHP电子邮件表单无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常喜欢php,但即使如此,对我来说也没有任何意义:
我有一个包含PHP表单的HTML页面如下:
...

 < article id =content>< div class =ic>这里是联系表格< / div> 
< div class =wrapper>
< h2>联络表单< / h2>
< form method =postid =ContactFormname =myemailformaction =sendemail.php>
< div>
< div class =wrapper>
< span> Nume:< / span>
< div class =bg>< input type =textclass =inputname =myname>< / div>
< / div>
< div class =wrapper>
< span> Adresa:< / span>
< div class =bg>< input type =textclass =inputname =myadresa>< / div>
< / div>
< div class =wrapper>
< span>电子邮件:< / span>
< div class =bg>< input type =textclass =inputname =myemail>< / div>
< / div>
< div class =textarea_box>
< span> Mesaj:< / span>
< div class =bg>< textarea name =textareacols =1rows =1name =mymesaj>< / textarea>< / div>
< / div>
< a href =#class =button1name ='submit'value =submitonclick ='javascript:document.myemailform.binset = 2; document.myemailform.submit();'>发送消息< / a>
< / div>
< / form>
< / div>
< / article>

行动sendemail.php如下:



($ _ p pre
$ b如果(isset($ _ POST ['submit'])){

//编辑下面的2行AS REQUIRED
$ email_to =myreal_email@email.com;
$ email_subject =来自联系表单的消息;

函数死($ error){
//你的错误代码可以在这里
echo我们很抱歉,但是发现有错误,表单你提交。
echo这些错误出现在下面。< br />< br />;
echo $ error。< br />< br />;
echo请回去修复这些错误。< br />< br />;
die();
}

//验证预期数据存在
if(!isset($ _ POST ['myname'])||
!isset($ _ POST ['myadresa '])||
!isset($ _ POST ['myemail'])||
!isset($ _ POST ['mymesaj'])){
死了(我们很抱歉,但是您提交的表单似乎有问题。);
}

$ first_name = $ _POST ['myname']; // required
$ last_name = $ _POST ['myadresa']; // required
$ email_from = $ _POST ['myemail']; // required
$ comments = $ _POST ['mymesaj']; // required

$ error_message =;
$ email_exp ='/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2, 4} $ /';
如果(!preg_match($ email_exp,$ email_from)){
$ error_message。='您输入的电子邮件地址似乎没有效果。< br />';
}
$ string_exp =/ ^ [A-Za-z .'-] + $ /;
if(!preg_match($ string_exp,$ first_name)){
$ error_message。='您输入的名字似乎没有效果。< br />';
}
如果(!preg_match($ string_exp,$ last_name)){
$ error_message。='您输入的姓氏似乎没有效果< br /> ;
}
if(strlen($ comments)< 2){
$ error_message。='您输入的评论似乎没有效果。< br />';
}
if(strlen($ error_message)> 0){
已死($ error_message);
}
$ email_message =Form details below.\\\
\\\
;

函数clean_string($ string){
$ bad = array(content-type,bcc:,to:,cc:,href
return str_replace($ bad,,$ string);
}

$ email_message。=Name:.clean_string($ first_name)。\\\
;
$ email_message。=Adresa:.clean_string($ last_name)。\\\
;
$ email_message。=电子邮件:.clean_string($ email_from)。\\\
;
$ email_message。=Mesaj efectiv:.clean_string($ comments)。\\\
;

//创建邮件头
$ headers ='From:'$ email_from。\r\\\

'Reply-To:'。$ email_from。\r\\\

'X-Mailer:PHP /'。 phpversion();
@mail($ email_to,$ email_subject,$ email_message,$ headers);
?>
<! - 包括你自己的成功html这里 - >
感谢您与我们联系。我们将很快与您联系。
<?php
}
?>

现在,问题是我收到的电子邮件,主题确定,发件人电子邮件确定,但消息是空的...没有$ email_message被收到。
为什么会发生这种情况?
任何帮助将不胜感激。
谢谢

解决方案

问题在于您的

 < textarea name =textareacols =1rows =1name =mymesaj>< / textarea> 

有两个名称与textarea检查它。

 < textarea cols =1rows =1name =mymesaj>< / textarea> 


I am very new at php but even so, this does not make any sense to me: I have an HTML page containing the php form as follows: ...

    <article id="content"><div class="ic">Here comes the contact form!</div>
    <div class="wrapper">
    <h2>Contact form</h2>
    <form method="post" id="ContactForm" name="myemailform" action="sendemail.php">
    <div>
    <div  class="wrapper">
    <span>Nume:</span>
    <div class="bg"><input type="text" class="input" name="myname"></div>
    </div>
    <div  class="wrapper">
    <span>Adresa:</span>
    <div class="bg"><input type="text" class="input" name="myadresa"   ></div>                              
    </div>
    <div  class="wrapper">
    <span>Email:</span>
    <div class="bg"><input type="text" class="input" name="myemail"  ></div>                                
    </div>
    <div  class="textarea_box">
    <span>Mesaj:</span>
    <div class="bg"><textarea name="textarea" cols="1" rows="1" name="mymesaj"></textarea></div>
    </div>
    <a href="#" class="button1" name='submit' value="submit" onclick='javascript: document.myemailform.binset=2; document.myemailform.submit();'>Send message</a>  
    </div>
    </form>
    </div>
    </article>

And the action sendemail.php is as follows:

<?php
if(isset($_POST['submit'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "myreal_email@email.com";
$email_subject = "Message from contact form";

function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}

// validation expected data exists
if(!isset($_POST['myname']) ||
!isset($_POST['myadresa']) ||
!isset($_POST['myemail']) ||
!isset($_POST['mymesaj'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');      
}

$first_name = $_POST['myname']; // required
$last_name = $_POST['myadresa']; // required
$email_from = $_POST['myemail']; // required
$comments = $_POST['mymesaj']; // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Adresa: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Mesaj efectiv: ".clean_string($comments)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
    ?>

Now, the problem is that I receive the email, the subject is ok, the sender email is ok, but the message is empty... no $email_message is received. Why is this happening? Any help would be appreciated. Thank you

解决方案

the problem is with your

  <textarea name="textarea" cols="1" rows="1" name="mymesaj"></textarea>

there are two names with the textarea check it.

 <textarea  cols="1" rows="1" name="mymesaj"></textarea>

这篇关于PHP电子邮件表单无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆