我遇到了PHP Mailer的问题。每次加载页面时都会发送一封空白电子邮件。
我确定这很简单(如果按下提交按钮可能会错过一个条件)来解决这个问题。
他们的文档似乎没有,但是当我第一次使用它时脚本首先工作,但是在最初几次之后开始在页面加载时发送电子邮件。谢谢!
<form method="post" action="">
<div class="form-group">
<input name="name" type="text" class="form-control" placeholder="Enter Name">
</div>
<div class="form-group">
<input name="email" type="email" class="form-control" placeholder="Enter Email">
</div>
<div class="form-group">
<input name="subject" type="text" class="form-control" placeholder="Enter Subject">
</div>
<div class="form-group">
<textarea name="message" class="form-control" rows="5" placeholder="Enter Message"></textarea>
</div>
<button name="submit" type="submit" value="submit" class="btn btn-submit">Submit</button>
</form>
<?php
require '/phpmailer/PHPMailerAutoload.php';
require_once('/phpmailer/class.phpmailer.php');
include("/phpmailer/class.smtp.php");
$emailaddress = 'info@newpointdigital.com';
$message=
'Name: '.$_POST['name'].'<br />
Email: '.$_POST['email'].'<br />
Subject: '.$_POST['subject'].'<br />
IP: '.$_SERVER['REMOTE_ADDR'].'<br /><br />
Message:<br /><br />
'.nl2br($_POST['message']).'
';
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
//$mail->SMTPDebug = 2; // 1 = errors and messages,2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "smtp.gmail.com"; // sets the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "info@newpointdigital.com"; // SMTP account username (the email account your created)
$mail->Password = "newpoint!@#$"; // SMTP account password (the password for the above email account)
$mail->SMTPSecure = 'ssl'; // Enable encryption, 'ssl' also accepted
$mail->CharSet = 'UTF-8'; // so it interprets foreign characters
$mail->SetFrom($_POST['email']);
$mail->AddReplyTo($_POST['email']);
$mail->Subject = "Contact form from ".$_POST['name']." ";
$mail->MsgHTML($message);
$mail->AddAddress($emailaddress);
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
答案 0 :(得分:3)
您不检查是否发生了表单提交。您只需在页面加载时调用您的电子邮件代码。有几种方法可以决定是否发生表单提交。一种方法是检查表单操作是否为POST:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// email code goes here
}
您还可以查看是否按下了提交按钮:
if (isset($_POST['submit'])) {
// email code goes here
}
其他要知道的事情:
答案 1 :(得分:0)
这种情况会导致您没有应用任何条件来运行,因此代码会在页面加载时逐行运行。
所以请保持您的邮件在提交检查条件下发送代码,以便在提交单击
后运行if(isset($_POST['submit'])) {
// your mail sending code
}
或使用任何发布数据,例如电子邮件检查
if(!empty($_POST['email'])) {
// your mail sending code
}