duanjiao2978 2014-09-29 18:37
浏览 148
已采纳

当我在Yii php中使用我的助手时,发送电子邮件不起作用

I'm using Yii framework while my question is probably intended to PHP expert. I have created a controller to send email from my web application, it works fine.

Given that, I intend to use email in several sontrollers in my app, I wanted to created a helper but that does not work. Email is not sent. (I'm using swiftmailer)

The code of the working controller is the following:

<?php

class MailController extends Controller
{
    /**
     * Declares class-based actions.
     */
    public function actionSendemail() {

    // Plain text content
    $plainTextContent = "This is my first line ;-)
This is my second row of text";

    // Get mailer
    $SM = Yii::app()->swiftMailer;

    // New transport      mailHost= localhost,    mailPort = 25
    $Transport = $SM->smtpTransport(Yii::app()->params['mailHost'], Yii::app()->params['mailPort']);

    // Mailer
    $Mailer = $SM->mailer($Transport);

    // New message
    $Message = $SM
        ->newMessage('My subject')
        ->setFrom(array('test1@localhost.localdomain' => 'Example Name'))
        ->setTo(array('myemail@domain.com' => 'Recipient Name'))
     //   ->addPart($content, 'text/html')
        ->setBody($plainTextContent);

    // Send mail
    $result = $Mailer->send($Message);

}
}

The helper code is the following

<?php
// protected/components/Email.php

class Email {
    public static function sendEmail($subject, $from, $to, $body)
    {
        // Get mailer
        $SM = Yii::app()->swiftMailer;
        // New transport
        $Transport = $SM->smtpTransport(Yii::app()->params['mailHost'], Yii::app()->params['mailPort']);
        // Mailer
        $Mailer = $SM->mailer($Transport);
        // New message
        $Message = $SM
            ->newMessage($subject)
            ->setFrom(array($from => 'Example Name'))
            ->setTo(array($to => 'Recipient Name'))
         //   ->addPart($content, 'text/html')
            ->setBody($body);

        // Send mail
        $result = $Mailer->send($Message);
    }
}

the way I call it is the following

$subject= 'My subject';
$from = Yii::app()->params['adminEmail'];   // adminEmai is a globalparam like above controller
$to='xxxx@xxx.com';
$body='my body';
Email::sendEmail($subject, $from, $to, $body);

when I run this code, I have no error, but I dont receive the email.

Thank you for your help.

  • 写回答

1条回答 默认 最新

  • drrhr20884 2014-09-29 19:07
    关注

    I found my error.

    my global parameters in config.php was not set correctly.

    So content I put in the from field was not recognized by my hmailserver which is configured for the moment with the content test1@localhost.localdomain

    sorry for the question and thanks

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程