使用require字段从电子邮件发送所有数据

时间:2011-12-01 09:30:59

标签: php email

我正在我的网站中创建用户需求表单。为此,我对必填字段进行了一些验证,当用户填写表单并按提交并验证正确时,我会收到一封电子邮件地址。

但现在我想要电子邮件中的所有用户信息,例如姓名,城市,预算等......那么我需要在email.php脚本中进行哪些更改?

如果某些字段不是强制性的并且用户没有填写它们,它们会影响我的脚本吗?

我的脚本是:

<?php

  $to = "test@networkers.in";
  $subject = "a new requiremnet come!";
  $message = "Hi,\n\nyou get a new require";

  $headers = "MIME-Version: 1.0" . "\r\n";
  $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
  $headers .= "Sender-IP: " . $_SERVER["SERVER_ADDR"] . "\r\n";
  $headers .= "From: " . stripslashes($name) . " <" . $email . ">" . "\r\n";
  $headers .= "Priority: normal" . "\r\n";
  $headers .= "X-Mailer: PHP/" . phpversion();

  $sent = mail($to, $subject, $message, $headers) ;

  if ($sent) {
    echo "Your mail was sent successfully";
  } else {
    echo "We encountered an error sending your mail";
  }

?>

我收到的数据是:

$name = $_POST['fname'].' '.$_POST['lname'];
$email = $_POST['mail'];
$phone = $_POST['ph'];
$country = $_POST['country'];
$pt = $_POST['pt'];
$cwl = $_POST['cwl'];
$dyhyows = $_POST['dyhyows'];
$pb = $_POST['pb'];
$bpd = $_POST['bpd'];
$hdyhau = $_POST['hdyhau'];

1 个答案:

答案 0 :(得分:1)

您可以通过连接它们来添加字段到邮件正文中:

$message = "Hi,\n\nyou get a new require";
$message .= "\n Name:    " . $name;
$message .= "\n Email:   " . $email;
$message .= "\n Phone:   " . $phone;
$message .= "\n Country: " . $country;
$message .= "\n pt:      " . $pt;
$message .= "\n cwl:     " . $cwl;
$message .= "\n dyhyows: " . $dyhyows;
$message .= "\n pb:      " . $pb;
$message .= "\n bpd:     " . $bpd;
$message .= "\n hdyhau:  " . $hdyhau

用户未填写的任何字段都将为空白。