send email using command line

Hi all,

I was looking for a command to send emails but couldn't find proper setup to do so. What I'm trying to achieve is to be able to send emails from a workstation using command line. I do not run any mail server so i will have to use my ISP email servers.

Can somebody provide some help on how to do this?

MacBook Pro; iMac G5, Mac OS X (10.4.8)

Posted on Feb 4, 2007 12:19 PM

Reply
10 replies

Feb 4, 2007 1:00 PM in response to costicladop

Firstly, when sending mail, mail will use the local mail server, so youre going to need to enable mail on your machine no matter what.

Don't worry, it's not a big deal. You don't need to setup your server to handle incoming mail from the network, deal with mail clients, POP, IMAP, etc., just enable the outgoing mail server so that you can send mail.

So, in Server Admin, enable outgoing mail and add your ISP's mail server as the relay server (this is the server it will send all non-local mail to).

Once you've done that you can simply use mail at the command line via mail user@domain.com, like:

<pre class=command>localhost $ mail user@domain.com
Subject: This is a test

This is only a test. If this were a real email it might have been worth reading.</pre>

In this example I invoked mail user@domain.com
mail prompted me for the Subject line, after which I typed in the message. Use Ctrl-D on a blank line to signify the end of the message.

You can also specify the subject line when you invoke mail, using the -s switch, in which case mail doesn't prompt you for the Subject line:

<pre class=command>mail -s "this is the subject" user@domain.com</pre>

Finally, if you're trying to capture output from another shell command you can pipe the message into mail:

<pre class=command>cat /path/to/some/file | mail -s "here's the subject" user@domain.com</pre>

Note, though, that the output of the shell command will become the message body - you can't just attach files in this way since they need to be MIME-encoded to get through the mail system.

Feb 4, 2007 8:04 PM in response to costicladop

>I do not have any mail server at all and Server Admin accepts only connections to MacOS X server. Is it still possible to setup this on a MacOS X 10.4 workstation?

Since you posted in the Mac OS X Server forum I assumed you were running Mac OS X Server... oh well. The same mail server processes are installed, but you don't get a GUI to configure them.

>I was really surprised to get the email since my ISP requires authentication to send emails and it's using port 587.

I'm not surprised at all. As far as your ISP is concerned, this could be any mail coming in from anywhere for you - you'd expect them to accept the message if it was coming from any other domain, and at the moment this is no different.

You will likely have problems, however, if you don't configure your mail server to relay messages and you try to email other domains. Your ISP probably trust you because you have an IP address in their space, but other ISPs won't be so generous.

Feb 4, 2007 3:04 PM in response to David_x

when i try this:
mail -s test_terminal my emailaddress
i get the email but mail.log shows following entries

