我有这个简单的表格在 php 中发送电子邮件。发送 php 代码中这个表单的好处是我不必写出所有字段名称。例如,我不必写这个:
$msg .= "$_POST[ContactName] Contact Name: ContactName\n"
在我的代码中,我只需要写:
foreach ($_POST as $Field=>$Value) {
$body .= "$Field: $Value\n";
该表格目前运行良好。它目前正在发送:
ContactName: name here
BusinessName: business name here
EmailAddress: email@email.com
Email_Confirmation:
PhoneNumber: 714-555-5555
但是,我希望这种情况发生:如果某个字段留空或网络用户未填写某个字段框,则表单不应发送此字段。例如:网络用户决定不填写 ContactName 和/或 BusinessName。所以表单应该只发送这种格式:
EmailAddress: email@email.com
Email_Confirmation:
PhoneNumber: 714-555-5555
注意到没有提及 ContactName: 和 BusinessName:
请帮忙!我会很感激。-米歇尔哈。
这是php发送代码:
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