I want Mathematica to send me an email when the computation is finished. To that end, I have a cell with SendMail[...] which I place behind my computation in the evaluation queue. The problem I have is that I need to enter a password for my mail server. Doing that as an explicit rule leaves the password unencrypted and visible to everyone. On the other hand, setting "Password"->Automatic will only show a dialog box once the SendMail cell is evaluated. Is there a way to show the dialog box for password entry beforehand, such that I can enter the password and walk away?
Here is one solution based on encoded package.
To create a pack you have to create a .m file (in this case mailPack.m). There is one example using gmail configuration:
Now you can open another notebook, save in the same directory, and the do this to encode the first:
Now, if you try to open your mailPack.m as txt file, you will see that it is completely unreadable.
To use your
In general SendMail should not access an email account that stores important emails or that functions to reset the passwords of other online accounts. Such a valuable email account should be protected by two-factor authentication as well as user-managed access such as OAuth, and so forth. Because SendMail does not support any such features it can not access an truly secure email account. Rather than accept inadequate security on an important email account, it is much better to setup a separate email account especially for SendMail to access. If this special account is on gmail, then SendMail will require the Allow less secure apps setting. In addition, because it will only be used with SendMail the need to encrypt its password will depend solely the SendMail application’s security needs.
Besides setting up an email account especially for SendMail, there are at least two other email solutions to consider.
If you’re developing web applications, then you may already be using the free version of the Wolfram Development Platform. If you’re not at least at the $20/month Explorer plan, then upgrade and you will be able to deploy and call a SendMail API in the Wolfram Cloud:
You only need to do the above CloudDeploy once. Then every time you want to send an email, just call the API from your desktop:
If for some reason you don’t want a public API that sends you email, then deploy the API as a private cloud object, add the Username and Password options to URLExecute, and protect it by the encoded package approach in Murta’s answer:
On the other hand, if you’re developing applications that have the potential to generate a significant volume of email, you probably are already using or at least considering an email delivery service. Calling such a service will be among your standard approaches to sending an email. For example, you can easily setup and use a Mailgun sandbox:
Assuming that the mail delivery service is also being used to send other important emails, then its password could again be protected by the approach in Murta’s answer.
As originally posted this answer simply alerted readers to the email delivery service alternative to SendMail and left it to them to decide on its suitablity for their application. This updated answer adds a Wolfram Development Platform alternative and specifies circumstances that might favor each solution.