22

I need to send email via command-line without any human interactions for automation.

I know we can use mailto command but that would compose email, subject,body and everything but it wouldn't send it unless I click send.

I read online we can use blat but I cannot use anything other than outlook.

This is closed post I have found Link to SOF post.

just for your information: I am looking into some telnet commands to send email haven't gotten success in that yet either. telnet commands to send email

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mowgli
  • 3,422
  • 21
  • 64
  • 88

4 Answers4

25

Option 1
You didn't say much about your environment, but assuming you have it available you could use a PowerShell script; one example is here. The essence of this is:

$smtp = New-Object Net.Mail.SmtpClient("ho-ex2010-caht1.exchangeserverpro.net")
$smtp.Send("reports@exchangeserverpro.net","administrator@exchangeserverpro.net","Test Email","This is a test")

You could then launch the script from the command line as per this example:

powershell.exe -noexit c:\scripts\test.ps1

Note that PowerShell 2.0, which is installed by default on Windows 7 and Windows Server 2008R2, includes a simpler Send-MailMessage command, making things easier.

Option 2
If you're prepared to use third-party software, an option is something like this SendEmail command-line tool. It depends on your target environment, though; if you're deploying your batch file to multiple machines, that will obviously require inclusion (but not formal installation) each time.

Option 3
You could drive Outlook directly from a VBA script, which in turn you would trigger from a batch file; this would let you send an email using Outlook itself, which looks to be closest to what you're wanting. There are two parts to this; first, figure out the VBA scripting required to send an email. There are lots of examples for this online, including from Microsoft here. Essence of this is:

Sub SendMessage(DisplayMsg As Boolean, Optional AttachmentPath)
    Dim objOutlook As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem
    Dim objOutlookRecip As Outlook.Recipient
    Dim objOutlookAttach As Outlook.Attachment
    
    Set objOutlook = CreateObject("Outlook.Application")
    Set objOutlookMsg  = objOutlook.CreateItem(olMailItem)
    
    With objOutlookMsg
        Set objOutlookRecip = .Recipients.Add("Nancy Davolio")
        objOutlookRecip.Type = olTo
        ' Set the Subject, Body, and Importance of the message.
        .Subject = "This is an Automation test with Microsoft Outlook"
        .Body = "This is the body of the message." &vbCrLf & vbCrLf
        .Importance = olImportanceHigh  'High importance
        
        If Not IsMissing(AttachmentPath) Then
            Set objOutlookAttach = .Attachments.Add(AttachmentPath)
        End If
        
        For Each ObjOutlookRecip In .Recipients
            objOutlookRecip.Resolve
        Next
        
        .Save
        .Send
    End With
    Set objOutlook = Nothing
End Sub

Then, launch Outlook from the command line with the /autorun parameter, as per this answer (alter path/macroname as necessary):

C:\Program Files\Microsoft Office\Office11\Outlook.exe" /autorun macroname

Option 4
You could use the same approach as option 3, but move the Outlook VBA into a PowerShell script (which you would run from a command line). Example here. This is probably the tidiest solution, IMO.

