希望你们能帮我解决这个问题。当有人完成我的表单时,我希望我收到的表单有一个“replyto”来实现客户端的电子邮件地址,而不是回复服务器。
所以基本上当我从客户端收到来自我希望能够回复的地方时,它会显示客户的电子邮件地址
真的希望你能帮助我
<form id="contact-form" method="post" action="contact-2.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="col-md-4">
<div class="form-group">
<label for="form_name"></label>
<input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Firstname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="form_email"></label>
<input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="form_subject"></label>
<input id="form_subject" type="text" name="subject" class="form-control" placeholder="Please enter your subject*" required="required" data-error="Subject is required">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label for="form_message"></label>
<textarea id="form_message" name="message" class="form-control" placeholder="Your Message*" rows="9" required data-error="Please,leave us a message."></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-4 col-md-offset-4">
<input type="submit" class="btn btn-primary btn-block btn-lg" value="Send message">
</div>
</div>
</form>
<?php
/*THIS FILE USES PHPMAILER INSTEAD OF THE PHP MAIL() FUNCTION*/
require 'PHPMailer-master/PHPMailerAutoload.php';
/**CONFIGURE EVERYTHING HERE*/
// an email address that will be in the From field of the email.
$fromEmail = 'Demo@gmail.com';
$fromName = 'Demo@gmail.com';
// an email address that will receive the email with the output of the form
$sendToEmail = 'Demo@gmail.com';
$sendToName = 'Demo@gmail.com';
// subject of the email
$subject = 'contact form';
// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'email' => 'Email', 'subject' => 'Subject', 'message' => 'Message');
// message that will be displayed when everything is OK :)
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';
/*LET'S DO THE SENDING*/
// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);
try{
if(count($_POST) == 0) throw new \Exception('Form is empty');
$emailTextHtml = "<h1>You have a new message from your contact form</h1><hr>";
$emailTextHtml .= "<table>";
foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailTextHtml .= "<tr><th>$fields[$key]</th><td>$value</td></tr>";
}
}
$emailTextHtml .= "</table><hr>";
$emailTextHtml .= "<p>Have a nice day,<br>Kind Regards,<br>Felleng Tours</p>";
$mail = new PHPMailer;
$mail->setFrom($fromEmail, $fromName);
$mail->addAddress($sendToEmail, $sendToName); // you can add more addresses by simply adding another line with $mail->addAddress();
$mail->addReplyTo($from);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->msgHTML($emailTextHtml); // this will also create a plain-text version of the HTML email, very handy
if(!$mail->send()) {
throw new \Exception('I could not send the email.' . $mail->ErrorInfo);
}
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e){
// $responseArray = array('type' => 'danger', 'message' => $errorMessage);
$responseArray = array('type' => 'danger', 'message' => $e->getMessage());
}
// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}// else just display the message
else {
echo $responseArray['message'];
}
?>
答案 0 :(得分:1)
目前尚不清楚定义$from
的位置,但您应该可以这样做:
$mail->addReplyTo($_POST['email']);
这将使用您的表单的电子邮件地址字段作为回复地址。
你有一个try / catch块,但是你没有告诉PHPMailer使用异常,你可以通过将true
传递给构造函数来实现:
$mail = new PHPMailer(true);
这样你就不需要抛出自己的异常了。您还使用旧版本的PHPMailer。