简体   繁体   中英

Trying to get emails from database using Yiimail

A little background,

I'm creating a simple function that emails all users whenever the admin of a blog creates a new announcement. I want to gather all emails using an sql query and inputting them all inside the message body or perhaps looping sending the emails one at a time (though that may seem like it would take longer).

So far this is my code:

 public function emailAll()
    {
        $this->set_mail_settings();
        $message = new YiiMailMessage;        

        $request = Yii::app()->db->createCommand("
                                    SELECT email FROM persons WHERE party_id
                                ")->queryRow();

                $message->subject = 'Star Cruises - Login Information';
                $message->addTo('sendTo@someone.com');
                $message->from = Yii::app()->params['adminEmail'];
                Yii::app()->mail->send($message);
                exit();

        }

 private function set_mail_settings()
    {            
        $sysParam = SystemParameters::model()->getSystemParameters(
        array("smtp_host", "smtp_port", 'smtp_user','smtp_password')
    );

        Yii::app()->mail->transportOptions['username'] = $sysParam['smtp_user'];
        Yii::app()->mail->transportOptions['password'] = $sysParam['smtp_password'];
        Yii::app()->mail->transportOptions['host'] = $sysParam['smtp_host'];
        Yii::app()->mail->transportOptions['port'] = $sysParam['smtp_port'];
    }

The emailAll() function is called whenever an email is used.

My problem is the $request. I don't know how I would gather all the emails and putting them into the $message->addTo();

UPDATE: I was doing it fine until I reached this point:

    public function emailAll()
    {

        $this->set_mail_settings();
        $message = new YiiMailMessage;        

                $emails = Yii::app()->db->createCommand("SELECT group_concat(email) as em FROM persons")->queryRow();
                $email_ids = explode(",",$emails["em"]);
                $message->setBcc($email_ids);
                $message->setBody('Sample');
                $message->subject = 'New Announcement!';
                //$message->addTo('blah@blah.com');
                $message->from = Yii::app()->params['adminEmail'];
                Yii::app()->mail->send($message);


        }

 private function set_mail_settings()
    {            
        $sysParam = SystemParameters::model()->getSystemParameters(
        array("smtp_host", "smtp_port", 'smtp_user','smtp_password')
    );

        Yii::app()->mail->transportOptions['username'] = $sysParam['smtp_user'];
        Yii::app()->mail->transportOptions['password'] = $sysParam['smtp_password'];
        Yii::app()->mail->transportOptions['host'] = $sysParam['smtp_host'];
        Yii::app()->mail->transportOptions['port'] = $sysParam['smtp_port'];
    }

Then I got an error on this line from YiiMail.php:

 189         $msg = 'Sending email to '.implode(', ', array_keys($message->to))."\n".

And it stated that : array_keys() expects parameter 1 to be array, null given.

I understand what it wants, but I don't understand WHY this error occurred?

You can get all email ids with use of below query and use BCC instead of To for security reason.

$emails = Yii::app()->db->createCommand("SELECT group_concat(email) as em FROM persons WHERE party_id = $party_id")->queryRow();
$email_ids = explode(",",$emails["em"]);
$message->setBcc($email_ids); // use bcc to hide email ids from other users
$message->addTo("noreply@yourdomain.com"); //as To is required, set some dummy id or your own id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM