duanliang789262
2009-08-07 14:50 浏览 122
已采纳

致命错误:未捕获的异常..使用php SwiftMailer

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答 默认 最新

  • 已采纳
    doudang2537 doudang2537 2009-08-07 14:53

    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.

    点赞 评论 复制链接分享
  • doudou_3636 doudou_3636 2009-08-07 14:54

    I think that it means that the given email address doesn't respect the email adressess standards.

    点赞 评论 复制链接分享
  • du229908 du229908 2012-10-04 01:23

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

    点赞 评论 复制链接分享
  • dtn43447 dtn43447 2013-07-16 14:10

    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);
    }
    
    点赞 评论 复制链接分享

相关推荐