I am trying to send auto-generated mails to users who opted for the option. Now, the code below is only sending mail for the first value it receives. It doesn't send mail for the rest of the values. The mail code executes for only the first time & breaks. Doesn't echo results after that. The Error it shows: Fatal error: Cannot redeclare class PHPMailer in /var/www/html/class.phpmailer.php on line 40
<?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();
?>
I am hereby including the Send_Mail.php file:
<?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;
}
?>
Now you have provided the Send_Mail.php file, I believe the answer is this: Move the require
line out of the Send_Mail
function to the top of the Send_Mail.php file:
<?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;
}
?>
Move the require
line to the first line of your code (ie. before the database connection code). I suspect there is something in the library that doesn't like being included more than once.
Like this:
<?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();
?>
Another option is to change it from require
to require_once
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.