It seems you are trying to use it in Laravel that has a attachData
method which does'n call fopen
. You can use it like below code
$message->attachData($debug_data, 'debug.txt');
Question
I'm trying to attach a string as a plain text file to the swift mailer message as explained in the doc:
$debug_data = 'Exception: ' . $e_message . PHP_EOL;
$debug_data .= $file . ': ' . $line . PHP_EOL;
$debug_data .= $trace;
$attach = Swift_Attachment::newInstance($debug_data, 'debug.txt', 'text/plain');
$message->attach($attach);
but this gives me this error:
Error in exception handler: fopen(Content-Type: text/plain; name=debug.txt Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=debug.txt 1ay9wdWJsaWMvaW5kZXgucGhwKDQ5KTog SWxsdW1pbmF0ZVxGb3VuZGF0aW9uXEFwcGxpY2F0aW9uLT5ydW4oKQojMTMge21haW59): failed to open stream: No such file or directory in /vendor/swiftmailer/swiftmailer/lib/classes/Swift/ByteStream/FileByteStream.php:138
any idea?!
Solution
It seems you are trying to use it in Laravel that has a attachData
method which does'n call fopen
. You can use it like below code
$message->attachData($debug_data, 'debug.txt');
OTHER TIPS
Both answers above are not compatible with Symfony 4.0.
For Symfony 4.0:
$attachment = new Swift_Attachment($debug_data, 'debug.txt', 'text/plain');
$message->attach($attachment);
// I post it here, because it's top google result for 'swiftmailer attachment from string'
try something like this:
$attachment = Swift_Attachment::newInstance()
->setFilename('debug.txt')
->setContentType('text/plain')
->setBody($debug_data);