how to email a file from command line?

LuKreme kremels at kreme.com
Sun Mar 9 04:03:29 PDT 2008


On 8-Mar-2008, at 09:52, Gregg Dinse wrote:
> I know this is a dumb question, and I tried to figure it out from  
> the man pages, but I had no luck.  I am at home and I need to  
> retrieve a Word document from my MacOSX machine at work.  Suppose  
> the file is called junk.doc and thatdummy at company.com is my email  
> address.  I can ssh to my mac at work.  Once I get there and cd to  
> the correct directory, what command line instruction can I give to  
> email junk.doc to myself as an attachment?

Unless the postfix mail service on your machine is enabled and setup,  
you will probably need an Applescript (via osascript) or a CLI mail  
client to do this, or you will need to look into using the sendmail  
command, which I think can be configured to use a SMTP server for  
outbound relaying.

> I tried sending a test message by typing:  mail dummy at company.com


> but I could not even figure out how to gracefully end the process.   
> When I typed ^Z, it stopped, but nothing was mailed.  Thanks in  
> advance for any answers to this dumb question.

OK.  First issue is actually SENDING the mail.  Page down for the  
second issue.

^Z is 'suspend' so all you did was background the process.  The  
default way out of a interactive mail window is ^D or a '.' on a line  
by itself.

You can also type a '~v' on a line by itself and get a vi window or  
'~e' to get your default CLI editor.

I just did a test from the CLI:

Mar  9 04:21:36 cerebus postfix/master[4380]: daemon started --  
version 2.4.3, configuration /etc/postfix
Mar  9 04:21:36 cerebus postfix/pickup[4381]: 7ADBF6D189C5: uid=501  
from=<kreme>
Mar  9 04:21:36 cerebus postfix/cleanup[4383]: 7ADBF6D189C5: message- 
id=<20080309102136.7ADBF6D189C5 at cerebus.local>
Mar  9 04:21:36 cerebus postfix/qmgr[4382]: 7ADBF6D189C5: from=<kreme at cerebus.local 
 >, size=307, nrcpt=1 (queue active)
Mar  9 04:21:36 cerebus postfix/smtp[4385]: 7ADBF6D189C5: to=<kreme at kreme.com 
 >, relay=mail.covisp.net[64.140.43.68]:25, delay=0.41,  
delays=0.05/0.03/0.21/0.11, dsn=2.0.0, status=sent (250 Ok: queued as  
D8056118AD34)
Mar  9 04:21:36 cerebus postfix/qmgr[4382]: 7ADBF6D189C5: removed

and on the remote mailserver

Mar  9 04:21:36 akane postfix/qmgr[1052]: D8056118AD34: from=<kreme at cerebus.local 
 >, size=512, nrcpt=1 (queue active)

So it appears that in Leopard, mail from the command-line knows to do  
the right thing (startup postfix's queue and send the mail), BUT keep  
in mind that my remote server only accepted the message because it  
knows my IP address.  This is what happened when I tried to sent to  
mac.com or gmail.com:

Mar  9 04:25:19 cerebus postfix/qmgr[4445]: 273666D18FBF: from=<kreme at cerebus.local 
 >, size=331, nrcpt=1 (queue active)
Mar  9 04:25:19 cerebus postfix/smtp[4448]: 273666D18FBF: to=<*munged*@mac.com 
 >, relay=smtp-mx1.mac.com[17.148.20.64]:25, delay=0.8,  
delays=0.08/0.01/0.62/0.09, dsn=5.1.8, status=bounced (host smtp- 
mx1.mac.com[17.148.20.64] said: 553 5.1.8 <kreme at cerebus.local>...  
Domain of sender address kreme at cerebus.local does not exist (in reply  
to MAIL FROM command))

So, while Leopard is now smart enough to start postfix and send, it  
doesn't have the SMTP setting from mail.app so that your message will  
actually go out.

I *BELIEVE* this is possible to do on the command line using the  
'sendmail' command and that there is a way to specify the SMTP server  
you want to use, but my quick look at man sendmail(1) didn't discover  
it.

Probably the easiest thing to do is edit /etc/postfix/main.cf and add

relayhost = mail.smtpserver.tld

in the right section (search for relayhost)

If you are an admin on your machine at work, you can use

$ sudo nano /etc/postfix/main.cf

Then type ^W to search, and type relayhost

That will take you to the right area.  Just use the arrow keys to move  
down after the last commented line and put in your own relayhost there.

^X and 'Y' to exit and save and you should then be able to type

mail <any address> < file

And that brings us to our second issue...

That email will not work for anything but a plain text file.  Without  
a real mail client like mutt, you will have to encode the file  
yourself before trying to send it or you will get gibberish.  If the  
'file' is a bundle, you will need to first tar it, then encode it.

$ uuencode Pictures/Desktops/Irony.jpg Irony.jpg | mail kreme at kreme.com
$ tar xzf myfile.tgz myfile.bundle
$ uuencode myfile.tgz Myfile.tgz | mail kreme at kreme.com

Note that uuencode requires TWO args, the path to the file, and the  
filnename you want to embed in the uuendcode.

So, it's all possible from the command line.

For my money, what I would do is port install mutt and set it up.


-- 
A man, in a word, who should never have been taught to write and whom,  
if unhappily gifted with that ability, should have been restrained by  
an Act of Parliment from writing Reminiscences.  - PG Wodehouse




More information about the MacOSX-talk mailing list