2012-06-01 12 views
10

電子メールに添付ファイルを追加したいとします。私はsfmailerクラスを使用しています。Symfonyの添付ファイルをメールに追加するには?

ここで私は以下の私のコードを与えている:

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

} 
+0

http://swiftmailer.org/docs/messages.html#attaching-files – Adam

答えて

26

あなたが迅速なメーラーを使用して電子メールに文書を添付するには、いくつかのオプションがあります。 the symfony docから

the swift mailer 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); 

そして他の多くの可能性。

3

また、リソース別にファイルを添付することもできます。

$message = Swift_Message::newInstance() 
    ->setFrom('[email protected]') 
    ->setTo('[email protected]') 
    ->setSubject('Subject') 
    ->setBody('Body') 
    ->attach(Swift_Attachment::newInstance($content, 'invoice.pdf','application/pdf')); 
関連する問題