Thursday, 23 March 2017

Mail sending with template using swift mailer in Symfony

Create new class named MailSender.

namespace MyBundle;

use Swift_Mailer;
use Swift_Message;
use Twig_Environment;

class MailSender
{
    /** @var Swift_Message */
    private $mailer;

    /** @var Twig_Environment */
    private $twig;

    /**
     * @param Swift_Mailer     $mailer
     * @param Twig_Environment $twig
     */
    public function __construct(Swift_Mailer $mailer, Twig_Environment $twig)
    {
        $this->mailer = $mailer;
        $this->twig = $twig;
    }

    /**
     * Prepare message to sent.
     *
     * @param string $to
     * @param string $subject
     * @param string $twig
     * @param array  $parameters
     *
     * @return Swift_Message
     */
    private function prepare($to, $subject, $twig, array $parameters)
    {
        return  Swift_Message::newInstance()
            ->setSubject($subject)
            ->setFrom('no-reply@galitein.com')
            ->setTo($to)
            ->setBody(
                $this->twig->render($twig, $parameters),
                'text/html'
            );
    }

    /**
     * @param string $to
     * @param string $subject
     * @param string $twig
     * @param array  $parameters
     *
     * @return bool
     */
    public function send($to, $subject, $twig, array $parameters)
    {
        $this->mailer->send($this->prepare($to, $subject, $twig, $parameters));

        return true;
    }
}


* Set parameters for swift mailer in config.yml

# Swiftmailer Configuration
swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    spool:     { type: memory }
mailer details set in parameters.yml
 
* Register MailSender class as service in Services.yml
 
services:
    my_bundle.mail_sender:
        class: MyBundle\MailSender
        arguments:
            - '@mailer'
            - '@twig'
 

* Send mail from controller
 

$this->get('my_bundle.mail_sender') 

->send($email, 'Mail subject', 'Templates/contact.html.twig', [ 

 'contact_details' => $data, 

]);

Thursday, 16 March 2017

Copy all files from source to destination folder in Symfony

This example will show how you can copy all files from source folder to destination folder.

You can specify source and destination path in parameters.yml also or set static path in method too.

/**
  * copy images from source to destination.
  */
public function copyImagesSourceToDestination($imageSourcePath, $imageDestinationPath)
{
    if (!file_exists($imageSourcePath)) {
        throw new \LogicException(sprintf('Images source %s was not found', $imageSourcePath));
    }

    if (!file_exists($imageDestinationPath)) {
        mkdir($imageDestinationPath, 0777, true);
        chmod($imageDestinationPath, 0777);
    }

    $recursiveIteratorIterator = new \RecursiveIteratorIterator(
        new \RecursiveDirectoryIterator(
            $imageSourcePath,
            \FilesystemIterator::KEY_AS_PATHNAME
        ));

    foreach ($recursiveIteratorIterator as $file) {
        if (is_file($file)) {
            copy($file, $imageDestinationPath.'/'.substr($file, strrpos($file, '/') + 1));
        }
    }
}