【问题标题】:Retrieving email-id from database and send mail to them从数据库中检索电子邮件 ID 并将邮件发送给他们
【发布时间】:2013-04-18 18:41:51
【问题描述】:

在 yii 中,我正在创建 sendemail 功能。在进行 SMTP 的所有设置后,我正在使用邮件扩展程序及其正常工作。我在控制器中将方法 actionEmail 设置为 -

public function actionEmail()
{
    $model=new User;
    $mailer = Yii::createComponent('application.extensions.mailer.EMailer');
    $mailer->IsSMTP();
    $mailer->IsHTML(true);
    $mailer->SMTPAuth = true;
    $mailer->SMTPSecure = "ssl";
    $mailer->Host = "smtp.gmail.com";
    $mailer->Port = 465;
    $mailer->CharSet = 'UTF-8';
    $mailer->Username = "abc@gmail.com";
    $mailer->Password = "abc";
    $mailer->From = "xyz@gmail.com";
    $mailer->FromName = "Balaee.com";
    $mailer->AddAddress('shilpa.kirad@shailani.com');
    $mailer->Subject = "welcome to Balaee";
    $mailer->IsHTML(true);
    //  $html = $this->renderPartial('myview',array('content'=>'Hello World'),true);

$mailer->正文 = "

欢迎


点击链接查看其他详情 ".$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    if($mailer->Send()) {
        echo "Please check mail";
        //Yii::app()->user->setFlash('register','Thank you for contacting us. We will respond to you as soon as possible.');
        //  $this->refresh();
    }
    else {
        echo "Fail to send your message!";
    }
}

此方法实现正确。它正在将邮件发送到 mailer->AddAdress 中提到的地址。但是现在我想从与特定用户 ID 对应的数据库中检索电子邮件 ID 并将邮件发送给他。即我不想为此字段插入硬编码值。那么我该怎么做。请帮帮我。

【问题讨论】:

    标签: yii extension-methods mailer


    【解决方案1】:

    for fetch 使用用户的 id 来获取电子邮件地址

    $user_model=User::model()->findByPk($id);
    

    并在电子邮件中设置为

    $mailer->AddAddress($user_model->email_id);
    

    其中 id 和 email_id 是表列名。 检查其他方式。 http://www.yiiframework.com/doc/guide/1.1/en/database.dao

    【讨论】:

    • Sirthanx 的回复...我已经按照您所说的进行了更改。但它给出了一个错误-尝试获取非对象的属性
    • 检查是否设置了 $user_model。检查数据库是否存在与给定 $id 相关的值。
    • 我通过将 id 的硬编码值传递为 1 进行了尝试。但仍然给出相同的错误。
    • 检查您将任何列定义为主键并且值为 1
    • yaa 先生,它的工作......实际上我没有任何值为 1 的记录。感谢帮助
    【解决方案2】:

    为此,您可以使用以下查询从数据库中获取电子邮件 ID:

      $email = SELECT email FROM USER WHERE user_id = "X"; 
    

    这里 X 是您要发送电子邮件的用户的 user_id。

    并在收件人的电子邮件字段中提供此$email。谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 2016-02-19
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2016-09-30
      • 2012-07-06
      相关资源
      最近更新 更多