Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) {

}
like image 575
nic Avatar asked Jun 01 '12 07:06

nic


2 Answers

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

From the symfony doc:

$message = Swift_Message::newInstance()
  ->setFrom('[email protected]')
  ->setTo('[email protected]')
  ->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.

like image 134
j0k Avatar answered Nov 17 '22 10:11

j0k


Also you can attach a file by resource.

$message = Swift_Message::newInstance()
  ->setFrom('[email protected]')
  ->setTo('[email protected]')
  ->setSubject('Subject')
  ->setBody('Body')
  ->attach(Swift_Attachment::newInstance($content, 'invoice.pdf','application/pdf'));
like image 3
Danil Pyatnitsev Avatar answered Nov 17 '22 09:11

Danil Pyatnitsev