我有一个发送电子邮件的代码,但是我可以阻止它每次刷新时始终发送。我尝试使用if (isset($_POST["submit"])) {}
,以便在点击Submit
按钮时触发邮件,但它根本没用。
function two_dim_array_to_html_table($arr, $colcomments){
$ret = "<table border='1' width='auto' cellpadding='1px' cellspacing='0px' align='center'>\n";
$ret .= "\t<tr>\n";
foreach($arr[0] as $key => $val){
$ret .= "\t\t<th>".$colcomments[$key]."</th>\n";
}
$ret .= "\t</tr>\n";
foreach($arr as $row){
$ret .= "\t<tr>\n";
foreach($row as $column){
$ret .= "\t\t<td>".$column."</td>\n";
}
$ret .= "\t</tr>\n";
}
$ret .= "<table>\n";
return $ret;
}
if($result) {
$Body = "<html>\n"
. "<head>\n"
. "</head>\n"
. "<body>\n"
. two_dim_array_to_html_table($result, $colcomments)
. "</body>\n"
. "</html>\n";
//Setting up Mail
$mail = new PHPMailer();
if (EMAIL_USE_SMTP) {
// Set mailer to use SMTP
$mail->IsSMTP();
//useful for debugging, shows full SMTP errors
//$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
// Enable SMTP authentication
$mail->SMTPAuth = EMAIL_SMTP_AUTH;
// Enable encryption, usually SSL/TLS
if (defined(EMAIL_SMTP_ENCRYPTION)) {
$mail->SMTPSecure = EMAIL_SMTP_ENCRYPTION;
}
// Specify host server
$mail->Host = EMAIL_SMTP_HOST;
$mail->Username = EMAIL_SMTP_USERNAME;
$mail->Password = EMAIL_SMTP_PASSWORD;
$mail->Port = EMAIL_SMTP_PORT;
} else {
$mail->IsMail();
}
$mail->From = EMAIL_FROM_ADDRESS;
$mail->FromName = EMAIL_FROM_NAME;
$mail->AddAddress('test.test@domain.COM');
$mail->Subject = 'Daily Tasks - "'.date('d-m-Y h:m:s').'"';
$mail->WordWrap = 100;
$mail->IsHTML(true);
$mail->Body = $Body;
$mail->Send();
}
我尝试过使用它,但它似乎不起作用,
if(!$mail->Send()) {
header("Location: http://www.example.com");
exit;
}
答案 0 :(得分:0)
我能够通过添加另一个发送邮件和使用按钮来解决这个问题,
if(isset($_POST['sendmail'])){
//EMAIL function
if($mail) {
echo 'Mail successfully sent';
} else {
echo 'Mail failed to send';
}
}
答案 1 :(得分:-1)
您可以将“邮件”代码放在函数中,并在提交表单时调用该函数。这样您就可以确定只有在提交表单时才会发送邮件。