我在我的联系页面中创建了一个表单,人们将在其中填写基本详细信息,当他们点击提交时,所有详细信息应发送到我的 Outlook 邮件,我为此使用了 phpmailer。一切都很好,但我不知道他们为什么不来,请检查下面的代码并告诉我,谢谢。
<?php
require 'PHPMailer/class.phpmailer.php';
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$phone = $_POST['phone'];
$text = $_POST['text'];
$body = "
<h3>Get in touch Form Data</h3> <br>
Your Name * = $name; <br>
Your Email * = $email; <br>
Your Subject * = $subject; <br>
Phone Number = $phone; <br>
Your Text = $text; <br>
";
$mail = new PHPMailer(true);
//smtp settings
$mail->IsSMTP();
$mail->isHTML(true);
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->Host = "smtp-mail.outlook.com";
$mail->WordWrap = 50;
$mail->Username = "[email protected]";
$mail->Password = "password";
$mail->SMTPSecure = 'tls';
$mail->SMTPDebug = 1;
$mail->IsSendmail();
$mail->From = "[email protected]";
$mail->FromName = "Get in touch Form Data";
//email settings
$mail->isHTML(true);
// $mail->setFrom($email);
$mail->addAddress("[email protected]");
$mail->Subject = ("Get in touch Form Data");
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
if(!$mail->Send())
{
echo "Mail Not Sent";
}
else
{
echo '<script language="javascript">';
echo 'alert("Thank You Contacting Us We Will Response You As Early Possible")';
echo '</script>';
}
}
?>
<form action="contact.php" method="POST" enctype="multipart/form-data">
<div class="row">
<div class="col-sm-6 mb-5">
<label class="form-label">
Your name
<span class="text-danger-alt">*</span>
</label>
<div class=" input-group form">
<input class="form-control " type="text" name="name" placeholder="Your full name">
</div>
</div>
<div class="col-sm-6 mb-5">
<label class="form-label">
Your email
<span class="text-danger-alt">*</span>
</label>
<div class=" input-group form">
<input class="form-control " type="email" name="email" placeholder="Your email">
</div>
</div>
<div class="col-sm-6 mb-5">
<label class="form-label">
Your subject
</label>
<div class=" input-group form">
<input class="form-control " type="text" name="subject" placeholder="Subject">
</div>
</div>
<div class="col-sm-6 mb-5">
<label class="form-label">
Your phone
</label>
<div class="input-group form">
<input class="form-control " type="text" name="phone" placeholder="Phone">
</div>
</div>
<div class="col-12">
<label class="form-label">
Your message
<span class="text-danger-alt">*</span>
</label>
<div class=" input-group form mb-5">
<textarea class="form-control " rows="6" name="text" placeholder="Type your message"></textarea>
</div>
</div>
<div class="col">
<div class="text-center">
<button type="submit" name="submit" class="btn btn-primary btn-wide">Send Message</button>
</div>
</div>
</div>
</form>
这是 PHP 代码的已解决版本
- 添加了 PHPMailer.php 和 SMTP.php 的要求
- 清理代码结构
<?php
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$phone = isset($_POST['phone']) ? $_POST['phone'] : ''; // Handling phone field if it's not provided
$text = $_POST['text'];
$body = "
<h3>Get in touch Form Data</h3> <br>
Your Name * = $name; <br>
Your Email * = $email; <br>
Your Subject * = $subject; <br>
Phone Number = $phone; <br>
Your Text = $text; <br>
";
$mail = new PHPMailer\PHPMailer\PHPMailer();
// SMTP settings
$mail->IsSMTP();
$mail->Host = "smtp-mail.outlook.com";
$mail->SMTPAuth = true;
$mail->Username = "[email protected]";
$mail->Password = "password";
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPDebug = 0; // Set to 0 for production
$mail->setFrom('[email protected]', 'Get in touch Form Data');
$mail->addAddress("[email protected]");
$mail->isHTML(true);
$mail->Subject = "Get in touch Form Data";
$mail->Body = $body;
if(!$mail->send()) {
echo "Mail Not Sent";
} else {
echo '<script language="javascript">';
echo 'alert("Thank You for Contacting Us! We Will Respond to You As Early As Possible.")';
echo '</script>';
}
}
?>
<form action="contact.php" method="POST" enctype="multipart/form-data">
<div class="row">
<div class="col-sm-6 mb-5">
<label class="form-label">
Your name
<span class="text-danger-alt">*</span>
</label>
<div class="input-group form">
<input class="form-control" type="text" name="name" placeholder="Your full name" required>
</div>
</div>
<div class="col-sm-6 mb-5">
<label class="form-label">
Your email
<span class="text-danger-alt">*</span>
</label>
<div class="input-group form">
<input class="form-control" type="email" name="email" placeholder="Your email" required>
</div>
</div>
<div class="col-sm-6 mb-5">
<label class="form-label">
Your subject
</label>
<div class="input-group form">
<input class="form-control" type="text" name="subject" placeholder="Subject">
</div>
</div>
<div class="col-sm-6 mb-5">
<label class="form-label">
Your phone
</label>
<div class="input-group form">
<input class="form-control" type="text" name="phone" placeholder="Phone">
</div>
</div>
<div class="col-12">
<label class="form-label">
Your message
<span class="text-danger-alt">*</span>
</label>
<div class="input-group form mb-5">
<textarea class="form-control" rows="6" name="text" placeholder="Type your message" required></textarea>
</div>
</div>
<div class="col">
<div class="text-center">
<button type="submit" name="submit" class="btn btn-primary btn-wide">Send Message</button>
</div>
</div>
</div>
</form>