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'));
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
I am developing a newsletter emailer, where I have to send thousands of mails with different body content. How can I do this using swiftmailer?
Now my code is:
$emails = array('mail1#gmail.com'=>'name1',
'mail2#gmail.com'=>'name2',
'mail3#gmail.com'=>'name3',
'mail4#gmail.com'=>'name4',
'mail5#gmail.com'=>'name5');
$message = Swift_Message::newInstance('Testing Email')
->setFrom(array('mail1#gmail.com' => 'name'))
->setTo($emails) //$emails
->setReplyTo(array('reply_to#gmail.com' => 'reply'));
$html = $this->send_model->getEmailContent();
$body = $html;
$message->setBody($body, 'text/html')
;
// Send the message
$result = $mailer->send($message);
I want to send all separate mails for all the emails which are in the above array.. with different body content.
Thanks in advance.
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);
I ve tried everything, i don't know how to fix this, so, i am using this Swift Mailer library to send a confirmation email. So here is the code from my index.php
if($confirm){
//include the swift class
include_once 'inc/php/swift/swift_required.php';
//put info into an array to send to the function
$info = array(
'username' => $username,
'email' => $email,
'key' => $key);
//send the email
if(send_email($info)){
//email sent
$action['result'] = 'success';
array_push($text,'Thanks for signing up. Please check your email for confirmation!');
}else{
$action['result'] = 'error';
array_push($text,'Could not send confirm email');
}
}
And my send_email function is in another php file functions.php
//send the welcome letter
function send_email($info){
//format each email
$body = format_email($info,'html');
$body_plain_txt = format_email($info,'txt');
//setup the mailer
$transport = Swift_MailTransport::newInstance(smtp.gmail.com,465,ssl)
->setUsername('my email here')
->setPassword('my password');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
$message ->setSubject('Welcome to Site Name');
$message ->setFrom(array('somedomain.com' => 'somethinghere'));
$message ->setTo(array($info['email'] => $info['username']));
$message ->setBody($body_plain_txt);
$message ->addPart($body, 'text/html');
$result = $mailer->send($message);
return $result;
}
The error that i am getting is
Fatal error: Call to undefined method Swift_MailTransport::setUsername() in /srv/disk7/something/www/something/signup/inc/php/functions.php on line 31
How can i fix this? I am a beginner in php.
I'm guessing Swift_MailTransport::newInstance(smtp.gmail.com,465,ssl) fails to create the expected instance.
BTW, shouldn't it be Swift_MailTransport::newInstance('smtp.gmail.com',465,'ssl') (i.e. smtp.gmail.com and ssl in quotes)?
Swift_MailTransport, from the documentation...
...sends messages by delegating to PHP's internal mail() function.
In my experience -- and others' -- the mail() function is not particularly predictable, or helpful.
Another thing it does not have is a setUsername or setPassword method.
I'd say you want to use Swift_SmtpTransport
One more thing; as pointed out by others, your string arguments should be quoted, ie
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl');
Looks like you haven't included the Swift Mail class.
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);