使用 swiftmailer 和 symfony2 的文件条件附件 [英] Conditional attachment of file using swiftmailer and symfony2

查看:25
本文介绍了使用 swiftmailer 和 symfony2 的文件条件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 swiftmailer 附加邮件.有时我需要在邮件中附加图像文件,有时我必须发送不带附件的邮件.

I am currently working on attaching mails using swiftmailer. I need to attach a image file to the mail sometimes and sometimes I have to send the mail without the attachment.

这是我试过的

function sendMail($mailer,$subject , $to , $msg, $isAttachment, $attach)
    {
        $message = \Swift_Message::newInstance()
        ->setSubject($subject)
        ->setFrom('abc@gmail.com')
        ->setTo('xyz@gmail.com')
        ->setBody($msg)
        ->setContentType("text/html");

    if($isAttachment){

            $message->attach(Swift_Attachment::fromPath($attach));
        }

        $this->get('mailer')->send($message);

    } 

$isAttachment 是一个布尔变量,如果有附件,它有 1.

$isAttachment is a Boolean variable which has 1 if there is a attachment.

这是我得到的错误.

Fatal error: Class 'MyProject\FrontBundle\Controller\Swift_Attachment' not found in /var/www/ABC/src/Myproject/FrontBundle/Controller/trialController.php on line 187

这是我第一次在 swiftmailer 工作.. 如果这个问题听起来很幼稚,请原谅我.

This is the first time i am working in swiftmailer .. So pardon me if this question sounds naive.

提前致谢.

推荐答案

命名空间问题.你做了 \Swift_Message::newInstance 所以你必须做:

Namespacing problem. You did \Swift_Message::newInstance so you have to do:

$message->attach(\Swift_Attachment::fromPath($attach));

这篇关于使用 swiftmailer 和 symfony2 的文件条件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