我尝试使用 Yii 扩展的 ERunactions 作为后台进程发送邮件,但它不起作用。
public function actionTimeConsumingProcess()
{
ERunActions::touchUrl($this->createAbsoluteUrl('test/SendMail'),null,null,array());
}
SendMail 方法:
public function actionSendMail()
{
yii::import('application.extensions.smtpmail.PHPMailer');
$mail = new PHPMailer(true);
try{
$dmmodel = DeliveryMethodModel::model()->findByPk(7);
$mail->Host =$dmmodel->host;// "smtp.gmail.com";
$mail->Username= $dmmodel->username;
$mail->Password= $dmmodel->password;
$mail->Mailer='smtp';
$mail->Port=$dmmodel->port;
$mail->SMTPAuth = true;
$mail->SMTPSecure = ($dmmodel->smtp_enableSSL==1?'ssl':($dmmodel->smtp_enableSSL==2 ? 'tls':''));
$mail->From = $dmmodel->email_from;
$mail->FromName = $dmmodel->name_from;
$mail->Subject = "Mail Configuration : Test Mail";
$mail->MsgHTML("This is an e-mail message sent automatically");
$mail->AddAddress($dmmodel->email_from, $dmmodel->name_from);
$mail->AddAddress($dmmodel->default_TO);
$mail->IsHTML(true);
$mail->Send();
// if(!$mail->Send()) {
echo "Msg sent";
//}else {
}
catch (phpmailerException $e)
{
echo "Mailer Error: " . $e->errorMessage();
}
}
调用 Controller 操作的ajax代码,
$.ajax({
type: 'POST',
url: "<?php echo CController::createUrl('operator/TimeConsumingProcess'); ?>",
success: function(data){
}
});
application.log 中没有显示错误,有人可以帮助我为什么它不起作用吗?
最佳答案
您的代码不完整(例如 SendMail 中没有使用 View 代码或模型),因此我无法说出确切的问题是什么,但总的来说您的示例是有效的。
我能够基于它组装一个小型工作应用程序,希望它有用。
您可以在 github 上找到完整代码.
Controller 代码:
<?php
Yii::import('ext.runactions.components.ERunActions');
class TestController extends Controller
{
/**
* This is the default 'index' action that is invoked
* when an action is not explicitly requested by users.
*/
public function actionIndex()
{
Yii::log('action: Index', 'error');
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
$this->render('index');
}
public function actionTimeConsumingProcess()
{
Yii::log('action: TimeConsumingProcess', 'error');
ERunActions::touchUrl(
$this->createAbsoluteUrl('test/SendMail'),null,null,array()
);
echo json_encode(array('status'=>'ok'));
}
public function actionSendMail()
{
Yii::log('action: SendMail', 'error');
}
}
查看代码:
<h1>Welcome to the TEST PAGE</h1>
<p>Run the test <a class="run-test" href="#">run</a>.</p>
<script type="text/javascript">
$('.run-test').click(function() {
alert('run');
$.ajax({
type: 'POST',
url: "<?php echo CController::createUrl('test/TimeConsumingProcess'); ?>",
success: function(data){
alert(data);
}
});
});
</script>
代码的作用是:
- Index 操作使用
run
链接呈现index
View run
链接启动对test/TimeConsumingProcess
的 ajax 调用actionTimeConsuminProcess
调用actionSendMail
- 所有操作都会记录到应用程序日志文件
protected/runtime/application.log
日志文件如下所示:
2016/01/13 00:51:48 [error] [application] action: Index
in /home/seb/web/yii-test-runactions/app/protected/controllers/TestController.php (14)
in /home/seb/web/yii-test-runactions/app/index.php (13)
2016/01/13 00:51:51 [error] [application] action: TimeConsumingProcess
in /home/seb/web/yii-test-runactions/app/protected/controllers/TestController.php (22)
in /home/seb/web/yii-test-runactions/app/index.php (13)
2016/01/13 00:51:51 [error] [application] action: SendMail
in /home/seb/web/yii-test-runactions/app/protected/controllers/TestController.php (31)
in /home/seb/web/yii-test-runactions/app/index.php (13)
您可以看到所有三个操作都被调用。
另请注意,SendMail
操作中有一些 echo
语句。除非您直接调用发送邮件操作(例如 http://mysite/controller/sendmail ),否则您将看不到它们。
您实际上可以这样做以确保发送邮件正常工作,但要与 runactions 一起测试它,最好使用 Yii::log(...) 在 application.log 中包含调试数据。
关于jquery - ERunactions 不工作 - Yii,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34522896/