我的发送联系人php文件有问题。我希望根据访问者选择的主题发送电子邮件,例如:Padicure预订主题,电子邮件将发送至booking@example.com,而一般查询主题将发送至info@example.com
下面是我的PHP代码。
<?php
function emailswitch( $key ) {
$to = array(
'Pedicure Price Inquiry' => 'enquiry@gmail.com',
'Manicure Price Inquiry' => 'enquiry@gmail.com',
'Booking Time' => 'info@gmail.com'
);
$default = 'info@gmail.com';
return isset( $_POST['field_?'] ) && !empty($to[ $_POST['field_?'] ]) ? $to[ $_POST['field_?'] ]: $default; $from = $_POST['email'];
}
$from_name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['category'];
// collect data
$body = "";
foreach($_POST as $key => $val)
{
if($key != 'captcha')
$body .= ucfirst($key).": ".$val."\r\n";
}
// construct MIME PLAIN Email headers
$header = "MIME-Version: 1.0\n";
$header .= "Content-type: text/plain; charset=utf-8\n";
$header .= "From: $from_name <$email> \r\nReply-To: $from_name <$email> \r\nReturn-Path: <$email> \r\n";
// send email
$to = emailswitch( $subject );
$mail_sent = mail($to, $subject, $body, $header);
?>
下面是我的表单代码:
<div id="contact_form">
<form action="sendContact.php" method="post" onsubmit="return sendContact();">
<p><label id="lname" for="name">Full name:</label>
<input id="name" class="text" name="name" onblur="input_blur('name');" onfocus="input_focus('name');" type="text" /></p>
<p><label id="lemail" for="email">Email address:</label>
<input id="email" class="text" name="email" onblur="input_blur('email');" onfocus="input_focus('email');" type="text" /></p>
<div class="x"></div>
<p id="email-error" class="error">You must enter your email address!</p>
<p><label id="lcategory" for="category">Category:</label>
<select id="category" name="category" onblur="input_blur('category');" onfocus="input_focus('category');">
<option value="Pedicure Price Inquiry">Pedicure Price Inquiry</option>
<option value="Manicure Price Inquiry">Manicure Price Inquiry</option>
<option value="Booking Time">Booking Time</option></select></p>
<p><label id="lmessage" for="message">Message:</label>
<textarea id="message" name="message" onblur="input_blur('message');" onfocus="input_focus('message');" rows="" cols=""></textarea></p>
<div class="x"></div>
<p id="message-error" class="error">You must enter your message!</p>
<p><label id="lcaptcha" for="captcha"></label>
<input id="captcha" class="text" name="captcha" onblur="input_blur('captcha');" onfocus="input_focus('captcha');" type="text" /></p>
<div class="x"></div>
<p id="captcha-error" class="error">Are you sure about your calculations?</p>
<script type="text/javascript">
generate_captcha('lcaptcha');
</script>
<div class="x"></div>
<input class="submit" name="send_contact" type="submit" value="Send" /><input class="submit" type="reset" value="Reset" />
</form>
</div>
<div id="message_sent" style="display: none;">
<h1>Your message has been sent</h1>
<p>We'll contact you as soon as possible.</p>
<p>You can now <a href="./">go back</a> to home page.</p>
</div>
<!-- end of contact form --></div>
但是电子邮件没有发送出去......我想知道我的php文件中有什么错误导致该表单无法触发所述电子邮件......
答案 0 :(得分:0)
$to
未在外部上下文中定义。
$mail_sent = mail(emailswitch(), $subject, $body, $header);
答案 1 :(得分:0)
首先,将参数传递给您的函数,而不是直接尝试使用$_POST
:
function emailswitch( $key ) {
$to = array(
'Pedicure Price Inquiry' => 'info@example.com',
'Manicure Price Inquiry' => 'info@example.com',
'Booking Time' => 'booking@example.com'
);
$default = 'booking@example.com';
return (!empty($to[$key]))?$to[$key]:$default;
}
暂且不说:$from
内的emailswitch
元素永远不会被调用,也不应该在函数内部。
其次,请务必通过电子邮件呼叫呼叫该功能:
$to = emailswitch( $subject );
$mail_sent = mail($to, $subject, $body, $header);