Php 在yii中动态配置smtp发送邮件
我正在Yii on中使用SMTP PHPMailer扩展,并通过在控制器中创建函数sentMail成功发送邮件。但总的来说,我需要在main/config.php中进行如下配置:Php 在yii中动态配置smtp发送邮件,php,yii,smtp,phpmailer,Php,Yii,Smtp,Phpmailer,我正在Yii on中使用SMTP PHPMailer扩展,并通过在控制器中创建函数sentMail成功发送邮件。但总的来说,我需要在main/config.php中进行如下配置: 'Smtpmail'=>array( 'class'=>'application.extensions.smtpmail.PHPMailer', 'Host'=>"smtp.gmail.com", 'Username'=>'your@gmail.c
'Smtpmail'=>array(
'class'=>'application.extensions.smtpmail.PHPMailer',
'Host'=>"smtp.gmail.com",
'Username'=>'your@gmail.com',
'Password'=>'password here',
'Mailer'=>'smtp',
'Port'=>587,
'SMTPAuth'=>true,
'SMTPSecure' => 'tls',
),
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
}
}
如何获取从数据库到设置SMTP的所有配置?我曾尝试写入函数init()和它的_constructor(),但它不起作用。我甚至在控制器中配置mailSend函数如下:
'Smtpmail'=>array(
'class'=>'application.extensions.smtpmail.PHPMailer',
'Host'=>"smtp.gmail.com",
'Username'=>'your@gmail.com',
'Password'=>'password here',
'Mailer'=>'smtp',
'Port'=>587,
'SMTPAuth'=>true,
'SMTPSecure' => 'tls',
),
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邮件信息的型号的名称)我打赌这是您的问题:
$mail->IsSMTP=true
IsSMTP
是一种方法,您正在为其赋值。该名称具有误导性,因为它实际上指示mailer使用smtp。请尝试$mail->IsSMTP()
或$mail->Mailer='smtp'
。否则,我认为你的思路是对的。看一看。这可能就是你要找的。你能帮我弄清楚吗?