Sending mail from a Bash shell script

前端 未结 12 1811
梦毁少年i
梦毁少年i 2021-01-29 22:54

I am writing a Bash shell script for Mac that sends an email notification by opening an automator application that sends email out with the default mail account in Mail.app. The

12条回答
  •  野性不改
    2021-01-29 23:36

    Probably the only way you could do this, while keeping the program self-sufficient, is if you have direct access to an SMTP server from the clients.

    If you do have direct access to an SMTP server you can use the SMTP example from wikipedia and turn it into something like this:

    #!/bin/bash
    telnet smtp.example.org 25 <<_EOF
    HELO relay.example.org
    MAIL FROM:
    RCPT TO:
    DATA
    From: Joe 
    To: Jane 
    Subject: Hello
    
    Hello, world!
    .
    QUIT
    _EOF
    

    To handle errors I would redirect the output from telnet to a file and then grep that for a "success message" later. I am not sure what format the message should be, but I see something like "250 2.0.0 Ok: queued as D86A226C574" in the output from my SMTP server. This would make me grep for "^250.*queued as".

提交回复
热议问题