Symfony2 delayed flashbag message when sending mail - php

I've got an odd problem that I can't put my finger on.
In my controller, I'm sending an email using swiftmailer, setting a flash message using the session flashbag, and redirecting to another page.
public function testAction() {
$this->get('session')->getFlashBag()->add('info', 'test flash message');
$message = new \Swift_Message('test', 'test');
$message->setTo('email#example.com');
$message->setFrom('email#example.com');
$this->get('mailer')->send($message);
return $this->redirect($this->generateUrl('home_page'));
}
After the redirect, I expect to see the flash message, but it seems that something happens when the mail is sent that makes the flash message delayed by one request. That is, I don't see the flash message when I get to 'home_page', but I do if I reload the page after the redirect.
If I comment out the send() call or even set disable_delivery: true in the swiftmail configuration, the flash message works as expected.
I thought I might have found a bug, but when I created a new project (symfony/framework-standard-edition 2.4.1) and tried this, it works even when I send mail. I've checked to make sure I'm using the same releases for swiftmailer, symfony, etc. So, I'm sure it's something with my project, but I am out of ideas of where to go from here.
I'm using PHP 5.5.7, Symfony 2.4.1.
Thanks for your time

A quick fix is to disable spooling for SwiftMailer. I am still uncertain whether this is the actual solution, but it seems to work.
By disabling spooling, the mailer will block execution while sending the mail.
I commented out the line that enables spooling:
#spool: { type: memory }
By default, the mailer will not use spooling but send the mail right away: http://symfony.com/doc/master/cookbook/email/spool.html

I believe there's a bug when using PdoSessionHandler for storing sessions - if you use native session storage the problem goes away and the flash message works as expected with SwiftMailer.
The flash messages (app.session.flashbag) rely on sessions to store the data between page loads.

Related

Can't send mail with Lumen framework and no error appears

Since yesterday I have had problems sending emails with Lumen. I think I have followed all the steps in the documentation correctly, but unfortunately I cannot send anything. Moreover, no error is displayed (I have activated APP_DEBUG=true), and I am sure that the credentials of the smtp server are correct. I also did composer require illuminate/mail.
Here are my modifications in bootstrap/app.php (I have $app->withFacades(); uncommented).
Here is my build function content: return $this->view('emails.mailTemplate', ['message' => $this->message]);.
And the line of code that ask to send mail: Mail::to("wewanthalflifethree#gmail.com")->send(new sendMail($destEmail, $subject, $message));.
Did I do something wrong? :/
Thx in advance for help!
EDIT: I just noticed something, the code stop working when I send the mail. If I add an echo after Mail::send, it will be appear on the page.
I have a problem with the Exceptions handler. By following this answer (which consists in adding a dd in the render function of Handler.php), I am now able to see what's going wrong.
So, here is the reason why my mail didn't send and why my page turned blank: Object of class Illuminate\Mail\Message could not be converted to string (View: /home/serveur-web/api.sl-projects.com/resources/views/emails/mailTemplate.blade.php).
The problem came from the fact that I was using the variable name $message, and this causes problems because it seems to be used in the class that handles the mails. My problem was therefore solved by renaming this variable $mailContent.
I hope this will help other people. :D

Laravel4 + Iron io: How to send mail using Mail::queue?

I have a laravel app with a form that when it is submitted will send a mail.
The mail configuration is working properly since the mail is received.
But since the process was quite slow i decided to use Queue.
I have set up Iron mq account and set it up on Laravel4.
So instead of Mail::send i switch to Mail::queue. Here is the code:
return \Mail::queue($this->view, $this->data, function($message) use($self)
{
$message->to($self->email, $self->to)->subject($self->subject);
});
And in the routes there is simply:
Route::post('booking', 'HomeController#booking');
Route::post('rezervesana', 'HomeController#booking'); // This is for the latvian version
When i submit the form, the queue is received in Iron mq dashboard and apparently fired.
But no mail is received..
If instead i do something like this:
Route::post('booking', function()
{
return Queue::marshal();
});
Then magically it will work in the latvian version (rezervesana) but of course than the english version page does not even open anymore..
So I am quite confused.
The question is: How can I send properly a mail using Mail::queue and how to deal with it in the routes?
I think that the code regarding the Mail::queue is correct, what is wrong can be in routes.
So the real question can be: How I have to set up the routes to make queue working properly?
Which is the proper place for Queue::marshal?
Thank you guys!
Sorry if it is confused.
Hope you can clarify it to me!
I've used SQS before but not Iron, maybe you should use this?
Route::post('booking', array('before'=>'csrf', 'uses'=>'HomeController#booking'));

