【问题标题】:Form with multiple email fields to multiple recipients具有多个电子邮件字段的表单到多个收件人
【发布时间】:2011-09-28 03:04:18
【问题描述】:

这是我的 html 表单。用户将输入他/她希望将 html 电子邮件发送到的电子邮件地址。

<form id="form1" name="form1" method="post" action="">
<table width="400">
  <tr>
    <td>Please enter your email address:</td>
    <td<input type="text" name="email" id="email" /></td>
  </tr>
  <tr>
    <td>Please enter the email addresses you would like to notify below:</td>
    <td>
      &nbsp;
    </td>
  </tr>
  <tr>
    <td>Email:</td>
    <td>
      <input type="text" name="email1" id="email1" />
    </td>
  </tr>
  <tr>
    <td>Email:</td>
    <td><input type="text" name="email2" id="email2" />
    </td>
  </tr>
  <tr>
    <td>Email:</td>
    <td><input type="text" name="email3" id="email3" />
    </td>
  </tr>
  <tr>
    <td>Email:</td>
    <td><input type="text" name="email4" id="email4" />
    </td>
  </tr>
</table>
</form>

这有点像 php 代码。

<?php 
$ToEmail = '["email1"]["email2"]["email3"]["email4"]'; 
$EmailSubject = 'Check this out guys!'; 
$mailheader = "From: ".$_POST["email"]."\r\n"; 
$mailheader .= "Reply-To: "noreply@domain.com"\r\n"; 
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";  
mail(......) or die ("Failure"); 
?>

<script type="text/javascript">
    alert("Success! You have sent the notification to the emails you have entered.");
    <!--
    window.location = "form.html"
    //-->
  </script>

我该如何:
1。修改 PHP 代码,使其发送到用户输入的电子邮件?
2。通知的正文消息是 html 电子邮件。如何将其添加到 PHP 代码中?

我们非常感谢您的帮助。提前致谢!

【问题讨论】:

    标签: php html forms email


    【解决方案1】:

    看起来您需要将 email1、email2 等值 $_POST 到一个变量中,然后在 mail() 函数中将其用作 $to 的值 - 只需确保在每个变量后添加一个逗号:

    $to = $_POST['email1'] . ', ';
    $to .= $_POST['email2'] . ', ';
    $to .= $_POST['email3'];
    

    等等。去掉最后一封邮件的逗号,你应该准备好了。

    关于你邮件的内容,你应该可以发送 html 没有问题 - 只需将它存储在一个变量中以便以后使用,例如:

    $message = '
    <html>
      <head>
        <title>This is the HTML Email</title>
      </head>
      <body>
        <div id="container">
          <p>Welcome to the html!</p>
          <img src="../img/some_image.jpg" alt="some image"/>
        </div>        
      </body>
    </html>
    ';
    

    然后确保添加相关的 HTML 标头:

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    

    连同任何其他标题,例如:

    $headers .= 'From: HTML Email <you@example.com>' . "\r\n";
    

    然后使用您定义的变量调用mail()

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

    希望对您有所帮助。

    附言它在邮件功能定义中全部可用:mail()

    【讨论】:

    • 很好的答案!除了我不希望收件人将电子邮件“收件人:”字段视为收件人:you@example.com、hello@example.com、hi@example.com 我只希望他们在“收件人”中看到自己的电子邮件收件人:'字段
    • 快速谷歌搜索发现了这个帖子,pear-forum.org/topic2410.html,这很有趣。我推荐最后一个选项,即提供 To: 标头作为您自己的姓名/电子邮件地址(发件人的地址) - 我在收到的时事通讯中看到这样做了很多。 $headers .= 'To: Your Name &lt;your@email.com&gt;' . "\r\n";
    【解决方案2】:

    the php manual for mail

    例子:

    $toemails = "user@example.com, anotheruser@example.com";
    

    【讨论】:

    • $toemails 不是由开发者指定的,用户将根据表单确定邮件应该发送给谁。
    猜你喜欢
    • 1970-01-01
    • 2016-02-08
    • 2018-03-16
    • 2012-09-24
    • 2018-11-19
    • 1970-01-01
    • 2012-05-18
    相关资源
    最近更新 更多