由于我的PHP级别很低,因此我仅使用PHP来完成此任务。有人可以帮我实现我的代码自动回复。我想将电子邮件发送到已在电子邮件输入字段中输入的电子邮件地址。谢谢您的联系。...
一切正常。
同样,我如何实现该用户需要选中复选框以发送邮件
<?php
// Message Vars
$msg = '';
$msgClass = '';
// Check For Submit
if(filter_has_var(INPUT_POST, 'submit')){
// Get Form Data
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
// Check Required Fields
if(!empty($email) && !empty($name) && !empty($message)){
// Passed
// Check Email
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
// Failed
$msg = 'Please use a valid email';
$msgClass = 'alert-danger';
} else {
// Passed
$toEmail = '';
$subject = 'Contact Request From '.$name;
$body = '<h2>Contact Request</h2>
<h4>Name</h4><p>'.$name.'</p>
<h4>Email</h4><p>'.$email.'</p>
<h4>Message</h4><p>'.$message.'</p>
';
// Email Headers
$headers = "MIME-Version: 1.0" ."\r\n";
$headers .="Content-Type:text/html;charset=UTF-8" . "\r\n";
// Additional Headers
$headers .= "From: " .$name. "<".$email.">". "\r\n";
if(mail($toEmail, $subject, $body, $headers)){
// Email Sent
$msg = 'Your email has been sent';
$msgClass = 'alert-success';
} else {
// Failed
$msg = 'Your email was not sent';
$msgClass = 'alert-danger';
}
}
} else {
// Failed
$msg = 'Please fill in all fields';
$msgClass = 'alert-danger';
}
}
?>
<div class="container">
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control" value="<?php echo isset($_POST['name']) ? $name : ''; ?>">
</div>
<div class="form-group">
<label>Email</label>
<input type="text" name="email" class="form-control" value="<?php echo isset($_POST['email']) ? $email : ''; ?>">
</div>
<div class="form-group">
<label>Message</label>
<textarea name="message" class="form-control"><?php echo isset($_POST['message']) ? $message : ''; ?></textarea>
</div>
<br>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
</div>
答案 0 :(得分:0)
更新如果阻止将邮件发送到的位置。
if(mail($toEmail, $subject, $body, $headers)){
// Email Sent
$msg = 'Your email has been sent';
$msgClass = 'alert-success';
//add this line, it'll send mail to form submitter.
mail($email, "We received your mail", "Thank you for contacting us, we'll get you back soon" , $headers);
}