Created
October 21, 2016 13:40
-
-
Save silvioq/fb07753e27d5f80892262c62a9f920ca to your computer and use it in GitHub Desktop.
Ejemplo de envío de test symfony
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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