문제

I have a simple code for sending mails. sometime $host might not available or my SMTP server might be down but what happens in these cases swiftmailer throws lot of exceptions and $result despite of returning true or false give me a complete mess of errors.

so how i can turn off the errors for this page of code but not for the whole library?

    $transport = Swift_SmtpTransport::newInstance(self::$host, 25)
    ->setUsername(self::$username)
    ->setPassword(self::$password);

    //Create the Mailer using your created Transport
    $mailer = Swift_Mailer::newInstance($transport);
    $message = Swift_Message::newInstance("Custom Sheets");
    $message->setFrom(array('EDB@abc.com.pk' => 'Approval of Custom Duty Sheet'));
    $message->setTo($to);
    $message->setBody($html,'text/html');
    //Send the message
    $result = $mailer->send($message);
도움이 되었습니까?

해결책

You can catch that exception and handle it:

try {
    $transport = Swift_SmtpTransport::newInstance(self::$host, 25)
            ->setUsername(self::$username)
            ->setPassword(self::$password);

    //Create the Mailer using your created Transport
    $mailer = Swift_Mailer::newInstance($transport);
    $message = Swift_Message::newInstance("Custom Sheets");
    $message->setFrom(array('EDB@abc.com.pk' => 'Approval of Custom Duty Sheet'));
    $message->setTo($to);
    $message->setBody($html,'text/html');
    //Send the message
    $result = $mailer->send($message);
} catch(Exception $e) {
    // handle error here
}

Turning off errors / warnings / notices in general:

error_reporting(-1);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top