Guest User

Untitled

a guest
Oct 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Command;
  4.  
  5. use Badcow\LoremIpsum\Generator;
  6. use Symfony\Component\Console\Command\Command;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Output\OutputInterface;
  9. use ZipStream\ZipStream;
  10.  
  11. class SendMailCommand extends Command
  12. {
  13. /**
  14. * @var \Swift_Mailer
  15. */
  16. private $mailer;
  17.  
  18. public function __construct(\Swift_Mailer $mailer)
  19. {
  20. parent::__construct('mailer');
  21. $this->mailer = $mailer;
  22. }
  23.  
  24. protected function configure()
  25. {
  26. $this
  27. // the name of the command (the part after "bin/console")
  28. ->setName('send:mail');
  29. }
  30.  
  31. protected function execute(InputInterface $input, OutputInterface $output)
  32. {
  33. $memoryStream = fopen('php://memory', 'w+b');
  34. $arch = new ZipStream(null, [
  35. ZipStream::OPTION_OUTPUT_STREAM => $memoryStream
  36. ]);
  37.  
  38. $arch->addFile("lorem-ipsum.txt", $this->getText());
  39. $arch->finish();
  40.  
  41. rewind($memoryStream);
  42. $data = stream_get_contents($memoryStream);
  43.  
  44. $attachment = new \Swift_Attachment($data, 'attachmemt.zip', 'application/zip');
  45. $message = (new \Swift_Message('Hello Email'))
  46. ->setFrom('from@example.com')
  47. ->setTo('address@example.com')
  48. ->setBody('Hello')
  49. ->attach($attachment);
  50. ;
  51.  
  52. $this->mailer->send($message);
  53. }
  54.  
  55. private function getText() {
  56. $lorem = new Generator();
  57. return implode("\n", $lorem->getParagraphs(100));
  58. }
  59. }
Add Comment
Please, Sign In to add comment