我正在尝试向选择该选项的用户发送自动生成的邮件。现在,下面的代码只发送它收到的第一个值的邮件。它不会为其余值发送邮件。邮件代码仅在第一次执行时才会执行。休息。之后没有回应结果。它显示的错误:致命错误:无法在第40行的/var/www/html/class.phpmailer.php中重新声明类PHPMailer
<?php
//Including Send Mail Code
require 'Send_Mail.php';
//Connecting To Database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Selecting The Values From DB
$sql = "SELECT * FROM applications_meta WHERE job_stamp='1'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//Sending Mails To Emails From Results
$to = $row["email"];
$subject = "Test Mail Subject";
$body = "Hi<br/>Test Mail<br/>Amazon SES"; // HTML tags
Send_Mail($to,$subject,$body);
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
我现在包括Send_Mail.php文件:
<?php
function Send_Mail($to,$subject,$body)
{
require 'class.phpmailer.php';
$from = "abcd@abcs.com";
$mail = new PHPMailer();
$mail->IsSMTP(true); // SMTP
$mail->SMTPAuth = true; // SMTP authentication
$mail->Mailer = "smtp";
$mail->Host = "tls://XXXX-XXXX.XX-XXXX-X.XXXXXXXXX.XXX"; // Amazon SES server, note "tls://" protocol
$mail->Port = 465; // set the SMTP port
$mail->Username = "XXXXXXXXXXXXXXXXXX"; // SES SMTP username
$mail->Password = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; // SES SMTP password
$mail->SetFrom($from, 'Notification Centre');
$mail->AddReplyTo($from,'Administrator');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $to);
if(!$mail->Send())
return false;
else
return true;
}
?>
答案 0 :(得分:0)
现在你已经提供了Send_Mail.php文件,我相信答案是这样的:
将require
行从Send_Mail
函数移到Send_Mail.php文件的顶部:
<?php
require 'class.phpmailer.php'; //move this here
function Send_Mail($to,$subject,$body)
{
$from = "abcd@abcs.com";
$mail = new PHPMailer();
$mail->IsSMTP(true); // SMTP
$mail->SMTPAuth = true; // SMTP authentication
$mail->Mailer = "smtp";
$mail->Host = "tls://XXXX-XXXX.XX-XXXX-X.XXXXXXXXX.XXX"; // Amazon SES server, note "tls://" protocol
$mail->Port = 465; // set the SMTP port
$mail->Username = "XXXXXXXXXXXXXXXXXX"; // SES SMTP username
$mail->Password = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; // SES SMTP password
$mail->SetFrom($from, 'Notification Centre');
$mail->AddReplyTo($from,'Administrator');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $to);
if(!$mail->Send())
return false;
else
return true;
}
?>
将require
行移动到代码的第一行(即数据库连接代码之前)。我怀疑图书馆里有些东西不喜欢被多次包括在内。
像这样:
<?php
require 'Send_Mail.php'; //<---- moved here
//Connecting To Database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Selecting The Values From DB
$sql = "SELECT * FROM applications_meta WHERE job_stamp='1'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
//Sending Mails To Emails From Results
//----> LINE REMOVED HERE <----
$to = $row["email"];
$subject = "Test Mail Subject";
$body = "Hi<br/>Test Mail<br/>Amazon SES"; // HTML tags
Send_Mail($to,$subject,$body);
}
} else {
echo "0 results";
}
$conn->close();
?>
另一种选择是将其从require
更改为require_once
。