Email FOS UserBundle如何将图像添加到注册电子邮件小枝
如何使用FOS UserBundle添加公司登录电子邮件小枝?我确实想在twig文件中这样做,不想创建一个定制的mailer类来添加一个图像 我已经覆盖了defaild模板并启用了swift邮件程序 迄今为止:Email FOS UserBundle如何将图像添加到注册电子邮件小枝,email,symfony,twig,fosuserbundle,Email,Symfony,Twig,Fosuserbundle,如何使用FOS UserBundle添加公司登录电子邮件小枝?我确实想在twig文件中这样做,不想创建一个定制的mailer类来添加一个图像 我已经覆盖了defaild模板并启用了swift邮件程序 迄今为止: <img id="logo" src="{{ asset('bundles/demo/img/logo_login.png') }}" alt="test!" /> 这会发送电子邮件,但是图像路径不完整(缺少主机部分) 当我添加app.request.getScheme
<img id="logo" src="{{ asset('bundles/demo/img/logo_login.png') }}" alt="test!" />
这会发送电子邮件,但是图像路径不完整(缺少主机部分)
当我添加app.request.getSchemeAndHttpHost()以获取主机时,邮件将不会被发送
<img id="logo" src="{{ app.request.getSchemeAndHttpHost() ~ asset('bundles/demo/img/logo_login.png') }}" alt="test!" />
有人有我可以尝试的解决方案或想法吗
提前感谢asset()
将为您网站上的图像创建一个url。这将使许多垃圾邮件过滤器敲响警钟,大多数电子邮件客户端只是阻止图像。幸运的是,您可以使用Swiftmailer嵌入图像
我假设您已经按照中的说明配置了自定义电子邮件模板
首先,在自定义用户包中创建一个类(当您有FosUserBundle时)或其他地方,例如Foo/BarBundle/Mailer/CustomUserMailer.php
:
并在您的服务中注册此类。yml:
# Service that extends the default twig mailer
foo_bar.custom_mailer:
class: Foo\BarBundle\Mailer\CustomUserMailer
public: false
arguments:
- '@mailer'
- '@router'
- '@twig'
- template:
confirmation: %fos_user.registration.confirmation.template%
resetting: %fos_user.resetting.email.template%
from_email:
confirmation: %fos_user.registration.confirmation.from_email%
resetting: %fos_user.resetting.email.from_email%
接下来,将以下内容放入config.yml文件中,让FosUserBundle知道它应该使用这个Mailer类:
fos_user:
service:
mailer: foo_bar.custom_mailer
假设您已将公司徽标放入src/Foo/BarBundle/Resources/images/your_fancy_logo.png
中,您现在可以在邮件模板中引用该图像:
<img width="xxx" height="xxx" border="0" alt="My fancy company logo" src="{{ company_logo_cid }}" />
这里有一个更干净的解决方案
//AppBundle/Mailers/CustomFOSUserMailer.php
名称空间AppBundle\Mailers;
使用AppBundle\Services\StoreService;
使用AppBundle\Services\ThemeService;
使用Symfony\Component\DependencyInjection\ContainerInterface;
使用Symfony\Component\Routing\Generator\UrlGeneratorInterface;
使用FOS\UserBundle\Mailer\TwigSwiftMailer作为基本类型;
/**
*此类重写默认的FOS用户邮件程序
*我们需要它将一些数据从我们的服务传递到细枝模板:
*>徽标路径
*>商店电话号码和电子邮件地址
*/
类CustomFOSUserMailer扩展了BaseType
{
/**@var-ThemeService*/
受保护的主题;
/**@var-StoreService*/
受保护的商店;
/**@var容器接口*/
受保护的集装箱;
公共函数构造(
ThemeService$主题,
StoreService$store,
ContainerInterface$container,
\Swift_Mailer$Mailer,
UrlGeneratorInterface$路由器,
\Twig_环境$Twig,
数组($参数)
{
父项::_构造($mailer、$router、$twig、$parameters);
$this->theme=$theme;
$this->store=$store;
$this->container=$container;
}
/**
*重写父类的sendMessage,以便可以呈现变量
*
*@param string$templateName
*@param数组$context
*@param string$fromEmail
*@param string$toEmail
*/
受保护的函数sendMessage($templateName、$context、$fromEmail、$toEmail)
{
$request=$this->container->get('request');
$baseurl=$request->getScheme()。:/“。$request->getHttpHost()。$request->getBasePath();
$context=array\u merge($context[
'storeLogoUrl'=>$baseurl.$this->theme->logoImage(),
'storePhone'=>this->store->getCurrentStore()->getPhone(),
'storeEmail'=>this->store->getCurrentStore()->getEmail(),
]);
父::sendMessage($templateName,$context,$fromEmail,$toEmail);
}
}
如果我的图像位于“web/assets/images”中,我使用.././web/assets/images或其他东西??plz@Krachleur您可以将内核rootdir注入到邮件程序中,并将“./web/assets/images”附加到其中。看见
<img width="xxx" height="xxx" border="0" alt="My fancy company logo" src="{{ company_logo_cid }}" />
// services.yml
services:
app.mailer.custom_fos_user_mailer:
class: AppBundle\Mailers\CustomFOSUserMailer
arguments:
- @app.theme
- @app.store
- @service_container
- @mailer
- @router
- @twig
-
template:
confirmation: %fos_user.registration.confirmation.template%
resetting: %fos_user.resetting.email.template%
from_email:
confirmation: %fos_user.registration.confirmation.from_email%
resetting: %fos_user.resetting.email.from_email%
// config.yml
fos_user:
service:
mailer: app.mailer.custom_fos_user_mailer
registration:
confirmation:
enabled: true
template: AppBundle:emails/user:confirm.html.twig
<img src="{{ storeLogoUrl }}" height="50">