Zend Mail Exception - unroutable Address - why? - zend-framework

Hi guys I've built a simple cron program which runs in php using the zend framework. It sends emails periodically to members of a website with updates. It was working fine when all of a sudden it just dies out upon emailing to a particular email and all I get is this error message:
PHP Fatal error: Uncaught exception 'Zend_Mail_Protocol_Exception' with message 'Unrouteable address ' in /web/content/library/Zend/Mail/Protocol/Abstract.php:431
Whats going on and why is this happening for this particular email?
The email seems fine though i.e it isn't malformed or so. Also how can I prevent something like this from stopping the cron job to proceed with other emails.

What about placing it in a try catch block ?

First of all you should validate email addresses, Afterwards you should try a catch block like this:
try {
/*
* Set up Testing environment for Smtp mail
* Handle Mail exceptions Different
*/
$mail = new Zend_Mail();
$mail->send();
} catch (Zend_Mail_Transport_Exception $ex) {
$this->addError('There was an error sending e-mail to the new admin !');
} catch (Exception $ex) {
$this->addError('There was an error processing your request');
}

Related

A socket operation was attempted to an unreachable network [2a00:1450:4013:c01::6d]:993 when receiving Gmail email with S22 ImapClient

I'm trying to receive email from Gmail. Occaisionly (approx 1 out of 5 times) I get a System.Net.Sockets.SocketException, error message:
A socket operation was attempted to an unreachable network [2a00:1450:4013:c01::6d]:993
The network address is not always the same, but varies slightly. This error does appears occaisonally on all the Gmail boxes I want to check, but does not appear on my Office 365 mailbox at all.
My app is an MVC 5 applications hosted by Microsoft Azure. I use the S22 Imap library
The relevant part of the code to retrieve the email is:
using S22.Imap;
ImapClient Client;
List<MailMessage> NewMessages;
try
{
Client = new ImapClient(tenant.ImapHostName,
tenant.ImapPortNumber,
tenant.ImapUserName,
tenant.ImapPassword,
AuthMethod.Login, tenant.UseSsl);
}
catch (Exception e)
{
return;
}
try
{
NewMessages = GetUnseenMessages(Client);
}
catch (Exception e)
{
}
I've disable IPv6 on my Azure webservice (disabled it on the adapter) but still this error comes back over and over again.
imap.gmail.com returns several IP addresses (three right now, but the number might vary depending on time and location). You're supposed to try all three. If one fails in the manner you see, you're supposed to try the next address, so your next step is to find out whether S22 does that, and if not, how you can make that happen.
I had the same problem as OP and did following to retry multiple IP addresses with S22.Imap.dll
var ips = Dns.GetHostAddresses("imap.gmail.com");
foreach(var ip in ips)
{
try
{
return new ImapClient(ip.ToString(), "993", Email, Password, AuthMethod.Login, true);
}
catch(SocketException e) //error means server is down, try other IP
{
//nothing, check next IP for connection
}
}

Redemption: RDOMail accessing attachments and moving mail

I am trying to access the attachments for an RDOMail object. When I either search for a specific item using LINQ or just try and iterate through the list with a foreach it freezes outlook and throws no exception.
Also when I try and move the RDOMail to another folder it freezes outlook and throws no exception.
I can accomplish both these things just using the Outlook.MailItem
Anyone have any ideas?
void store_OnNewMail(string entryId)
{
RDOMail mail = _store.GetMessageFromID(entryId);
RDOAttachment protocolAttachment = mail.Attachments.Cast<RDOAttachment>().SingleOrDefault(attach => attach.FileName == "protocol.id");
mail.Move(_hiddenDeliveryTrustFolder);
}
My guess is that the IMAP4 message available at the time of the NewMail event is just an envelope message (header only, no body or attachments). When you access the attachments, IMAP4 provider attempts to connect to the IMAP4 server to retrieve the data, but the call is blocked because of a critical section signaled before the event is raised.
Try to bypass the IMAP4 provider level by
RDOStore unwrappedStore = rSession.Stores.UnwarpStore(_store);
RDOMail mail = unwrappedStore.GetMessageFromID(entryId);
You can also try to save the message entry id in a variable and start a timer (use the one from the Forms namespace). When the timer event fires (you will be out of the newMail event by then), you can open the message and process it.

How to know CakePHP mail has been sent successfully or not?

In my CakePHP web application I'm sending mails and if mail is successfully sent then updating data base field 'mailSent' to true.
But How to know that mail is successfully sent or not?
You can use a try catch block to check whether the mail was successfully send or not, you can not detect or check if the mail was successfully delivered to the recipient. That is a different scenario.
try {
if ( $this->Email->send() ) {
// Success
} else {
// Failure, without any exceptions
}
} catch ( Exception $e ) {
// Failure, with exception
}
above is just sudo code you can change variable as you need.
let me know if i can help you more.

Sending email from Magento to fails

I am trying to send an email from a custom module in magento, however it fails to send it.
do i need to include anything or should i make some configuration with my hosting?
Here you can see my code:
$mail = new Zend_Mail();
$mail->setBodyText($mailbody);
$mail->setFrom('admin#gmail.com', 'admin');
$mail->addTo('email#gmail.com', 'client');
$mail->setSubject('Error report');
try {
$mail->send();
Mage::getSingleton('core/session')->addSuccess('Your request has been sent');
}
catch (Exception $e) {
Mage::getSingleton('core/session')->addError('Unable to send.');
}
It's better to use Magento's models for sending email. This way you know it's being send correctly, and get userful errors when it fails
The most simply way:
$email = Mage::getModel('core/email_template');
$email->setSenderEmail('sender#email.com');
$email->setSenderName('name');
$email->setTemplateSubject('Subject');
$email->setTemplateText('emailbody');
$email->send('receiver#mail.com', 'receiver name');
Remember that you host my not support sending mail, or that your provider is blocking port 25. This will result in a message in your exception.log
If you want to see how the final email looks, print $email->getProcessedTemplate() onto your screen
You have to configure your smtp in php.ini
What is your provider ? It's possible to find the smtp server's url, and put it in your php.ini file.

Commit transaction then send email

In Java, let's say I have a transaction that once it is committed, I want to do another action, in this case, send an email.
Therefore,
try {
transaction.begin()
...
transaction.commit()
// point 1
Utility.sendEmail(...)
} catch (Exception e) {
transaction.rollback();
}
Is it possible for the thread to die/get killed at point 1 and not send an email ?
Any way to minimize this ? JTA + JMS perhaps, where the action of sending a message is part of a transaction ?
I'm investigating an issue and exploring whether this is possible. JVM is still alive (no OOM). I do not know the inner working of the app server so not sure if this is possible.
I can't say for sure if the rollback() in the catch clause has any effect if the commit() was OK and sendEmail() threw an exception. The quickest way to test this is to throw an exception from the sendEmail() method and see if the transaction was actually committed.
The way I would put it though, is to move the sendEmail() call away from your try block:
try {
transaction.begin()
...
transaction.commit()
} catch (Exception e) {
transaction.rollback();
}
try {
// point 1
Utility.sendEmail(...)
} catch (Exception e) {
// handle it
}
This way you can control what will happen if a rollback was made.
Also, I think that sending the email to a JMS queue is in most cases a good idea. Doing it like that will give your DB code permission to continue and supposedly give feedback to your user that everything went OK and the email will be sent whenever it fits the email controller's schedule. For example, there might be a connection issue with your email server and the email sending would hang for say 30 seconds before throwing an exception and your user would see this as a very long button click.