Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Using mail in Shell script

Hey guys,


I am writing a shell script to parse some data and create a csv file when done. I want to be able to email the csv file when the script is done. I cannot seem to figure out how to do this. I am running Mountain Lion and I have tried mail -s, mailx at the command line in Terminal and in the script and when I go check my gmail account there is no email there. Its not in the spam folders or the junk folder. Any help would be appreciated

iMac, OS X Mountain Lion, Using Terminal and a shell script

Posted on Aug 2, 2012 8:44 AM

Reply
Question marked as Best reply

Posted on Aug 2, 2012 10:10 AM

As far as I recall, the mail command does not interact with Apple's Mail program, so you would probably need to configure sendmail for it to work. It would be easier, I think, to use Applescript for that part, as it is easy to invoke Mail.

24 replies

Aug 2, 2012 5:26 PM in response to Austin Kinsella1

Austin,


I found osascript and was reading about it, here is my script so far:

-- Variables

set recipientName to "Mark Perryman"

set recipientAddress to "appledad05@gmail.com"

set theSubject to "Tupperware Team CSV File"

set theContent to "Here is your CSV file created from your website" & return


on run argv

set fileName to "HD:Users:markperryman:GinaApp:" & item 1 of argv

end run


-- Mail Tell Block

tell application "Mail"

--Create the message

set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}

-- Set a recipient

tell theMessage

make new to recipient with properties {name:recipientName, address:recipientAddress}

make new attachment with properties {file name:fileName as alias} at after the last paragraph

-- Send the Message

send

end tell

end tell


I get this error

error "The variable argv is not defined." number -2753 from "argv"


When I added the on run argv I get this error, what am I doing wrong

The run handler is specified more than once, or there were top-level commands in addition to the run handler.


Any help would be much appreciated

Aug 2, 2012 5:29 PM in response to appledad05

Plus my shell script told me this:


here is the line in shell:

osascript send_email_gina.scpt gina_${local_date}.csv


error:

end_email_gina.scpt: execution error: This script contains uncompiled changes and cannot be run. (-2700)


how do I compile it to run in the .sh file. I though hitting the compile button in the script editor would compile it.

Aug 3, 2012 1:32 AM in response to appledad05

The immediately obvious thing is that your on run ... end run should wrap the whole script, not just the set filename.


It is not an error, but the visible:true in the make new is unnecessary; the default is false. You would only put that in on a script that was doing the basics of a message, then leaving it on screen for the user to finish and press send. When the script is doing the send, all the user will get is a flash on the screen.


As for calling it from the shell, I have never actually done that! However, compile does not actually save the script, so if the script editor is open, you make a change, compile to check it - and the shell script can't run it. It is just as handy to press save instead of compile - if there are errors it won't compile properly anyway.

Aug 3, 2012 10:00 AM in response to Austin Kinsella1

Austin,


OK one last thing I have been reading on how to put a signature in the email and I think I'm missing something:


Code Im using:


make new signature with properties {name:"My Sig", content:"Mark Perryman"}
set the message signature of outgoing message 1 to signature "My Sig"


When I look at the dictionary it tells me that name and content are properties on the container signature. However I get the following error when running:

Mail got an error: Can’t make or move that element into that container.


Why is this? I just cant figure it out.

Any help would be great. Thanks so much for all your help so far with my script. The attachment is working now, this is just my last thing.

Aug 3, 2012 10:40 AM in response to appledad05

-- Mail Tell Block

tell application "Mail"

set theSig to signature "Cloydagh" -- I have one named Cloydagh

--Create the message

set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}


-- Set a recipient

tell theMessage

make new to recipient with properties {name:recipientName, address:recipientAddress}

set message signature to theSig

make new attachment with properties {file name:fileName as alias} at after the last paragraph


-- Send the Message

send

end tell

end tell


That works for me - more or less, I didn't test it with the make new attachment ...

Aug 3, 2012 10:54 AM in response to Austin Kinsella1

You say you have one called Cloydagh. How do I create one like that with a different name odviously.


Here is my code:

on run argv

-- Set the Variables Needed

set recipientName to "Kristine Perryman"

set recipientAddress to "appledad05@gmail.com"

set theSubject to "Tupperware Team CSV File"

set theContent to "Here is your CSV file. You can open this directly in MS Excel" & return

set fileName to "HD:Users:markperryman:GinaApp:" & item 1 of argv

-- Mail Tell Block

tell application "Mail"

set theSig to signature "My Sig"

--Create the message

set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:false}

-- Set a recipient

tell theMessage

make new to recipient with properties {name:recipientName, address:recipientAddress}

make new attachment with properties {file name:fileName as alias} at after the last paragraph

set message signature to theSig

-- Send the Message

send

end tell

end tell

end run


I get error: Mail got an error: Can’t get signature "My Sig". I am sure cause it doesnt have one created.


I dont know how to create the signature, can you show me an example of how you created yours?

Aug 3, 2012 1:01 PM in response to appledad05

My guess would be you create one in mail. /Applications/Mail


mail > preferences > signatures

I put yourNameHere in all signatures.



User uploaded file



--------------------------------------------------------------

You migh like the try statement to ignore errors.


try

move every item of folder currentFolder of the disk fileVolume to folder priorFolder of the disk fileVolume with replacing

on error errorMsg

display dialog "error-1 " & errorMsg giving up after 30

end try

Aug 3, 2012 1:09 PM in response to rccharles

Here are some Applescript hints.


AppleScript

Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X, Third Edition the book


AppleScript Language Guide pdf download the pdf file


Intro to applescript with sending an email ( You are probably beyond this. )
http://mac.appstorm.net/how-to/applescript/the-ultimate-beginners-guide-to-apple script/



--------------------------------------------------------------


(*


It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on. Here is an example.



Author: rccharles


For testing, run in the Script Editor.

1) Click on the Event Log tab to see the output from the log statement

2) Click on Run


*)



on run

-- Write a message into the event log.

log " --- Starting on " & ((current date) as string) & " --- "

-- debug lines

set desktopPath to (path to desktop) as string

log "desktopPath = " & desktopPath

end run

Using mail in Shell script

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