I need to log result of sending an e-mail message by sfMailer in Symfony 1.4
The message is to be sent when user submits a form which includes his contact e-mail address.
In case of failure (sending the mail) I should log his e-mail address using logger. That is pretty clear to me.
What I still don't get is how to get an "update" from sfMailer about whether the sending was success?
Something that returns true or false if possible.
Web debug toolbar is nice but it does not help here.
According to the doc:
When using send() the message will be sent just like it would be sent if you used your mail client. An integer is returned which includes the number of successful recipients. If none of the recipients could be sent to then zero will be returned, which equates to a boolean false. If you set two To: recipients and three Bcc: recipients in the message and all of the recipients are delivered to successfully then the value 5 will be returned.
Which result in:
// Send the message
$result = $mailer->send($message);
// or in a symfony action
$result = $this->getMailer()->send($message);
Related
Docs for SwiftMailer say:
It’s possible to get a list of addresses that were rejected by the Transport by using a by-reference parameter to send(). As Swift Mailer attempts to send the message to each address given to it, if a recipient is rejected it will be added to the array.
// Pass a variable name to the send() method
if (!$mailer->send($message, $failures))
{
echo "Failures:";
print_r($failures);
}
However, I do not see a way to determinate what was the reason for this failure. E.g. send returns 0, $failures is filled with e-mail address, but I would like to know why sending has failed/rejected.
How can I do this? Is it not possible? A quick look inside sources indicate that SwiftMailer catches exceptions to fill $failedRecipients and it seems that exception's message is not saved anywhere. Am I missing something?
I don't think that it is possible to do this. Unfortunately it is only possible to see the list of email addresses that failed rather than the reasons for these addresses failing.
From the discussion on GitHub (https://github.com/swiftmailer/swiftmailer/issues/891):
Exceptions are swallowed silently in \Swift_Transport_AbstractSmtpTransport::_doMailTransaction()
So I am want to send an email and record in the database that it was sent successfully, here is what I do:
First, try sending an email to the user containing the product information
Second, check if the email was sent successfully. If yes, then record in the database that it was sent successfully.
But if sending the email failed (an exception was thrown) I want to catch that exception and return an error message.
My question is:
Is there a case that the email gets sent but still throws an exception?
So by that the code returns error thinking that the email wasn't sent .. but it was actually sent and the exception was throw later after that.
// pseudo code
try{
$is_sent = send_email();
if($is_sent){
$db->email_was_sent();
}
}catch(Exception $e){
return 'Email was not sent. An exception';
}
Is there a case that the email gets sent but still throws an exception?
It depends.
If email is sent for a single recipient, any 3 of these situation could result:
email is delivered to recipient
email failed to be delivered to recipient
an exception was raised
For this case, it would be undocumented behaviour of the SwiftMailer email client
to have an email sent but still throw an exception.
If email is sent to several recipients, any 3 of these situation could result:
email is delivered to all recipients
email failed to be delivered to one or more recipient(s)
an exception was raised
For this other case, email could be delivered to some recipients and still raise an exception.
https://swiftmailer.symfony.com/docs/sending.html#using-the-send-method
AbstractSmtpTransport::send() shows that email may fail to be sent for one or more of the recipients.
https://github.com/swiftmailer/swiftmailer/blob/v6.2.1/lib/classes/Swift/Transport/AbstractSmtpTransport.php#L178
I'm trying to send emails in a loop and it is working fine but it prints the result to page in one go rather one by one.
What I want is, it should print a response for every email sent. This is what I have so far:
//foreach loop
$Response = $ObjMail->send();
if ($Response) {
echo "Email Sent Successfully to $val[name] </br>";
} else {
echo "There was an error sending Email to $val[email]";
}
Depending on your $ObjMail, a "successfully send mail" generally will equate to
the sending mail server (i.e. smtp server) accepted the email or
the mail() function got called (actually read the doc, especially the return value part).
Email functions rarely return a very useful value, as long as the email being sent is at least somewhat plausible. It will even return true, if the email address doesn't exist, the email gets bounced, your smtp server is blacklisted, ...
The probable answer to your question: Your output is almost instantaneous by default, unless your local sendmail (the default on most hosts) call takes longer than a few microseconds, which it usually doesn't. Additionally, it doesn't say anything about the mails actually being sent. (And I assume, that you thought that was actually the case, it's not.)
My advice is, drop the stylish output and just send the mails. You can't be certain if they actually reached their target. If the $ObjMail actually returns an error, that would probably be wise to log somewhere, so that you don't repeatedly send to the same false address.
I have a gmail account; Three other emails are related to this one.
When I receive an email at xxx#mydomain.com it will be transferred to xxx#gmail.com.
Also when I send a message I can use an alias which mean that using gmail I send a mail with my xxx#mydomain.com.
When I use imap_search, From xxx#mydomain.com or To xxx#mydomain.com the result is blank but messages exist in my Inbox. Is this a problem of imap_search?
The solution is:
The default folder when we make imap_connection is INBOX. When we try to make imap_sort or iamp_search. We don't get anyresult. We have to switch to the folder which contain all messages for example [GMAIl]/All.
I can successfully send emails by using CakeEmail class of CakePHP.
But how can I be sure that the email is sent ?
Does a response comes when email is sent successfully?
When i take success response, I will redirect visitor to some other page.
send() method send the content array if the mail send successfully or it throws an exception.