只发送字段框填充。如果空,那么不要发邮件。 php表单发邮件 [英] send only field boxes filled. if empty then do not send in email. php form to email

查看:112
本文介绍了只发送字段框填充。如果空,那么不要发邮件。 php表单发邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的表单发送到php。发送php代码中的这个表单的好处是,我不必写出所有的字段名称。例如,我不必写这个:

  $ msg。=$ _POST [ContactName]联系人姓名:ContactName\\\

在我的代码中,我必须写的是:

  foreach($ _POST as $ Field = > $ Value){
$ body。=$ Field:$ Value\\\
;

表单目前工作正常。目前正在发送:

  ContactName:name here 
BusinessName:business name here
EmailAddress:email @ email.com
Email_Confirmation:
电话号码:714-555-5555

但是,我希望这样做:如果一个字段留空,或者网页用户没有填写某个字段框,则表单不应该发送此字段。例如:web用户决定不填写ContactName和/或BusinessName。所以表单只能发送这种格式:

  EmailAddress:email@email.com 
Email_Confirmation:
电话号码:714-555-5555

注意到没有提及ContactName:和BusinessName:



请帮忙!我会很感激。 -Michelle Ha。



这是php发送代码:

 如果Email_Confirmation字段为空,则
if(isset($ _ POST ['Email_Confirmation'])&& $ _POST ['Email_Confirmation'] ==''){

// put你的电子邮件地址在这里
$ youremail ='bomandty@gmail.com';

//准备一个漂亮版本的消息
$ body。=谢谢你的要求,我们很快就会回来。
$ body。=\\\
;
$ body。=\\\
;
foreach($ _POST as $ Field => $ Value){
$ body。=$ Field:$ Value\\\
;
$ body。=\\\
;
}

$ CCUser = $ _POST ['EmailAddress'];

//如果提供一个
//(并且它并不是要破解您的表单),请使用提交者电子邮件。
//否则从您的电子邮件地址发送。
if($ _POST ['EmailAddress']&&!preg_match(/ [\r\\\
] /,$ _POST ['EmailAddress'])){
$ headers = From:$ _POST [EmailAddress];
} else {
$ headers =From:$ youremail;
}

//最后,发送消息
mail($ youremail,'subject line here',$ body,$ headers,$ CCUser);

}

//否则,让垃圾邮件发送者认为他们通过


解决方案

  foreach($ _POST as $ Field => $ Value){
if Value!=''){
$ body。=$ Field:$ Value\\\
;
}
}


I have this simple form to email in php. The nice thing about this form in the send php code is that I do NOT have to write out all the fields name.For example, I do NOT have to write this:

$msg .= "$_POST[ContactName]     Contact Name:     ContactName\n"

In my code, all I have to write is:

foreach ($_POST as $Field=>$Value) { 
$body .= "$Field: $Value\n";

The form is currently working perfectly. It is currently sending this:

ContactName: name here
BusinessName: business name here
EmailAddress: email@email.com
Email_Confirmation: 
PhoneNumber: 714-555-5555

However, I wish this to happen: If a field is left empty or the web user does not fill it a certain field box, then the form should NOT send this field. For example: web user decides to NOT fill in ContactName and or BusinessName. So the form should only send this format:

EmailAddress: email@email.com
Email_Confirmation: 
PhoneNumber: 714-555-5555

Noticed no mention of ContactName: and BusinessName:

Please help! I would appreciate it. -Michelle Ha.

Here is the php send code:

if the Email_Confirmation field is empty
if(isset($_POST['Email_Confirmation']) && $_POST['Email_Confirmation'] == ''){

    // put your email address here
    $youremail = 'bomandty@gmail.com';

    // prepare a "pretty" version of the message
    $body .= "Thank you for your request. We will get back with you soon.";
    $body .= "\n";
    $body .= "\n";
    foreach ($_POST as $Field=>$Value) { 
    $body .= "$Field: $Value\n"; 
    $body .= "\n";
    }

    $CCUser = $_POST['EmailAddress'];

    // Use the submitters email if they supplied one
    // (and it isn't trying to hack your form).
    // Otherwise send from your email address.
    if( $_POST['EmailAddress'] && !preg_match( "/[\r\n]/", $_POST['EmailAddress']) ) {
      $headers = "From: $_POST[EmailAddress]";
    } else {
      $headers = "From: $youremail";
    }

    // finally, send the message
    mail($youremail, 'subject line here', $body, $headers, $CCUser );

}

// otherwise, let the spammer think that they got their message through

解决方案

foreach ($_POST as $Field=>$Value) { 
if($Value != ''){
$body .= "$Field: $Value\n";
}
}

这篇关于只发送字段框填充。如果空,那么不要发邮件。 php表单发邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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