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?
Answer
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:
CloudDeploy[
APIFunction[
{ "To" -> <|"Interpreter" -> "EmailAddress"|>,
"Subject" -> "String",
"Body" -> "String" },
SendMail[#] &],
FileNameJoin[{$CloudRootDirectory, "WolframCloudSandboxEmail"}],
Permissions -> "Public" ]
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:
URLExecute[
FileNameJoin[{$CloudRootDirectory, "WolframCloudSandboxEmail"}],
{ "To" -> "YOUR-NAME ",
"Subject" -> "Wolfram Cloud Sandbox Email",
"Body" -> "Here is the body of the message." } ]
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:
URLExecute[
FileNameJoin[{$CloudRootDirectory, "WolframCloudPrivateSandboxEmail"}],
{ "To" -> "YOUR-NAME ",
"Subject" -> "Wolfram Cloud Private Object Sandbox Email",
"Body" -> "Here is the body of the message." },
"Username" -> "YOUR-WOLFRAM-ID",
"Password" -> "YOUR-WOLFRAM-CLOUD-PASSWORD" ]
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:
URLFetch["https://api.mailgun.net/v3/YOUR-SANDBOX-DOMAIN-NAME/messages",
"Method" -> "POST",
"MultipartElements" ->
{ {"from", "text/plain"} ->
"Mailgun Sandbox ",
{"to", "text/plain"} -> "YOUR-NAME ",
{"subject", "text/plain"} -> "Mailgun Sandbox Email",
{"text", "text/plain"} -> "Here is the body of the message." },
"Username" -> "api",
"Password" -> "YOUR-PRIVATE-API-KEY"]
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.
Comment 2/9/16:
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.
Comments
Post a Comment