I am sending an email using swiftmailer in symfony2, but I would like to add a specified PDF file as a file attachment to the email. How would I do that?
Here is my current code:
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('send#example.com')
->setTo('recipient#example.com')
->setBody(
$this->renderView(
'HelloBundle:Hello:email.txt.twig',
array('name' => $name)
)
)
;
$this->get('mailer')->send($message);
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);
If you want to upload a file from buffer, you can do this:
$attach=getPdfFunction(); //binary
Swift_Attachment::newInstance($attach, 'document.pdf','application/pdf');
You can add your attachement using this line:
$message->attach(\Swift_Attachment::fromPath($attach));
The following code should do it:
$attachment = \Swift_Attachment::fromPath('subpath/to/attachment');
$message->attach($attachment);
Related
I want to attach a pdf file that I created with TCPDF to my email.
This is how I do it:
$fileatt = $pdf->Output('file.pdf', 'E');
header("Location: mailto:someone#somepage.com?subject=my report&attachment=".$fileatt."&body=see attachment");
But I get an error message Warning: Header may not contain more than a single header
UPDATE:
Fred -ii- suggested to use Swiftmail to get this problem work:
$fileatt = $pdf->Output('quotation.pdf', 'E');
require_once 'swiftmailer/lib/swift_required.php';
// Create the mail transport configuration
$transport = Swift_MailTransport::newInstance();
// Create the message
$message = Swift_Message::newInstance();
$message->setTo(array(
"someone#somewhere.com" => "Someone",
));
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("You're our best client ever.");
$message->setFrom("someone#somewhere.com", "Your bank");
// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);
$message->attach(
Swift_Attachment::$fileatt);
But I still cannot figure out, how to combine this with the mailto
Im really stuck with this swiftmail method of sending email with attachment. My emails never seem to be delivered. I send the email and it just ruturns without any errors but when I check my mail nothing is delivered. Please help! I troubleshooted everything and everything works except for the attach() function. I dont know whats wrong. Heres my code.
<?php
//I didnt add my validations and variables above.....
require_once('./swiftmailer/lib/swift_required.php');
$transport = Swift_SmtpTransport::newInstance('smtp.host.com', 25)
->setUsername('user')
->setPassword('pass');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setSubject('Online Form')
->setFrom(array($from_email => $full_name))
->setTo(array('email#mail.com' => 'Jack'))
->setBody(''.$message_temp.'')
->attach(Swift_Attachment::fromPath($_FILES['attachment']['tmp_name'])
->setFilename($_FILES['attachment']['name']));
$result = $mailer->send($message);
?>
Oops! Never mind, I solved it myslef.
I assigned $_FILES['attachment']['tmp_name'] to a temporary variable and it worked!
Dont know why but that solved it for me.
Here's my code;
// Swiftmail commands ====================================
require_once('./swiftmailer/lib/swift_required.php');
$transport = Swift_SmtpTransport::newInstance('smtp.host.com', 587)
->setUsername('email#host.com')
->setPassword('pass');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setSubject($subject_temp)
->setFrom(array($from_email => $full_name))
->setTo(array('email#host.com' => 'Jack'))
->setBody($message_temp)
->attach(Swift_Attachment::fromPath($file_temp_name)
->setFilename($name_of_file));
$result = $mailer->send($message);
// Swiftmail commands ====================================
Where $file_temp_name = $_FILES['attachment']['tmp_name']; and
$name_of_file = basename($_FILES['attachment']['name']);
I am trying to remove the attachment files after sending an email with Symfony 2.1 and Swiftmailer but if I delete the file before returning a response object (a redirect), the email does not send.
I suppose this is because symfony sends the email in the response, so when the email has send the attachment has been removed already.
For example:
<?php
// DefaultCotroller.php
$message = \Swift_Message::newInstance($subject)
->setFrom('no-reply#dasi.es')
->setTo($emails_to)
->setBody($body, 'text/html')
->attach(\Swift_Attachment::fromPath('backup.rar'));
$this->get('mailer')->send();
unlink('backup.rar'); // This remove the file but doesn't send the email!
return $this->redirect($this->generateUrl('homepage'));
An option is to create a crontab to clean the files, but I prefer not using it.
Thanks!
You can look at the code that processes memory spools here:
https://github.com/symfony/SwiftmailerBundle/blob/master/EventListener/EmailSenderListener.php
This is used to batch the emails to be sent.
You can add this after your send() call and before your unlink() call to mimic the behavior of sending an email
$transport = $this->container->get('mailer')->getTransport();
$spool = $transport->getSpool();
$spool->flushQueue($this->container->get('swiftmailer.transport.real'));
I am not sure, but the message spool might cause this problem. In SF2 memory spool is used by default, which means the messages are being sent on the kernel terminate event.
So you'd have to flush the spool before deleting the file.
If this is the cause of your problem, look here for a well explained solution:
http://sgoettschkes.blogspot.de/2012/09/symfony-21-commands-and-swiftmailer.html
In order to complete the very good answer of james_t, if you use multiple mailers some changes are needed.
Replace
// Default mailer
$mailer = $this->container->get('mailer');
$subject = '...';
$from = '...';
$to = '...';
$body = '...';
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from)
->setTo($to)
->setBody($body, 'text/html')
;
// Put e-mail in spool
$result = $mailer->send($message);
// Flush spool queue
$transport = $mailer->getTransport();
$spool = $transport->getSpool();
$realTransport = $this->container->get('swiftmailer.transport.real')
$spool->flushQueue($realTransport);
By
// Custom mailer
$mailerServiceName = 'myCustomMailer';
$customMailer = $this->container->get("swiftmailer.mailer.".$mailerServiceName);
$subject = '...';
$from = '...';
$to = '...';
$body = '...';
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from)
->setTo($to)
->setBody($body, 'text/html')
;
// Put e-mail in spool
$result = $customMailer->send($message);
// Flush spool queue
$transport = $customMailer->getTransport();
$spool = $transport->getSpool();
$realTransport = $this->container->get('swiftmailer.mailer.'.$mailerServiceName.'.transport.real');
$spool->flushQueue($realTransport);
I am successully able to send emails via Amazon SES with the code below, but I am trying to set a return path and it doesnt work. When i use ->setReturnPath('bounce#example.com') the emails do not send at all. Can anyone shed some light why, or know how to get it to work?
Any help would be great!
This is the latest swiftmailer (4.2.2)
require_once 'lib/swift_required.php';
require_once 'classes/Swift/Transport/AWSTransport.php';
require_once 'classes/Swift/AWSTransport.php';
require_once 'classes/Swift/AWSInputByteStream.php';
define( 'AWSAccessKeyId', 'XXXXX' );
define( 'AWSSecretKey', 'XXXXX' );
//Create the Transport
$transport = Swift_AWSTransport::newInstance( AWSAccessKeyId, AWSSecretKey );
$transport->setDebug( true ); // Print's the response from AWS for debugging.
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance( $transport );
//Create the message
$message = Swift_Message::newInstance()
->setSubject( 'Sample Subject' )
->setFrom( array( 'test#example.com' ) )
->setTo( array( 'to#someone.com' ) )
->setBody( $message_body, 'text/html' )
->addPart( "Please use a HTML compatible web browser to view this email.", 'text/plain' );
$mailer->send( $message );
You can use this Swiftmailer function:
$message = Swift_Message::newInstance();
$headers = $message->getHeaders();
$headers->addPathHeader('Your-Header-Name', 'person#example.org');
I've just tried it with similar code, and it worked. swiftmailer is now up to version 4.3.0, so it could be something fixed in that. Otherwise about the only difference in my code is I don't have the addPart() that you have.
Also, had you checked your php error logs? :-)
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) {
}
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'));