2012-11-26 33 views
0

在yii中我創建了sendemail功能。我正在使用郵件程序擴展名並在完成SMTP的所有設置後正常工作。我曾在控制器製造方法actionEmail原樣從數據庫中檢索email-id併發送郵件給他們

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 = "[email protected]"; 
    $mailer->Password = "abc"; 
    $mailer->From = "[email protected]"; 
    $mailer->FromName = "Balaee.com"; 
    $mailer->AddAddress('[email protected]'); 
    $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!"; 
    } 
} 

此方法實現correctly.It被髮送郵件到在mailer-> AddAdress.But現在我想以檢索從數據庫中對應於特定的用戶ID電子郵件ID和發送郵件給他提到的地址。即我不想插入此字段的硬編碼值。那麼我該如何做到這一點。請幫幫我。

回答

1

用於獲取用戶使用的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

+0

主席先生thanx回覆...我已經做出了改變,因爲你告訴。但它提供了一個錯誤 - 嘗試獲取非對象 –

+1

的屬性,檢查是否設置了$ user_model或不。如果存在與給定$ id相關的值,請檢查數據庫。 – Hemc

+0

我曾經通過將ID的硬編碼值作爲1來嘗試它,但仍然給出了相同的錯誤。 –

0

爲了這個做的,你可以從數據庫中使用以下查詢獲取電子郵件ID:

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

這裏X是您要發送電子郵件的人的用戶USER_ID。

並在收件人的電子郵件字段中提供此$email。謝謝。

相關問題