Symfony Mailer - Migrate addPart from SwiftMailer - symfony

I am migrating from SwiftMailer to Symfony Mailer in my application.
How to migrate addPart method of SwiftMailer to Symfony Mailer? In my case the content type is text/plain
->addPart('Plain text content', 'text/plain');
using
swiftmailer 6.1
symfony mailer 4.4

To set the plain text part of your email with Symfony Mailer, you can use
$email->text("...")
See https://symfony.com/doc/current/mailer.html#message-contents for a complete reference

You would just migrate it to
->text('Plain text content')
https://symfony.com/doc/current/mailer.html#message-contents

You can use like this
$email->from($fromEmail)
->to($toEmail)
->html($html_content) /// for html content
->text($text_content) /// for text content
->replyTo($email_info['reply_email'])
->subject($email_info['subject']);
hope this will help. Comment the line if you don't need html content or text content.

Related

Symfony 4.1 Custom twig error template

I'm trying to override twig html error templates as per the symfony documentation
So I tried to override the views like for other third party bundles by creating the following files in 'templates/bundles/TwigBundle/Exception' :
error.html.twig
error404.html.twig
However the default html twig error templates show up when I trigger a 404 error in production mode.
Testing with the /_error/{statusCode} route in dev mode displays my custom templates correctly.
EDIT: I found out that it was an issue with FriendsOfSymfony/FOSRestBundle. Using composer require friendsofsymfony/rest-bundle dev-master solved the issue

Rendering a template in Symfony2 Console application

I am using Symfony 2.8 and I want to render a template in a Symfony Console application. Basically I can render the template but it is trying to fetch assets from file protocol. I need those assets to be fetched from http protocol in order to creating a pdf from that template. I did try scopes but it did not work.
Actually the question is that I want the template to be rendered with correct asset and image urls. I want to get fully rendered html.

How to append content in file using symfony2 Filesystem?

As question is self explanatory. And very common but how can i do with the help of symfony2 components (Filesystem).
I do not see read function in filesystem.
After lot of googles, I could not fine. Please help!
There is no function in Symfony2's Filesystem component that appends content to a file. Just use php's built in function:
file_put_contents($file, $content, FILE_APPEND);
PHP Documentation on file_put_contents
Symfony2 API for FileSystem Component
EDIT:
Since Symfony 3.3, it is possible to append content in file:
$fs = new Filesystem();
$fs->appendToFile('logs.txt', 'Email sent to user#example.com');
Symfony Filesystem Component
Since Symfony 3.3, it is possible to append content in file:
$fs = new Filesystem();
$fs->appendToFile('logs.txt', 'Email sent to user#example.com');
Symfony Filesystem Component

How can I override the class Mailer on fosuser bundle?

I have a problem with the ressetting's email of FosUser Bundle because is in txt and I would like to send an email with html form, but the template is a txt template also in the class don't use the 'text/html' option.
So my question is how can I override a internal class (Mailer) of FosUser Bundle (not a controller).
I found the answer in the fosuser docs
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/1.2.0/Resources/doc/emails.md
"Sending HTML mails"

Setting defaults for number_format in Symfony 2

The twig documentation describes how to set the default parameters for the number_format extension. How can I set this in Symfony 2?
Symfony2 does not give access to core extension of twig. You can create a kernel.request event listener, inject twig service and in onKernelRequest method of the listener you have to add
$this->twig->getExtension('core')->setNumberFormat(3, ',', '.');
Since Symfony 2.7, through Symfony 4.2-dev I'm running on, this can now be configured.
# app/config/config.yml
twig:
number_format:
decimals: 3
decimal_point: ','
thousands_separator: '.'
See Symfony blog post: https://symfony.com/blog/new-in-symfony-2-7-default-date-and-number-format-configuration

Resources