1
我在Yii上使用SMTP PHPMailer擴展http://www.yiiframework.com/extension/smtp-mail/,並且通過在Controller中創建函數sentMail,成功發送了郵件。但總體而言,它需要我像這樣在main/config.php中配置:動態配置smtp在yii發送郵件
'Smtpmail'=>array(
'class'=>'application.extensions.smtpmail.PHPMailer',
'Host'=>"smtp.gmail.com",
'Username'=>'[email protected]',
'Password'=>'password here',
'Mailer'=>'smtp',
'Port'=>587,
'SMTPAuth'=>true,
'SMTPSecure' => 'tls',
),
如何從數據庫中獲取所有配置以設置SMTP?我試圖寫入它的函數init()和_constructor(),但它不工作。在控制器和I甚至config功能mailSend這樣的:
public function mailsend($to, $from = '', $from_name, $subject, $message, $cc = array(), $attachment = array()) {
$mail = Yii::app()->Smtpmail;
$mail->Host = Smtpconfig::model()->findByPk(1)->host;
$mail->Username = Smtpconfig::model()->findByPk(1)->username;
$mail->Password = Smtpconfig::model()->findByPk(1)->matkhau;
$mail->IsSMTP = true;
$mail->Port = Smtpconfig::model()->findByPk(1)->port;
$mail->SMTPSecure = Smtpconfig::model()->findByPk(1)->phuongthuc;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->MsgHTML($message);
$mail->AddAddress($to, "");
$mail->CharSet = "utf-8";
// Add CC
if (!empty($cc)) {
foreach ($cc as $email) {
$mail->AddCC($email);
}
}
// Add Attchments
if (!empty($attachment)) {
foreach ($attachment as $attach) {
$mail->AddAttachment($attach);
}
}
if (!$mail->Send()) {
return false; // Fail echo "Mailer Error: " . $mail->ErrorInfo;
} else {
return true; // Success
}
}
但是不行,無論是。 (Smtpconfig是包括管理smtp郵件信息的模型名稱)
查看[工廠方法模式](https://en.wikipedia.org/wiki/Factory_method_pattern#PHP)。這可能是你正在尋找的東西。 – Tom 2015-04-03 14:07:37
你能幫助我更清楚嗎? – 2015-04-03 14:30:27