I'm getting an wired exception while using phpmailer v5.1
Uncaught exception 'phpmailerException' with message 'You must provide at least one recipient email address.
I checked if i have a correct Email in there
$oMail->AddReplyTo = FROM;
$oMail->AddAddress = "testmail#web.de";
$oMail->SetFrom = FROM;
but still the same exception. any ideas what the problem can be?
AddAddress is a method, not a property. So, it should be like
$oMail->AddAddress("testmail#web.de");
This applies to all three settings you're trying to apply.
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 using Swift mailer to send emails, it's not catching exception on invalid email address & throws an error.
My code is:
try
{
$message->setBody($html, "text/html");
$result = $mailer->send($message);
}
catch(Swift_RfcComplianceException $e)
{
echo "Address ".$email." seems invalid";
}
For an email address which doesn't comply with RFC it just throws this error:
Fatal error: Uncaught exception Swift_RfcComplianceException with message Address
in mailbox given [ex#example#ex.com] does not comply with RFC 2822, 3.6.2.
thrown in /swiftmailer/classes/Swift/Mime/Headers/MailboxHeader.php on line 352
Can anybody help resolving it? Simply it should catch an exception so other functions are not affected.
You're wrapping the try ... catch-block around the wrong part of your composition of the swiftmailer mail.
Excerpt from the manual:
If you add recipients automatically based on a data source that may
contain invalid email addresses, you can prevent possible exceptions
by validating the addresses using Swift_Validate::email($email) and
only adding addresses that validate. Another way would be to wrap your
setTo(), setCc() and setBcc() calls in a try-catch block and handle
the Swift_RfcComplianceException in the catch block.
Therefore you should use it while adding addresses to your Swift_Message-object, like this:
$message = Swift_Message::newInstance();
// add some message composing here...
$email = "somewrongadress.org";
try {
$message->setTo(array($email));
} catch(Swift_RfcComplianceException $e) {
echo "Address ".$email." seems invalid";
}
Additionally, I'd advise some try-catch around the $result = $mailer->send($message); too, since it might throw an exception if something else is wrong.
I'm getting this error when trying to send an email using swiftmailer and the sendgrid smtp
Fatal error: *Uncaught exception 'Swift_TransportException' with message 'Expected response code 250 but got code "", with message ""'*
Here's my code :
$hdr = new SmtpApiHeader();
// Set all of the above variables
$hdr->addTo($toList);
$hdr->addSubVal('-name-', $nameList);
$hdr->addSubVal('-time-', $timeList);
// Specify that this is an initial contact message
$hdr->setCategory("initial");
// The subject of your email
$subject = 'Example SendGrid Email';
// Where is this message coming from. For example, this message can be from
// support#yourcompany.com, info#yourcompany.com
$from = array('no-reply#mupiz.com' => 'Mupiz');
$to = array('antonin#noos.fr'=>'AN',"antonin#mupiz.com"=>"AN2s");
$text="Hello -name-
Thank you for your interest in our products. We have set up an appointment
to call you at -time- EST to discuss your needs in more detail.
Regards,
Fred, How are you?
";
$html = "
<html>
<head></head>
<body>
<p>Hello -name-,<br>
Thank you for your interest in our products. We have set up an appointment
to call you at -time- EST to discuss your needs in more detail.
Regards,
Fred, How are you?<br>
</p>
</body>
</html>
";
// Your SendGrid account credentials
$username = 'XXXX';
$password = 'XXXX';
// Create new swift connection and authenticate
$transport = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 25);
$transport ->setUsername($username);
$transport ->setPassword($password);
$swift = Swift_Mailer::newInstance($transport);
// Create a message (subject)
$message = new Swift_Message($subject);
// add SMTPAPI header to the message
// *****IMPORTANT NOTE*****
// SendGrid's asJSON function escapes characters. If you are using Swift Mailer's
// PHP Mailer functions, the getTextHeader function will also escape characters.
// This can cause the filter to be dropped.
$headers = $message->getHeaders();
$headers->addTextHeader('X-SMTPAPI', $hdr->asJSON());
// attach the body of the email
$message->setFrom($from);
$message->setBody($html, 'text/html');
$message->setTo($to);
$message->addPart($text, 'text/plain');
// send message
if ($recipients = $swift->send($message, $failures))
{
// This will let us know how many users received this message
// If we specify the names in the X-SMTPAPI header, then this will always be 1.
echo 'Message sent out to '.$recipients.' users';
}
// something went wrong =(
else
{
echo "Something went wrong - ";
print_r($failures);
}
An idea ?
Thanks
There could be a number of things causing this. The most likely one is that your server has connecting to external hosts turned off. Other possibilities are that you're using an old version of PHP that has an openSSL error or that you're being rate limited by something.
You should take a look at this question for details on the external host issue: send mails via sendgrid
On a separate note, you should use the SendGrid PHP library if you want to send emails using SendGrid. It addresses a whole bunch of subtle nuances with Swift and sending in general. Also, it gives you access to the HTTP API in case you can't use SMTP for whatever reason.
https://github.com/sendgrid/sendgrid-php
Full Disclosure: I work as a developer evangelist for SendGrid and work on the PHP library from time to time.
for sendgrid might be two resons:
you may have the authentification data wrong
your account might still be provisioned, so you must wait a bit for it to be fully activated
so
check all the autentification data from app/config/mail.php to match with the data from https://sendgrid.com/docs/Integrate/index.html
check also not the have a top message/notification on your sendgrid account like:
Your account is still being provisioned, you may not be able to send
emails.
You will also receive this error if your account is frozen for billing reasons (e.g. lapsed credit card expiry date).
Note: SendGrid doesn't drop the message(s), they hold them until your billing issue is resolved then process them even when this error is returned.
I am using SwiftMailer for sending mails and if I try to use dummy email address, for example if I type in "asdf" in email address, I get this uncaught exception.
Fatal error: Uncaught exception 'Swift_RfcComplianceException' with message
'Address in mailbox given [asdf] does not comply with RFC 2822,
I am not very experienced in OO .. so not sure how to handle this? Actually I just want it to fail if the email address is not valid but it shouldnt throw the fatal error message. Any suggestions?
Thanks.
You need to catch the exception, like this
try
{
// Your code to send the email
}
catch (Swift_RfcComplianceException $e)
{
print('Email address not valid:' . $e->getMessage());
}
This isn't an OO thing, it's an exceptions thing.
Also, you can validate the email before sending it:
if(!Swift_Validate::email($email)){ //if email is not valid
//do something, skip them
$log_error->log($email);
}
I think that it means that the given email address doesn't respect the email adressess standards.
If the email address is valid based on what you see in the error message, make sure that there are no leading or trailing spaces in the address. eg. run trim($email_address).