2017-07-08 88 views
0

这是我第一次设置电子邮件表单。使用可选收件人创建电子邮件表格

到目前为止,我一直没有运气让它工作。

我已经创建了一个非常简洁的窗体演示(和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="[email protected]">Management</option> 
    <option value="[email protected]">Accounting</option> 
    </select> 
    </div> 

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

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

PHP页面:(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,'[email protected]', '[email protected]']; 
    $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> 

我还没有拿到形式在所有的工作,有或没有多个收件人下拉。我尝试了两个。所以我明显需要一些关于整个情况的建议。

回答

0

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

$listOfRecipients = ['[email protected]', '[email protected]']; 
$sent=mail(implode(',',$listOfRecipients),$subject,$message,$headers); 

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

您传递的$ firstname和$ lastname参数没有进入它。

编辑: 也许你不Wnt信号将其发送给多个收件人,您希望用户选择将其发送给哪一个,因此只是

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

佰仁坚持......应该是什么我用$来衡量价值吗? –

+0

如果你有一个地址列表发送到'$ listOfRecipients = [$ to,'[email protected]'];' –

+0

Nigel Ren .... OK,所以,我编辑了窗体和PHP页面我上面的问题反映了你的建议......看起来是否正确? –

相关问题