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 根据以下文字信息,做EA模型图
  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥60 关机时蓝屏并显示KMODE_EXCEPTION_NOT_HANDLED,怎么修?
  • ¥66 如何制作支付宝扫码跳转到发红包界面