Created
October 19, 2018 18:00
-
-
Save elmariachi111/0eec011dd6bd9804aee74e3a29e3fd6c to your computer and use it in GitHub Desktop.
PHP / Symfony: create zip file mail attachments in memory
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
{ | |
"type": "project", | |
"license": "proprietary", | |
"require": { | |
"php": "^7.1.3", | |
"ext-ctype": "*", | |
"ext-iconv": "*", | |
"badcow/lorem-ipsum": "^1.1", | |
"maennchen/zipstream-php": "^0.5.2", | |
"symfony/console": "*", | |
"symfony/flex": "^1.1", | |
"symfony/framework-bundle": "*", | |
"symfony/swiftmailer-bundle": "^3.2", | |
"symfony/yaml": "*" | |
}, | |
"require-dev": { | |
"symfony/dotenv": "*" | |
}, | |
"config": { | |
"preferred-install": { | |
"*": "dist" | |
}, | |
"sort-packages": true | |
}, | |
"autoload": { | |
"psr-4": { | |
"App\\": "src/" | |
} | |
}, | |
"autoload-dev": { | |
"psr-4": { | |
"App\\Tests\\": "tests/" | |
} | |
}, | |
"replace": { | |
"paragonie/random_compat": "2.*", | |
"symfony/polyfill-ctype": "*", | |
"symfony/polyfill-iconv": "*", | |
"symfony/polyfill-php71": "*", | |
"symfony/polyfill-php70": "*", | |
"symfony/polyfill-php56": "*" | |
}, | |
"scripts": { | |
"auto-scripts": { | |
"cache:clear": "symfony-cmd" | |
}, | |
"post-install-cmd": [ | |
"@auto-scripts" | |
], | |
"post-update-cmd": [ | |
"@auto-scripts" | |
] | |
}, | |
"conflict": { | |
"symfony/symfony": "*" | |
}, | |
"extra": { | |
"symfony": { | |
"allow-contrib": false, | |
"require": "4.1.*" | |
} | |
} | |
} |
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 | |
namespace App\Command; | |
use Badcow\LoremIpsum\Generator; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use ZipStream\ZipStream; | |
class SendMailCommand extends Command | |
{ | |
/** | |
* @var \Swift_Mailer | |
*/ | |
private $mailer; | |
public function __construct(\Swift_Mailer $mailer) | |
{ | |
parent::__construct('mailer'); | |
$this->mailer = $mailer; | |
} | |
protected function configure() | |
{ | |
$this | |
// the name of the command (the part after "bin/console") | |
->setName('send:mail'); | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$memoryStream = fopen('php://memory', 'w+b'); | |
$arch = new ZipStream(null, [ | |
ZipStream::OPTION_OUTPUT_STREAM => $memoryStream | |
]); | |
$arch->addFile("lorem-ipsum.txt", $this->getText()); | |
$arch->finish(); | |
rewind($memoryStream); | |
$data = stream_get_contents($memoryStream); | |
$attachment = new \Swift_Attachment($data, 'attachmemt.zip', 'application/zip'); | |
$message = (new \Swift_Message('Hello Email')) | |
->setFrom('[email protected]') | |
->setTo('[email protected]') | |
->setBody('Hello') | |
->attach($attachment); | |
; | |
$this->mailer->send($message); | |
} | |
private function getText() { | |
$lorem = new Generator(); | |
return implode("\n", $lorem->getParagraphs(100)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment