Could email be sent but still throws an exception? - php

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

Related

Is there any way to ignore error in laravel

Using Laravel mailable I want to send mail to my client.
But when I tried to send mail to an email that is not available or that email is not exist I got an error saying:
Expected response code 354 but got code "554", with message "554 5.5.1 Error: no valid recipients "
I tried to put try and catch method but it doesn't work.
I also find answers from the internet but i failed.
This is my try catch code:
try {
Mail::to($_val['email'])->send(new ClientMail($_val));
} catch(\Exception $e) {
// Do nothing
}
How can I ignore the error message even if the email does not exist I want to proceed

Send mails in a loop and output response to after every email is sent

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.

PHPMailer returns true even if sending to fake address

I need to know if PHPMailer was unable to send an email. But even sending to a fake email address returns true:
$phpmailer = new PHPMailer( true );
$phpmailer->setFrom( "myemail#myemailladdy.com", "myemail#myemailladdy.com" );
//This is definitely not reachable
$phpmailer->addAddress( "fake#shdsabdasdiuahsdiuhaiduhasidsjdfake.com", "IJustPressedRandomKeys" );
$phpmailer->Subject = "fake";
$phpmailer->Body = "fake";
echo "Is Mail: " . $phpmailer->IsMail();
//This prints "1"
echo "Was Sent: " . $phpmailer->send();
Why is this returning 1/true?
(When the email is valid, I do recieve the emails, so PHPMailer is setup correctly)
PHPMailer does not know whether an email address is real or not. The mail server won't know until it sends the email and gets a rejection response. But the handoff between the server and PHP has already been terminated by that point.
There is no real way to verify an email address exists without sending an email to it and getting either a response or having the user enter a unique token into a web form. The closest you can get is verifying MX records or other DNS information that verifies a domain exists, etc. But that will not be perfect and will have false positives as well as letting fake emails through if the domain is valid.
I wos thinking about it for a while... and i think i have a nice solution.
If there will be some kind big trouble:
if(!$mail->Send()) {
echo $mail->ErrorInfo; // this is important for you
// other functions...
}
or if will be success?
else {
$smtp_msg = 'ALL OK'; // sets the message you want to see
if ($mail->ErrorInfo != '') { // check if there wos any other error
$smtp_msg = $mail->ErrorInfo; // if yes - show it to me
}
// else is optional but no need couse if there wos no error we already set $smtp_msg = 'ALL OK';
return $smtp_msg;
}
Or even better you could try to use codes of exrrors to show youre own messages...
Or... use try/catch like here:
Error handling with PHPMailer
This could be helpfull!

PHPMailer issue with recipient

I use PHPMailer to send some email to one ore more detinations.
However I encountered a problem. Here comes:
This is my code:
$mail2->AddAddress($cliemail);
$mail2->AddCC('important#destination.com');
$mail2->SetFrom('important#destination.com', "...");
$mail2->AddReplyTo('important#destination.com', "...");
$mail2->Subject = 'Email subject... '.$wnume;
$mail2->AltBody = 'To be able to see this email please...';
$mail2->MsgHTML(file_get_contents('contents.html'));
$mail2->AddAttachment('teste/quiz.pdf'); // attachment
$mail2->Send();
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
So, my email must be sent from important#destination.com (it is another email of course)
And it gets sent to $cliemail, but it does not arrive to the CC (important#destination.com)
So apparently I cannot send an email to myself... Is this normal?
What could be the cause?
I tried to remove the AddCC and try to simply send the email directly to important#destination.com but it still does not arrive.
Is this some setting in my email account that I must do in order to be able to receive an email from myself?
So:
When I send the email to a third party email address using the above
code, it works
When I send the email to myself (the sender) (the same address that is the "SetFrom") it does not arrive
Even if I set the CC with my address, it still does not arrive, while it arrives at the main destination address ($cliemail)
Please help

Debug info from Symfony sfMailer regarding sending a mail

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

Categories