Geoff
  • 8,551
  • 1
  • 43
  • 50
  • sorry, my script is batch based. – Mowgli Mar 15 '13 at 13:22
  • So you'd just create the PowerShell script, then launch it from your batch file. You'd use parameters, as per [this question](http://stackoverflow.com/questions/5592531/how-to-pass-an-argument-to-a-powershell-script). – Geoff Mar 15 '13 at 13:25
  • Thanks for great links, but all of those require login to smtp server and I cannot store my user and password to any script that is very insecure. – Mowgli Mar 15 '13 at 13:50
  • I added a couple of other options there. You may also want to reconsider doing this in a batch file - depends a lot on how/when/why you want to trigger, etc. – Geoff Mar 15 '13 at 15:11
  • 1
    Thanks Geoff, I ended up creating PowerShell script sends email using gmail `:(` I have no other option, and in my batch script I send output to txt file and call Powershell script from there and in my PS script I `cat` that file in body part of the script. it is work around but I got working temporarily. – Mowgli Mar 15 '13 at 15:15
  • Geoff, you should add this, link to your Option 1. it sends email via existing outlook setup via powershell but doesn't require smtp server or passs and stuff. http://www.andyparkhill.co.uk/2010/08/send-outlook-email-via-powershell.html – Mowgli Mar 15 '13 at 15:30
  • 1
    @Mowgli: The link you show there is an example of option 3, I think (which would be my solution, if it were me). – Geoff Mar 15 '13 at 15:42
  • 1
    @jvriesem are you using Bash from WSL2? If so, you could use the PowerShell approach above from my answer, and call the PowerShell script using something like `powershell.exe -File "c:\users\myuser\Desktop\mymailscript.ps1"`. Note that the path is the native windows path, not the `/mnt` path. – Geoff Jun 18 '21 at 18:45
  • @Geoff: I'm a Mac user whose organization uses Outlook. :-) Although I'm quite comfortable with Windows in general, I am a novice with PowerShell. I'm very familiar with Bash scripting, however, so I was wondering if there's a script approach to using Outlook on macOS. – jvriesem Jun 24 '21 at 17:42
  • @jvriesem ok, makes sense now! I'd say you want to ask a fresh question in that context, in that case – Geoff Jul 01 '21 at 01:53
5

Using Powershell you can send a Email and integrate this into Bat file script. But you need to have outlook installed for this on your machine. This worked for me on local PC but not on server. On server, it asks me to give permission everytime I use this command, also installing outlook on server might not be preferred for many. This works perfectly when ran on local PC

$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "xyz@mail.com"
$Mail.Subject = "Action"
$Mail.Body ="Pay rise please"
$Mail.Send()
Priya
  • 329
  • 3
  • 14
1

Send SMS/Text Messages from Command Line with VBScript!

If VBA meets the rules for VB Script then it can be called from command line by simply placing it into a text file - in this case there's no need to specifically open Outlook.

I had a need to send automated text messages to myself from the command line, so I used the code below, which is just a compressed version of @Geoff's answer above.

Most mobile phone carriers worldwide provide an email address "version" of your mobile phone number. For example in Canada with Rogers or Chatr Wireless, an email sent to <YourPhoneNumber>@pcs.rogers.com will be immediately delivered to your Rogers/Chatr phone as a text message.

* You may need to "authorize" the first message on your phone, and some carriers may charge an additional fee for theses message although as far as I know, all Canadian carriers provide this little-known service for free. Check your carrier's website for details.

There are further instructions and various compiled lists of worldwide carrier's Email-to-Text addresses available online such as this and this and this.


Code & Instructions

  1. Copy the code below and paste into a new file in your favorite text editor.
  2. Save the file with any name with a .VBS extension, such as TextMyself.vbs.

That's all!
Just double-click the file to send a test message, or else run it from a batch file using START.

Sub SendMessage()
    Const EmailToSMSAddy = "3215551212@pcs.rogers.com"
    Dim objOutlookRecip
    With CreateObject("Outlook.Application").CreateItem(0)
        Set objOutlookRecip = .Recipients.Add(EmailToSMSAddy)
        objOutlookRecip.Type = 1
        .Subject = "The computer needs your attention!"
        .Body = "Go see why Windows Command Line is texting you!"
        .Save
        .Send
    End With
End Sub

Example Batch File Usage:

START x:\mypath\TextMyself.vbs

Of course there are endless possible ways this could be adapted and customized to suit various practical or creative needs.

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
  • Thank you for your answer. I did the same. It ran without any errors. But I do not get any E-Mail in my mailbox. Also looked into Junk, Spam as well. Rechecked with spelling mistakes or any. Script runs with no issues. But why do I not see any mail ? :( This is disappointing. – Priya Sep 23 '21 at 15:53
  • This was intended not for sending email, but for SMS Messages (text messages), so theoretically you should have a message show up in your "Sent Items", and a text messages arrive at the phone number specified in the script.. – ashleedawg Sep 23 '21 at 17:00
  • ...However, in theory it should work for sending email as well, but I just tested it and it **no longer works for me** either. It's been a few years since I used that so I'm not sure what the problem could be, but my guess is it's related to some new security setting in Outlook that can probably be disabled. (oh, and you need to have Outlook installed.) Sorry I don't have time to troubleshoot. Nowadays I send SMS messages similarly through Email-to-SMS, but by calling a PHP script on my webserver with PHP's simple [`mail()`](https://www.w3schools.com/php/func_mail_mail.asp) command. – ashleedawg Sep 23 '21 at 17:03
  • Perhaps you need to change an Outlook setting to [allow "**programmatic access**"](https://learn.microsoft.com/en-us/outlook/troubleshoot/security/a-program-is-trying-to-send-an-email-message-on-your-behalf). I think when I wrote that answer it may have been enabled by default.... but that's just a guess. – ashleedawg Sep 23 '21 at 17:07
  • Also note that is not a `BAT` file… it’s a `VBS` file (visual basic script, which is similar to Office’s VBA). [Here](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/indexsrv/running-a-visual-basic-scripting-edition-query) is more info about running VBS. Another possible problem could be your antivirus software preventing access (since this could easily be abused by malware). Good luck & feel free to update the answer if you find a solution! :-) – ashleedawg Sep 23 '21 at 17:11
  • Thank you very much for your detailed reply. Appreciate it. I already also had gone through those outlook allow programmatic access settings as well. But unable to find out still. Also I found out that I could send mail using powershell scripting, but that too runs successfully without errors and returns no email on server (yes I need to install outlook on server for this on server, which I don't want to). On server, it asks everytime for my confirmation. I Will post incase if I find any solution. Thanks a lot for ur time – Priya Sep 24 '21 at 07:09
-5

You can use cURL and CRON to run .php files at set times.

Here's an example of what cURL needs to run the .php file:

curl http://localhost/myscript.php

Then setup the CRON job to run the above cURL:

nano -w /var/spool/cron/root
or
crontab -e

Followed by:

01 * * * * /usr/bin/curl http://www.yoursite.com/script.php

For more info about, check out this post: https://www.scalescale.com/tips/nginx/execute-php-scripts-automatically-using-cron-curl/

For more info about cURL: What is cURL in PHP?

For more info about CRON: http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800

Also, if you would like to learn about setting up a CRON job on your hosted server, just inquire with your host provider, and they may have a GUI for setting it up in the c-panel (such as http://godaddy.com, or http://1and1.com/ )

NOTE: Technically I believe you can setup a CRON job to run the .php file directly, but I'm not certain.

Best of luck with the automatic PHP running :-)

Community
  • 1
  • 1
Jacob Topping
  • 126
  • 1
  • 8