簡體   English   中英

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

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

我正在嘗試將自動生成的郵件發送給選擇該選項的用戶。 現在,下面的代碼僅針對其收到的第一個值發送郵件。 其余值不發送郵件。 郵件代碼僅首次執行並中斷。 之后不回顯結果。 它顯示的錯誤: 致命錯誤:無法在第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;
}
?>

編輯:

現在您已經提供了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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM