使用可选收件人创建电子邮件表单

时间:2017-07-08 05:55:00

标签: php arrays forms email smtp

这是我第一次设置电子邮件表单。

到目前为止,我根本没有运气让它完全正常工作。

我已经在我的问题中创建了一个非常简化的表单(以及php页面)演示。

如果有人能帮助我弄清楚我做错了什么,我们将不胜感激。

*注意:就像这个演示....我的表单也有多个收件人选项。

表格:

  <html>
  <head>
  <script>
  function onBlur(el) {if (el.value == '') {el.value = el.defaultValue;}}
  function onFocus(el) {if (el.value == el.defaultValue) {el.value = '';}}
  </script>
  <style>
  body{margin-left:20px; margin-top:30px; font-family:Arial, Helvetica, sans-serif; font-size:12pt;}
  .header{font-size:20pt; margin-bottom:10px;}
  .field{display:block; margin-bottom:10px; width:200px;}
  .subjectTitle{margin-top:20px;}
  .subject{width:500px; margin-bottom:10px;}
  .messageTitle{margin-top:10px; margin-bottom:4px;}
  .textarea{width:500px; height:200; display:block;}
  .recipient{margin-top:20px;}
  .recipient a{display:block; margin-bottom:4px;}
  .sendBtn{margin-top:30px;}
  </style>
  </head>
  <body>

  <div class="header">EMAIL FORM</div>

  <form method="post" action="emailFormHandler.php">

  <input class="field" name="firstName" type="text" value="First Name" onBlur="onBlur(this)" onFocus="onFocus(this)">
  <input class="field" name="lastName" type="text" value="Last Name" onBlur="onBlur(this)" onFocus="onFocus(this)">
  <input class="field" name="emailAddress" type="text" value="Email Address" onBlur="onBlur(this)" onFocus="onFocus(this)">

  <div class="subjectTitle">Subject</div>
  <input class="subject" name="subject" type="text">

  <div class="messageTitle">Message</div>
  <textarea class="textarea" name="message"></textarea>

  <div class="recipient">
  <a>Recipient</a>
  <select class="field" name="recipient">
  <option value="management@mywebsite.com">Management</option>
  <option value="accounting@mywebsite.com">Accounting</option>
  </select>
  </div>

  <input class="sendBtn" type="submit" name="sendBtn" value="Send Email">
  </button>

  </form> 
  </body>
  </html>

PHP PAGE :( emailFormHandler.php)

  <html>
  <head>
  </head>
  <body>
  <?php
  $subject=$_REQUEST['subject'];
  $message=$_REQUEST['message'];
  $firstName=$_REQUEST['firstName'];
  $lastName=$_REQUEST['lastName'];
  $email=$_REQUEST['email'];
  $headers="From: $email";
  $recipient = [$to,'management@mywebsite.com', 'accounting@mywebsite.com'];
  $sent=mail(implode(',',$recipient),$subject,$message,$headers);
  if($sent){print ('<a>Your Message Has Been Sent</a>');}
  else {print "Sorry, we encountered a problem.<br>Your email was not sent.<br>Please try again later.";}
  ?>
  </div>
  </body>
  </html>

我还没有让表单工作,无论有没有多个收件人下拉。我试过了两个。所以我显然需要对整个情况提出一些建议。

1 个答案:

答案 0 :(得分:0)

使用mail功能时,您传递了一些我不确定的其他值。但是如果你想发送到多个电子邮件地址......

$listOfRecipients = ['a1@mail.com', 'b2@mail.com'];
$sent=mail(implode(',',$listOfRecipients ),$subject,$message,$headers);

因此,您的收件人列表是以逗号分隔的电子邮件地址列表。在这种情况下,我是从一个数组构建的,但这只是一个例子。

您传递的$ firstname和$ lastname参数不会进入。

编辑: 也许您不想将其发送给多个收件人,您希望用户选择将其发送给哪个收件人,因此只需坚持使用

$sent=mail($to,$subject,$message,$headers);
相关问题