Absolute path for assets in email without having a request object in Symfony2

symfony mailer
symfony mailer vs swiftmailer
symfony twig path
symfony/amazon-mailer
symfony mailer configuration

I'm sending emails using a Symfony2 command that is triggered by a cron job periodically. In the footer of the email I want to show the logo of the company. I know that in the case of emails, I have to use absolute paths for the images, and normally I would do something like this:

{% set abs_url = app.request.scheme ~'://' ~ app.request.httpHost %}
<img src="{{ abs_url ~ asset('bundles/common/images/logo_mialarma.png') }}">

But when the twig template of the email is being called by a command that is in turn called by a cron job, there isn't such request object.

How can I create the absolute path of the asset in this situation?

Create a configuration parameter to set your app's domain to.

Absolute URLs to Routes & Assets > Symfony Mailer: Love Sending , The HTML content of our email will use *this* template which is still *totally* static. Creating, Configuring & Sending the Email Object Normally in a template, we would use the {{ path() }} function to generate a Ok no errors a good sign! In the controller, we only called htmlTemplate() - we removed our call to the  The AbsolutePath property contains the path information that the server uses to resolve requests for information. Typically this is the path to the desired information on the server's file system, although it also can indicate the application or script the server must run to provide the information.

If someone is using a newer version, see here:

https://symfony.com/doc/current/console/request_context.html

You can set the Context and use global configs.

Absolute Asset Paths > All about Uploading Files in Symfony , One of the things I've noticed is that this word "uploads" - the directory where We have it here in our "liip_imagine" config file, the "oneup_flysystem. Call the second one uploads_base_url and set this to almost the same thing: / and then send an email that references an uploaded file, well suddenly, those paths will​  Using Absolute Paths. The link and asset helpers generate relative paths by default. To force the output to absolute paths, set the absolute option to true, as shown in Listing 9-14. This technique is useful for inclusions of links in an e-mail message, RSS feed, or API response.

try this in twig file:

{{ 'http://' ~ app.request.host }}

Sending Emails with Mailer (Symfony Docs), Out of the box, you can deliver emails over SMTP by configuring your .env file (​the an email, autowire the mailer using MailerInterface and create an Email object: path of the Twig template to render ->htmlTemplate('emails/signup.html.​twig') point this wherever your css files live '%kernel.project_dir%/assets/css': css  That's why in Symfony 3.4 we've introduced two new parameters called asset.request_context.base_path and asset.request_context.secure to define the default request context for assets. In this same application, if you define this new parameter:

Creating and Using Templates (Symfony Docs), the template path is the relative file path from `templates/` return This allows to evolve your application code without having to change the template code If you need absolute URLs for assets, use the absolute_url() Twig function as follows: The Request object that stores the current request data (depending on your  The Asset component manages assets through packages. A package groups all the assets which share the same properties: versioning strategy, base path, CDN hosts, etc. In the following basic example, a package is created to manage assets without any versioning:

How to manage static images with Symfony's Webpack Encore, Symfony's Webpack Encore is great to manage the first two places as it is able to As you can see, the path references the build folder: its full path is Then you can include the js and css files using the asset() function as shown above. So, basically, in this moment there is no best practice in place to  As with many applications, mine sends several e-mails. Some are sent instantaneously, some are sent from the spool. Whatever the case may be, you need absolute urls to links and assets. As an alternative to path(), Symfony provides the url() function for Twig, which creates an absolute url for a certain path.

Image absolute_url in email from CLI · Issue #15448 · symfony , Hi, I m trying to send emails from the CLI (with a valid scheme/host) As there is no request when on the CLI, this is empty, and it looks like it does not work correctly. asset's full path from a CLI (many email are in fact sent from the CLI, [Asset] CLI: use request context to generate absolute URLs #17503. The HttpFoundation Component: The HttpFoundation component defines an object-oriented layer for the HTTP specification. In PHP, the request is represented by some global variables ($_GET, $_POST, $_F

Comments
  • You mean something like app_domain: http://www.bcubo.com in twig -> globals (config.yml) and then in the template {{ app_domain ~ asset(...) }}?
  • Yea, that'll do. Although I wouldn't hardcode the http part in it.