Jump to content

Recommended Posts

Nothing is being returned. Why? 

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require("../PHPMailer/class.phpmailer.php");
include 'includes.php';
        $mail = new PHPMailer;
        $subject = $_POST['subject'];
        $text = $_POST['newsletterBody'];
        $mail->IsSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'localhost';  // Specify main and backup server
        $mail->Port = '465';
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = '**EMAIL_USERNAME**';                            // SMTP username
        $mail->Password = '**EMAIL_PASSWORD**';
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = 'ssl';                            // Enable encryption, 'ssl' also accepted

        $mail->From = '**EMAIL_ADDRESS**';
        $mail->FromName = '**EMAIL_NAME';
        
    
        $email = getEmail("62", $DBH);
        
        
        foreach ($email as $newEmail)
                {
                    $addEmail = $newEmail->email;
                    $mail->AddAddress($addEmail);
                    $DBH = null;
                    $addEmail = "";
                }
        

        $mail->AddReplyTo('**EMAIL_REPLY_TO**', '**EMAIL_REPLY_TO_NAME**');
    
        $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
        $mail->IsHTML(true);                                  // Set email format to HTML

        $mail->Subject = $subject;
        $mail->Body    = $text;
        $mail->AltBody = $text;

        if(!$mail->Send()) {
           echo 'Message could not be sent.';
           echo 'Mailer Error: ' . $mail->ErrorInfo;
           exit;
        }
        
            $insertSql = "INSERT INTO newsletter_log (date, title, body) VALUES (?,?,?)";
            $insertParams = array(time(), $subject, $text);
            $newsletterAdd = $DBH->prepare($insertSql);
            $newsletterAdd->execute($insertParams)
        
        echo 'Message has been sent <a href='newsletter.php'>Return back</a>";

?>

This is the include section that you need

function getEmail($inId, $DBH)
    {
        if (!empty($inId))
        {
            $blogstmt = $DBH->prepare("SELECT email_addr FROM newsletter_emails WHERE id = :userId");
            $blogstmt->bindParam(":userId", $inId);
            $blogstmt->execute();
        }
        else
        {
            $blogstmt = $DBH->prepare("SELECT * FROM newsletter_emails");
            $blogstmt->execute();
        }
    
        $postArray = array();
        
        $results = $blogstmt->fetchAll(PDO::FETCH_ASSOC);
        foreach($results as $row){
            $myPost = new nlEmails($row["id"], $row['email'], $DBH);
            array_push($postArray, $myPost);
        }
        
        return $postArray;
        $DBH = null;
    }
 
class nlEmails {
    public $id;
    public $email;
    
    function __construct($inId=null, $inEmail=null, $DBH)
    {
        if(!empty($inId))
        {
            $this->id = $inId;
        }
        if(!empty($inEmail))
        {
        $this->email = $inEmail;
        }
    }
}

The database is created with the newsletter_emails "id", "first_name", "last_name", "email_addr". Thanks! 

Link to comment
https://forums.phpfreaks.com/topic/292017-not-returning-an-index-of-the-values/
Share on other sites

Also what is the the n1Emails class used for? You seem to return the results as an associative array and then convert the results into a object manually.

 

PDO can return the results as an object, use PDO::FETCH_OBJ instead of PDO::FETCH_ASSOC.

One more thing I just thought of - when you say nothing is being returned, do you mean you get no data, or you get no output at all? I'm pretty sure that jcbones' observation should have thrown an error because the referenced array index doesn't exist, but if you're not getting that, then you need to turn on error reporting.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.