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 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao