Jump to content

Newsletter "Undefined Index" error


travisco87

Recommended Posts

I am working on building a newsletter with PHP. I have gotten everything to work but the pulling of the email address. 

 

First is the sendnl.php file 

<?php
require("../PHPMailer/class.phpmailer.php");
include 'includes.php';

        $mail = new PHPMailer;
        $subject = $_POST['subject'];
        $text = $_POST['newsletterBody'];
        $mail->IsSMTP();                                      
        $mail->Host = 'localhost';  
        $mail->Port = '465';
        $mail->SMTPAuth = true;                               
        $mail->Username = '******USERNAME';          
        $mail->Password = '*****PASSWORD';
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = 'ssl';                           

        $mail->From = '***EMAIL';
        $mail->FromName = '****FROMNAME';
        
    
        $email = getEmail("73", $DBH);
        
        foreach ($email as $nlEmail)
        {
            $addEmail = $nlEmail->email;
            $mail->AddAddress($addEmail);
            $DBH = null;
            $addEmail = "";
        }
        

        $mail->AddReplyTo('******EMAIL', '*****EMAILNAME');
    
        $mail->WordWrap = 50;                                 
        $mail->IsHTML(true);                                  

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

        if(!$mail->Send()) {
           echo 'Message could not be sent.';
           echo 'Mailer Error: ' . $mail->ErrorInfo;
           exit;
        }

        echo 'Message has been sent';

?>

Next is part of the includes file going over the function and class


function getEmail($inId, $DBH)
    {
        if (!empty($inId))
        {
            $blogstmt = $DBH->prepare("SELECT email_addr FROM newsletter_emails WHERE id = :userId");
            $blogstmt->bindParam(":userId", $inId, PDO::PARAM_STR, 6);
            $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;
        }
    }
}

It gives me an Unidentified index error message for the "id" and "email" used in the getEmail function.

Thanks for any help or pointers. 

Edited by travisco87
Link to comment
Share on other sites

In the database it looks like this id, first_name, last_name and email_addr.

 

In the function I have if the id is left blank to pull all rows from that table. If an id is inserted, it will get the email address of that one id. 

 

Is that what you are asking? 

Link to comment
Share on other sites

No, he is stating that you are only getting the email_addr from the database query, but then you are trying to access id and email from the returned values.  They don't exist, because you didn't ask for them from the database.  Actually, one of those indexes (email) isn't even in the database.

Link to comment
Share on other sites

No, simply fix your query statement, and then fix your indexes.  Simple fix:

change

 

"SELECT email_addr FROM newsletter_emails WHERE id = :userId"

To:

"SELECT id,email_addr FROM newsletter_emails WHERE id = :userId"

Then

Change

 

$myPost = new nlEmails($row["id"], $row['email'], $DBH);

To

 

$myPost = new nlEmails($row["id"], $row['email_addr'], $DBH);
Link to comment
Share on other sites

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.