Search code examples
rerror-handlingsendmailr

sendmailR: how to handle occasional "Unknown SMTP code" errors


I'm using sendmailR in my R script to send notifications.

Sometimes notification fails with the following error:

Unknown SMTP code: 452
Error in if (code == lcode) { : argument is of length zero

Execution halts.

How can I handle such errors, so that even if notification fails the script runs on?


Solution

  • Wrap the try function around sendmail (assuming you use sendmail, if not then wrap it around the function or code that produces the error) this way:

     try(sendmail(from,to,subject), silent=T)
    

    You can set silent to FALSE if you want the error message to appear but still continue with the process