travisco87 Posted October 8, 2014 Share Posted October 8, 2014 (edited) 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 October 8, 2014 by travisco87 Quote Link to comment https://forums.phpfreaks.com/topic/291518-newsletter-undefined-index-error/ Share on other sites More sharing options...
ginerjm Posted October 8, 2014 Share Posted October 8, 2014 In one spot you do a query that selects only 'emailaddr' from your table. Then later you try and reference a field named 'id'. How can that happen? Quote Link to comment https://forums.phpfreaks.com/topic/291518-newsletter-undefined-index-error/#findComment-1493079 Share on other sites More sharing options...
travisco87 Posted October 9, 2014 Author Share Posted October 9, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/291518-newsletter-undefined-index-error/#findComment-1493091 Share on other sites More sharing options...
jcbones Posted October 9, 2014 Share Posted October 9, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/291518-newsletter-undefined-index-error/#findComment-1493093 Share on other sites More sharing options...
travisco87 Posted October 9, 2014 Author Share Posted October 9, 2014 So I should test and do an if statement to correct it. I only need a for each statement when the condition selects all of the newsletter emails and id's. put more code in each of the if statements. Thank you for the help! Quote Link to comment https://forums.phpfreaks.com/topic/291518-newsletter-undefined-index-error/#findComment-1493095 Share on other sites More sharing options...
jcbones Posted October 9, 2014 Share Posted October 9, 2014 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); Quote Link to comment https://forums.phpfreaks.com/topic/291518-newsletter-undefined-index-error/#findComment-1493097 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.