Swiftmail not sending attachment $_FILES tmp - php

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']);

Related

How can I attach a pdf I created with TCPDF to a mailto link?

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

PHP error when trying to send email using Swift Mailer

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.

Swiftmailer remove attachment after send

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

PHP SwiftMailer Not sending

I am doing some testing prior to working on some production code and need to figure out how to do an auto e-mail.
The below script runs fine and the result of the send method returns 1, as if it sends. However, nothing ever makes it to the recipient.
require_once '/home/absolut2/lib/swift_required.php';
//Create the Transport
$transport = Swift_SmtpTransport::newInstance('mail.mysite.com', 25)
->setUsername('myuser')
->setPassword('password')
;
/*
You could alternatively use a different transport such as Sendmail or Mail:
//Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
//Mail
$transport = Swift_MailTransport::newInstance();
*/
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Create a message
$message = Swift_Message::newInstance('Subject')
->setFrom(array('rp#mysite.com' => 'RP'))
->setTo(array('rp#gmail.com'))
->setBody('Here is the message itself');
//Send the message
$result = $mailer->send($message);
echo "Messages sent: " . $result;
The code itself seems fine, so I guess something else is wrong. Either check the spam queue of the recipient or maybe just the address was rejected.
Find out if addresses were rejected.
You can do that with this code:
if (!$mailer->send($message, $failures)) {
echo "Failures:";
print_r($failures);
}

Swiftmailer 4 does not retrieve bounces as $failedRecipients

I am trying this code (from http://swiftmailer.org/docs/sending.html):
require_once 'lib/swift_required.php';
//Create the Transport
$transport = Swift_SmtpTransport::newInstance('localhost', 25);
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setBody('Here is the message itself')
;
//Send the message
$failedRecipients = array();
$numSent = 0;
$to = array('receiver#domain.org', 'other#baddomain.org' => 'A name');
foreach ($to as $address => $name)
{
$message->setTo(array($address => $name));
$numSent += $this->send($message, $failedRecipients);
}
printf("Sent %d messages\n", $numSent);
The problem is that if I sent an email to a bad domain swiftmailer recognize it as a correct sent email and $failedRecipients is empty. In my mail box I have returned a failure notice.
Why does Swiftmailer not recognize this mail as as a failure, and does not populate $failedRecipients Array?
Swiftmailer only takes care to hand the email over to the mail-server. Everything else is not related to Swiftmailer.
What you get is a bounce message, and you need to process them on your own, because the email itself actually was a syntactically mail address that was not rejected by the first server.
That btw is the case for any other mailing library and even the php mail function. You might be looking for a bounce processing application or code.
Related: Bounce Email handling with PHP?

Categories