Sending mail from a Bash shell script

前端 未结 12 1494
梦毁少年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:38

    1) Why not configure postfix to handle outbound mail only and relay it via a mail gateway? Its biggest advantage is that it is already installed on OS X clients.

    2) Install and configure one of the lightweight MTAs that handle only outbound mail, like nullmailer or ssmtp (available via MacPorts).

    In both cases use mailx(1) (or mutt if you want to get fancy) to send the mails from a shell script.

    There are several questions on Server Fault that go into the details.

    0 讨论(0)
  • 2021-01-29 23:40

    There is a program called Sendmail.

    You probably don't want to use the -bs command unless you are sending it as raw SMTP like Martin's example. -bs is for running an SMTP server as a deamon. Sendmail will send directly to the receiving mail server (on port 25) unless you override it in the configuration file. You can specify the configuration file by the -C paramter.

    In the configuration, you can specify a relay server (any mail server or sendmail running -bs on another machine)

    Using a properly configured relay server is good idea because when IT manages mail servers they implement SPF and domain keys. That keeps your mail out of the junk bin.

    If port 25 is blocked you are left with two options.

    1. Use the corporate SMTP server.
    2. Run sendmail -bd on a machine outside of the corporate firewall that listens on a port other than 25.

    I believe you can add configuration parameters on the command line. What you want is the SMART_HOST option. So call Sendmail like sendmail -OSMART_HOST=nameofhost.com.

    0 讨论(0)
  • 2021-01-29 23:42

    Here is a simple Ruby script to do this. Ruby ships on the Mac OS X versions you mentioned.

    Replace all the bits marked 'replace'. If it fails, it returns a non-zero exit code and a Ruby back trace.

    require 'net/smtp'
    
    SMTPHOST = 'replace.yoursmtpserver.example.com'
    FROM = '"Your Email" <youremail@replace.example.com>'
    
    def send(to, subject, message)
      body = <<EOF
    From: #{FROM}
    To: #{to}
    Subject: #{subject}
    
    #{message}
    EOF
      Net::SMTP.start(SMTPHOST) do |smtp|
        smtp.send_message body, FROM, to
      end
    end
    
    send('someemail@replace.example.com', 'testing', 'This is a message!')
    

    You can embed this in a Bash script like so:

    ruby << EOF
     ... script here ...
    EOF
    

    For some other ways to send Ruby emails, see Stack Overflow question How do I send mail from a Ruby program?.

    You can use other languages that ship with Mac OS X as well:

    • How do I send email with Perl?
    • Sending HTML email using Python
    0 讨论(0)
  • 2021-01-29 23:42

    sendmail and even postfix may be too big to install if all you want to do is to send a few emails from your scripts.

    If you have a Gmail account for example, you can use Google's servers to send email using SMTP. If you don't want to use gGoogle's server, as long as you have access to some SMTP server, it should work.

    A very lightweight program that makes it easy to do so is msmtp. They have examples of configuration files in their documentation.

    The easiest way to do it would be to set up a system-wide default:

    account default
    host smtp.gmail.com
    from john.doe@gmail.com
    user john.doe@gmail.com
    password XXX
    port 587
    

    msmtp should be very easy to install. In fact, there is a port for it, so it could be as easy as port install msmtp.

    After installing and configuring msmtp, you can send email to john.doe@gmail.com using:

    mail -s <subject> john.doe@gmail.com <<EOF
    <mail text, as many lines as you want. Shell variables will be expanded>.
    EOF
    

    You can put the above in a script. See man mail for details.

    0 讨论(0)
  • 2021-01-29 23:44

    Try mtcmail. Its a fairly complete email sender, completely standalone.

    0 讨论(0)
  • 2021-01-29 23:48

    Here's a modified shells script snip I've used on various UNIX systems... (echo "${MESSAGE}" | ${uuencode} ${ATTACHMENT}$basename ${ATTACHMENT}) | ${mailx} -s "${SUBJECT}" "${TO_LIST}"

    uuencode and mailx are set to the executables. The other variables are from user input parsed using getopts.

    This does work but I have to admit more often than not I use a simple Java program to send console emails.

    0 讨论(0)
提交回复
热议问题