Skip to content

Instantly share code, notes, and snippets.

@silvioq
Created October 21, 2016 13:40
Show Gist options
  • Save silvioq/fb07753e27d5f80892262c62a9f920ca to your computer and use it in GitHub Desktop.
Save silvioq/fb07753e27d5f80892262c62a9f920ca to your computer and use it in GitHub Desktop.
Ejemplo de envío de test symfony
<?php
# src/AppBundle/Command/TestMailCommand.php
namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class TestMailCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName( 'app:mail:test' )
->setDescription( 'Envía un correo electrónico de prueba' )
->addArgument( 'to', InputArgument::REQUIRED, 'Destinatario del correo de prueba' )
->addArgument( 'from', InputArgument::REQUIRED, 'Quien envía' )
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$to = $input->getArgument( 'to' );
$from = $input->getArgument( 'from' );
$mailer = $this->getContainer()->get( 'mailer' );
$message = \Swift_Message::newInstance()
->setSubject( 'Mail de prueba' )
->setFrom( $from )
->setTo( $to )
->setBody( 'Este es sólo un mail de pruebas',
'text/plain' );
$mailer->send( $message );
# http://symfony.com/doc/current/cookbook/console/sending_emails.html
$spool = $mailer->getTransport()->getSpool();
$transport = $this->getContainer()->get('swiftmailer.transport.real');
$spool->flushQueue($transport);
$output->writeln( "<info>Enviado</info>" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment