Hi all
I did some more work on my problem (original email below), and came up with
the following solution.
1. In my ContactAction::executeWrite, I attempt to send the email and,
depending on whether it succeeds, return 'Error' or 'Success'.
2. In my ContactAction::handleError, I return 'Input'.
Action code is as below:
class Default_ContactAction extends WASPDefaultBaseAction
{
public function getDefaultViewName()
{
return 'Input';
}
public function executeWrite(AgaviRequestDataHolder $rd)
{
$name = $rd->getParameter('name');
$email = $rd->getParameter('email');
$message = $rd->getParameter('message');
$subject = 'Contact form submission';
$to = '[email protected]';
if (@mail($to, $subject, $message, "From: $name <$email>\r\n")) {
return 'Success';
} else {
return 'Error';
}
}
public function handleError(AgaviRequestDataHolder $rd)
{
return 'Input';
}
}
The result is as follows:
1. If the contact form is submitted with invalid data, the handleError
method returns the form (Input view) with appropriate error messages on the
invalid fields.
2. If the form is submitted with valid data but message sending fails, it
returns the Error view, whcih contains a message saying 'Error sending your
mail, try later'.
3. If the form is valid and the message is sent, it returns the Success view
which says 'Thanks for your email'.
Please comment on the above, specifically any suggestions for how this could
be improved or if there is any way to do this better. Thanks.
Vikram
---------- Forwarded message ----------
From: <[email protected]>
Date: Thu, Apr 30, 2009 at 10:23 PM
Subject: How to handle mail sending errors
To: [email protected]
Hi all,
I have a contact form which users can fill up and have their message
sent via email. There's a ContactAction with an executeWrite() method
and three views: Input, Error and Success. There's also a validator
for the contact form.
When the form is submitted without the necessary values, the
ContactErrorView is called. Within this, I'm setting the template back
to ContactInput so that the initial form is redisplayed with error
messages:
public function executeHtml(AgaviRequestDataHolder $rd)
{
$this->setupHtml($rd);
$this->getLayer('content')->setTemplate('ContactInput');
}
This works fine. What I'm trying to figure out is, how do I handle a
situation where the form is correctly filled but the message is not
sent. I'll know when this happens because mail() will return false,
but I'm not sure how to catch it and display a page saying 'your
message could not be sent'. If I check the return value of mail() in
the ContactAction::executeWrite() method and return 'Error', it simply
returns the input form again.
Do I need another error view - maybe ContactSendError - and do something
like:
if (mail error) {
return 'SendError';
} else {
return 'Success';
}
Or is there a better way?
TIA,
Vikram
_______________________________________________
users mailing list
[email protected]
http://lists.agavi.org/mailman/listinfo/users