Shr*_*tty 0 swiftmailer symfony
我目前正致力于使用swiftmailer附加邮件.我有时需要将图像文件附加到邮件中,有时我必须在没有附件的情况下发送邮件.
这是我试过的
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);
}
Run Code Online (Sandbox Code Playgroud)
$isAttachment是一个布尔变量,1如果有附件就有.
这是我得到的错误.
Fatal error: Class 'MyProject\FrontBundle\Controller\Swift_Attachment' not found in /var/www/ABC/src/Myproject/FrontBundle/Controller/trialController.php on line 187
Run Code Online (Sandbox Code Playgroud)
这是我第一次在swiftmailer工作..如果这个问题听起来很幼稚,请原谅我.
提前致谢.
命名空间问题.你这样\Swift_Message::newInstance做了,你必须这样做:
$message->attach(\Swift_Attachment::fromPath($attach));
Run Code Online (Sandbox Code Playgroud)