fake sendmail for windows

I am using fake mail for windows, For long time i got a problem with the FIRST mail i am sending, What i mean is that for example i am using php so i will use this line:
mail("example#gmail.com", "hey", "bye");
If i will load the page with this mail function for the first time the mail function will return false, But the second time and third and so on it will work, But, After some time i can not determine how long exactly, I can say the gmail mail server "going to sleep", And again when i am sending a mail for the first time its like, Ohh you need a mail to send give me a second(the first mail return false), After the mail server got the second it wanted it will send the mail(return true), I did follow this tut, The error appear at the error.log is: Connection Closed Gracefully., Now as i see it there is a problem on my gmail account(some setting i should change), Anyway If anyone got any idea i will be very thankful, Thank you all and have a nice day.
After researching this bug, it seems to be a bug of the mail() function. One alternative is to download the phpMailer library, and implement your mail function there. The other alternative, since you say that the second time it always works, is:
if (!mail("example#gmail.com", "hey", "bye"))
mail("example#gmail.com", "hey", "bye");
In other words, if it fails once then you try again. You might insert a 300-milisecond pause there in case Google needs some time to come back from sleep.

MODX not sending emails

I'm using modMail class to send custom emails. I have followed the guidelines on MODX site and used the following code which I placed in a snippet:
$message = $modx->getChunk('myEmailTemplate');
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY,$message);
$modx->mail->set(modMail::MAIL_FROM,'me#example.org');
$modx->mail->set(modMail::MAIL_FROM_NAME,'Johnny Tester');
$modx->mail->set(modMail::MAIL_SUBJECT,'Check out my new email template!');
$modx->mail->address('to','user#example.com');
$modx->mail->address('reply-to','me#xexample.org');
$modx->mail->setHTML(true);
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
}
$modx->mail->reset();
The snippet has been modified to contain message from custom chunk as well as email addresses have been replaced with the correct ones. The snippet sent email once and never again. I have no idea what causes such behavior which prevents it from sending emails.
I have read that using the reset function $modx->mail->reset(); resets email fields and allows the email to be sent again yet I have a feeling that it causes problem here.
The snippet is called uncached on the page [[!email]]
Does anyone have an idea why the emails are not being sent, even though it worked once?
if there is an error in your chunk or in processing your chunk, modx is never going to get to thepoint where it logs an error. try something like:
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
}else{
$modx->log(modX::LOG_LEVEL_ERROR,'This mail was sent: '.$message);
}
to see if it logs something. but otherwise what you have there is exactly correct - try to take the $message variable out and send just a string. if it sent mail once, then something else must be wrong. I'd start looking at mail server logs, headers, spam [gmail??] etc.

Spam check before sending using Zend_Mail

I'm using Zend_Mail and the following code to send my email messages.
$mail = new Zend_Mail('UTF-8');
$mail ->setBodyText($plainBody)
->setBodyHtml($htmlBody)
->setSubject($subject)
->setFrom(FROM_ADDR, FROM_NAME)
->addTo($email, $name )
->addHeader(MY_HEADER, serialize( array( 'foo' => 'bar' ) ) )
;
I need to check the spam rating for the prepared message and I'd like to do it using SpamAssassin.
I thought to create a file with the contents and running something such as exec('spamc $filename'), but how to get the file content with the full MIME body?
I noticed that there's a _buildBody() function in Zend_Mail_Abstract class (library/Zend/Mail/Transport/Abstract.php) that's return that, but that's a protected function.
Thanks
If you want to use SpamAssasin, then run your email message through spamc:
http://spamassassin.apache.org/full/3.1.x/doc/spamc.html
Spamc is the client half of the spamc/spamd pair. It should be used in
place of spamassassin in scripts to process mail. It will read the
mail from STDIN, and spool it to its connection to spamd, then read
the result back and print it to STDOUT. Spamc has extremely low
overhead in loading, so it should be much faster to load than the
whole spamassassin program.
You can do use in PHP by:
Writing the message into a temporary file and running shell_exec('spamc < message.tmp'), or
Running the command with proc_open() then send message via STDIN.
I am assuming you want to simulate a spam check on the recipient's end. That's an interesting idea, but note that the results it will give you will be far from 100% realistic. After all, it's the sending process that adds much of the vital information that helps determine whether an E-Mail is spam (e.g. the sender IP and routes.)
Anyway, to do this, you will probably have to implement a custom Zend_Mail_Transport class, based on the example of Zend_Mail_Transport_Smtp. Any data that transport class sends to the SMTP server, you would have to re-route to a text file. As far as I can see at a cursory glance, it's going to be a bit of work but not impossible.

Categories