我第一次使用PHPMailer发送联系表单,并引发错误“从空值创建默认对象”,这涉及我的gmail用户名和“对未定义方法stdClass :: isSMTP的调用”。
我在Github上删除了PHPMailer示例代码提供的try catch块,因此可以使用if else语句访问POST数据。因此,提交时我现在得到一个空白页,但是在尝试catch块到位时抛出的错误可能仍然是问题。我浏览了以前的帖子,到目前为止,还没有人回答这个问题。
HTML表单
<form action='/mail_handler.php' name='contact-form' id='contact-form' method='post' enctype='text/plain'>
<div class="form-group padding-top">
<label for="name">Name</label>
<input type="name" class="form-control" id="name" name='name' placeholder="your name">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name='email' placeholder="name@example.com">
</div>
<div class="form-group">
<label for="ceremony-type">Ceremony Type</label>
<select class="form-control" id="ceremony_type" name='ceremony_type'>
<option value='bespoke wedding ceremony'>bespoke wedding ceremony</option>
<option value='vow renewal'>vow renewal</option>
<option value='naming ceremony'>naming cermony</option>
<option value='coming-of-age'>coming-of-age</option>
</select>
</div>
<div class="form-group">
<label for="ceremony-enhancement">Ceremony Enhancement</label>
<select class="form-control" id="ceremony_enhancement" name='ceremony_enhancement'>
<option value='sand ceremony'>sand ceremony</option>
<option value='unity candle'>unity candle</option>
<option value='hand-fasting'>hand-fasting</option>
<option value='ring ceremony'>ring ceremony</option>
<option value='I have not decided yet'>I haven't decided yet</option>
</select>
</div>
<div class="form-group">
<label for="msg">Message</label>
<textarea class="form-control" id="msg" name='msg' rows="3"></textarea>
</div>
<input type="submit" name='submit' value='Send!' class="btn">
</form>
mail_handler.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
if (isset($_POST['submit'])) {
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
//Server settings
$mail->SMTPDebug = 1; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mygmail@gmail.com'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('email@domain.ie', 'Name');
$mail->addAddress('example@gmail.com', 'Name'); // Add a recipient
$mail->addReplyTo($_POST['email'], 'Description');
// Content
$mail->isHTML(true);
$mail->Subject = 'Query';
$mail->Body = $_POST['msg'];
$mail->AltBody = $_POST['msg'];
if($mail->send()){;
echo 'Message sent! Thanks for the getting in touch. I\'ll be in touch soon. Best Wishes';
} else {
echo "Something went wrong! You can contact me on email@domain.ie";
}
}
?>