Feb 4 17:50:21 iMacG5 postfix/master[1203]: daemon started -- version 2.1.5
Feb 4 17:50:22 iMacG5 postfix/pickup[1204]: C528054663D: uid=501 from=<local_username>
Feb 4 17:50:22 iMacG5 postfix/cleanup[1205]: C528054663D: message-id=<20070204225021.C528054663D@iMacG5.local>
Feb 4 17:50:23 iMacG5 postfix/qmgr[1207]: 32F30546609: from=<local_username@iMacG5.local>, size=336, nrcpt=1 (queue active)
Feb 4 17:50:23 iMacG5 postfix/qmgr[1207]: 32F30546609: removed
Feb 4 17:50:23 iMacG5 postfix/qmgr[1207]: 48552546502: from=<local_username@iMacG5.local>, size=315, nrcpt=1 (queue active)
Feb 4 17:50:23 iMacG5 postfix/qmgr[1207]: C528054663D: from=<local_username@iMacG5.local>, size=336, nrcpt=1 (queue active)
Feb 4 17:50:53 iMacG5 postfix/smtp[1209]: connect to mx1.myISP.com[IP.add.res.s]: Operation timed out (port 25)
Feb 4 17:50:53 iMacG5 postfix/smtp[1208]: connect to mx1.myISP.com[IP.add.res.s]: Operation timed out (port 25)
Feb 4 17:51:21 iMacG5 postfix/master[1203]: master exit time has arrived
Feb 4 17:51:23 iMacG5 postfix/smtp[1209]: connect to mx1.myISP.com[IP.add.res.s]: Operation timed out (port 25)
Feb 4 17:51:23 iMacG5 postfix/smtp[1208]: connect to mx1.myISP.com[IP.add.res.s]: Operation timed out (port 25)
Feb 4 17:51:23 iMacG5 postfix/smtp[1208]: warning: 48552546502: trace service failure
Feb 4 17:51:23 iMacG5 postfix/smtp[1208]: warning: 48552546502: defer service failure
Feb 4 17:51:23 iMacG5 postfix/smtp[1208]: 48552546502: to=<my emailaddress>, relay=mx2.myISP.com[IP.add.res.s2], delay=1213, status=deferred (48552546502: trace service failed)
Feb 4 17:51:23 iMacG5 postfix/smtp[1208]: warning: 48552546502: trace service failure
Feb 4 17:51:23 iMacG5 postfix/smtp[1208]: warning: 48552546502: trace service failure
Feb 4 17:51:53 iMacG5 postfix/smtp[1209]: connect to mx2.myISP.com[IP.add.res.s2]: Operation timed out (port 25)
Feb 4 17:51:53 iMacG5 postfix/smtp[1209]: C528054663D: to=<my emailaddress>, relay=mx2.myISP.com[IP.add.res.s], delay=92, status=sent (250 ok dirdel)

I was really surprised to get the email since my ISP requires authentication to send emails and it's using port 587.

Feb 5, 2007 2:07 AM in response to costicladop

I had a quick look at 'mail' command (man page) and could not see a way to enter a relay server (your ISP) in the config file (however might be some way if you read elsewhere about it). Note from the logs that the mail daemon is looking up the MX record for recipient so, as Camelot says, you will likely be blocked by other servers since you will be exhibiting same characteristics as a spammer (no real HELO hostname, etc).

As an alternative, it may be worth looking at using Applescript with Mail app and then calling this from inside the script (using 'osascript').

-david

Feb 5, 2007 6:56 AM in response to costicladop

One quick and dirty way I've heard of sending mail from the command line is using telnet.

Telnet to your ISPs SMTP server using port 25, then use standard SMTP commands to write the email you want to send. I don't remember the exact method offhand, but if you search for "HELO" or "EHLO" you should be able to find the instructions to do this.

Good luck!

-Gregg

Feb 5, 2007 10:38 AM in response to David_x

>I had a quick look at 'mail' command (man page) and could not see a way to enter a relay server (your ISP) in the config file

Simple - you don't.

The command line mail will take the mesage ad submit it to the mail server running on the local machine.

That mail server's configuration will tell the mail server what to do with it (e.g. deliver it locally, send it directly to the destination domain's mail server, or relay it through another server). So what you're looking for is the relay configuration for postfix (the mail server).

Edit /etc/postfix/main.cf and edit (or add) the line:

<pre class=command>relayhost = mailserver.yourisp.net</pre>

(where 'mailserver.yourisp.net' is, of course, the address of your ISP's mail server that you want to relay through.

Feb 5, 2007 5:25 PM in response to costicladop

Thanks Camelot, I hadn't really appreciated that it was postfix which was built into the clients. Pretty obvious I suppose thinking about it (as in so much of the server open-source stuff is actually in the client as well and vice-versa).

costicladop, the sender name appears to be the user name the command is run under. So if run as root then the sender name will be 'root'. The domain/hostname can be set using myhostname = mydomain.com in /etc/postfix/main.cf. You can also set a different 'Reply To:' address using the ~R escape char in a line of the mail message (see 'man mail').

All fun stuff - and all going back how many decades since unix started? 🙂

-david

Server 10.4.8

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

send email using command line

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.