Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
DialogInput? See first example in section "Scope". Mind you, as long as your machine is not locked, retrieving the password will still be trivial... – Yves Klett Aug 21 '13 at 11:03
up vote 19 down vote accepted

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:

BeginPackage["mailPack`"]

sendMail::usage="sendMail[subject, body, to, file] send mail using myMail";
Begin["`Private`"]

sendMail[subject_,body_,to_,file_:None]:=
SendMail[
    "To"->to,
    "Subject"->subject,
    "Body"->body,
    "From"->"myMail@math.com.br",
    "Server"->"smtp.gmail.com",
    "PortNumber"->587,
    "EncryptionProtocol"->"StartTLS",
    "UserName"->"myLogin",
    "Password"->Uncompress@"myCompressedPassword", (*Use compress to create it*)
    "Attachments"->file
]

SetAttributes[sendMail, {ReadProtected,Locked}];

End[]
EndPackage[]

Now you can open another notebook, save in the same directory, and the do this to encode the first:

  SetDirectory@NotebookDirectory[];
  Encode["mailPack.m","mailPack.m"];

Now, if you try to open your mailPack.m as txt file, you will see that it is completely unreadable.

To use your sendMailfunction, copy your pack in the same directory of your file, then use SetDirectory@NotebookDirectory[], then use Needs["mailPack"]. If you use it all the time, copy the mailPack.m for the Application directory in your Mathematica folder (this command can help you SystemOpen@$UserBaseDirectory). Then you don't need the SetDirectory part neither to put the mailPack file in the same directory of your current notebook.

share|improve this answer
1  
    
Interesting that @Leonid suggest DumpSave instead of Encode. Also related about security of encode here. – Murta Aug 21 '13 at 11:22
    
The juicy part is @RolfMertig´s comment on Locked in pre-9 versions... perhaps there is some more secure stuff possible via Java or similar? – Yves Klett Aug 21 '13 at 11:25
    
@YvesKlett, cool lock all function. link – Murta Aug 21 '13 at 11:31
1  
This way it's more comfortable to open the code in front of another person, and if he is not Sheldon Cooper (with his eidetic memory) there is no problem. – Murta Aug 21 '13 at 12:02

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 <YOUR-EMAIL-ADDRESS>", 
      "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 <YOUR-EMAIL-ADDRESS>", 
      "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 <postmaster@YOUR-SANDBOX-DOMAIN-NAME>",
         {"to", "text/plain"} -> "YOUR-NAME <YOUR-EMAIL-ADDRESS>",
         {"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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.