Silence Errors

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
User avatar
goldfiles
Forum Commoner
Posts: 27
Joined: Sun Nov 12, 2006 5:50 pm

Silence Errors

Post by goldfiles »

How can I silence swift mailer errors? For example, I have a php script that my visitors can use to contact me. They enter 1. Their Email Address and 2. A Message. And it always goes to support@mydomain.com

However, if a visitor inputs an invalid email address, then I get this big error and the page stops loading after it:
Fatal error: Uncaught exception 'Swift_BadResponseException' with message 'Expected response code(s) [250] but got response [501 <adh>: sender address must contain a domain]' in /home/fluxmar/public_html/extra_pages/swift/Swift.php:250 Stack trace: #0 /home/fluxmar/public_html/extra_pages/swift/Swift.php(310): Swift->assertCorrectResponse(Object(Swift_Events_ResponseEvent), 250) #1 /home/fluxmar/public_html/extra_pages/swift/Swift.php(373): Swift->command('MAIL FROM: <adh...', 250) #2 /home/fluxmar/public_html/extra_pages/emailer.php(43): Swift->send(Object(Swift_Message), 'support@textadm...', Object(Swift_Address)) #3 /home/fluxmar/public_html/content/help/contact.php(19): include('/home/fluxmar/p...') #4 /home/fluxmar/public_html/content/help.php(26): include('/home/fluxmar/p...') #5 /home/fluxmar/public_html/content_loader.php(10): include('/home/fluxmar/p...') #6 /home/fluxmar/public_html/layout.php(87): include_once('/home/fluxmar/p...') #7 /home/fluxmar/public_html/help.php(6): include('/home/fluxmar/p...') #8 {mai in /home/fluxmar/public_html/extra_pages/swift/Swift.php on line 250
How can I get swift mailer to fail "nicely"?

You can see my script here: http://www.textadmarket.com/help.php?help=7
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Silence Errors

Post by John Cartwright »

Code: Select all

try {
    //all your swift code here
} catch (Swift_Exception $e) {
    //handle your error here
}
Swift uses exceptions, so you can use the try/catch to catch the error and handle it whoever you like.
User avatar
goldfiles
Forum Commoner
Posts: 27
Joined: Sun Nov 12, 2006 5:50 pm

Re: Silence Errors

Post by goldfiles »

That was a quick fix, thanks!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Silence Errors

Post by Chris Corbyn »

Yep, Exceptions are a blessing. You are able to "code" your way around errors instead of ignoring them.

$swift->reset() would probably be a good thing to call from within the catch block.

Just FYI, it appears you're trying to send an email from somebody else's address. This is a no-no. If you're doing that so you can reply to that person then what you should be doing is sending the email from your own address, but set the reply-to with:

Code: Select all

$message->setReplyTo($sendersAddress);
:)
User avatar
goldfiles
Forum Commoner
Posts: 27
Joined: Sun Nov 12, 2006 5:50 pm

Re: Silence Errors

Post by goldfiles »

Good point :)
Post Reply