Skip to content

Instantly share code, notes, and snippets.

@gigo6000
Created June 29, 2011 03:03
Show Gist options
  • Save gigo6000/1052884 to your computer and use it in GitHub Desktop.
Save gigo6000/1052884 to your computer and use it in GitHub Desktop.
Symfony2 console/command-line command to send email
namespace VayaFeliz\SiteBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\Command;
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 SendEmailCommand extends Command
{
protected function configure()
{
$this
->setName('capifony:sendemail')
->setDescription('Send email')
->addArgument('emailfrom', InputArgument::REQUIRED, 'What\'s the sender email address?')
->addArgument('emailto', InputArgument::REQUIRED, 'What\'s the recipient email address?')
->addArgument('subject', InputArgument::REQUIRED, 'What\'s the email subject?')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$emailfrom = $input->getArgument('emailfrom');
$emailto = $input->getArgument('emailto');
$subject = $input->getArgument('subject');
if ($emailfrom && $emailto) {
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($emailfrom)
->setTo($emailto)
->setBody('test')
;
$this->container->get('mailer')->send($message);
$text = "Email sent!";
} else {
$text = 'Email not sent';
}
$output->writeln($text);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment