how to add an attachment to an email in Symfony?

I want to add an attachment to an email. I am using sfmailer class.

Here I have given my code below:

$mail_body = '<p>custom html mail content</p>';
$message = Swift_Message::newInstance('Message title')
  ->setFrom(array('sender'))
  ->setTo(array('receiver'))
  ->setBody($mail_body, 'text/html', 'utf-8');

try {
  $this->getMailer()->send($message);
}
catch(Exception $e) {

}

Answers


You have several options to attach a document to an email using swift mailer.

From the symfony doc:

$message = Swift_Message::newInstance()
  ->setFrom('from@example.com')
  ->setTo('to@example.com')
  ->setSubject('Subject')
  ->setBody('Body')
  ->attach(Swift_Attachment::fromPath('/path/to/a/file.zip'))
;

$this->getMailer()->send($message);

And many others possibility from the swift mailer doc.


Also you can attach a file by resource.

$message = Swift_Message::newInstance()
  ->setFrom('from@example.com')
  ->setTo('to@example.com')
  ->setSubject('Subject')
  ->setBody('Body')
  ->attach(Swift_Attachment::newInstance($content, 'invoice.pdf','application/pdf'));

Need Your Help

Unable to import Eclipse project to Android Studio

android gradle android-studio

Whenever I try to import my Eclipse project to Android Studio I get the following error :

How do I display a byte array as an array of hex bytes or unsigned decimal numbers in the Eclipse Java debugger?

java eclipse types debugging

I want to view a byte array in the Eclipse (Helios Release, build id: 20100617-1415) Java debugger as an array of hex bytes (2 digits each) or unsigned decimal numbers? Is that possible? How?