9951 explained code solutions for 126 technologies


php-swiftmailerHow to embed an image in Swiftmailer?


To embed an image in Swiftmailer, you need to use the Embed() method. This method takes the image file path as an argument and returns a Swift_Image object.

Example code

$image = $message->embed(Swift_Image::fromPath('path/to/image.jpg'));

Output example

Swift_Image Object
(
    [_name:protected] => Swift_Image
    [_file:protected] => path/to/image.jpg
)

Code explanation

  • $message: an instance of Swift_Message class
  • Embed(): a method of Swift_Message class
  • Swift_Image: a class
  • fromPath(): a static method of Swift_Image class
  • $image: a variable to store the Swift_Image object

Helpful links

Edit this code on GitHub