Jump to content

How to return an array of emails.....


travisco87
Go to solution Solved by hansford,

Recommended Posts

I am trying to write a program that sends out a newsletter but I am lost at returning an array of emails. This is what I have

<?php
require("../PHPMailer/class.phpmailer.php");
include 'includes.php';
        $mail = new PHPMailer;
        $subject = $_POST['subject'];
        $text = $_POST['newsletterBody'];
        $unsubscribe = "<br /><br /><br /><br />If you would no longer like to receive these emails, please <a href='removeemail.php'>Click Here</a> to unsubscribe.";
        $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 ADDRESS****';                            // SMTP username
        $mail->Password = '****EMAIL PASSWORD*****';
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = 'ssl';                            // Enable encryption, 'ssl' also accepted

        $mail->From = '****EMAIL ADDRESS****';
        $mail->FromName = '****NAME****';
        
        $mail->AddAddress('****EMAIL ADDRESS****', '****NAME****');
        
        $email = getEmail("", $DBH);
        
        
        foreach ($email as $newEmail)
                {
                    $addEmail = $newEmail['email_addr'];
                    $mail->AddAddress($addEmail);
                    $DBH = null;
                }
        

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

        $mail->Subject = $subject;
        $mail->Body    = $text . "<br /><br /><br /> To unsubscribe to this email please  <a href='www.raven-consult.com/newsletter_remove.php'>Click Here</a>";
        $mail->AltBody = $text;

        if(!$mail->Send()) {
           echo 'Message could not be sent.';
           echo 'Mailer Error: ' . $mail->ErrorInfo;
           exit;
        }
        
        
        echo "Message has been sent <a href='newsletter.php'>Return back</a>";

?>

This is the portion of the includes file that you need to know. 

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();
            $postArray = $blogstmt->fetchAll(PDO::FETCH_ASSOC);
        }
        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_addr'], $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;
        }
    }
}

When I try and submit the newsletter to be sent out all I get back is this error. 

 

"Cannot use object of type nlEmails as array ......"

 

 

Link to comment
Share on other sites

  • Solution

You are attempting to iterate over an object like an array. $postArray is returned as an array of "nlEmails" objects.

To get the id and email properties of each "nlEmails" object:

        foreach ($email as $newEmail)
                {
                    $addEmail = $newEmail->email;
                    $mail->AddAddress($addEmail);
                    $DBH = null;
                }

 

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.