9951 explained code solutions for 126 technologies


php-swiftmailerHow to add an attachment from a string using Swiftmailer?


Using Swiftmailer, you can add an attachment from a string by creating a Swift_Attachment object and passing the string as the first parameter. The second parameter is the filename of the attachment.

Example code

$attachment = Swift_Attachment::newInstance($string, 'filename.txt');

The code above will create a new attachment object with the string as the content and the filename as the second parameter.

Code explanation

  • Swift_Attachment::newInstance(): creates a new attachment object
  • $string: the string to be attached
  • 'filename.txt': the filename of the attachment

Helpful links

Edit this code on GitHub