简体   繁体   English

Send_Mail()在Amazon EC2 Server的while循环中不起作用

[英]Send_Mail() not working in while loop for Amazon EC2 Server

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 它显示的错误: 致命错误:无法在第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();
    ?>

I am hereby including the Send_Mail.php file: 我特此包含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;
}
?>

EDIT: 编辑:

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: 现在您已经提供了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;
}
?>

Original Answer 原始答案

Move the require line to the first line of your code (ie. before the database connection code). require行移动到代码的第一行(即,在数据库连接代码之前)。 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 . 另一种选择是将其从require更改为require_once

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM