Skip to content

Instantly share code, notes, and snippets.

@andreineculau
Forked from masnick/gist:6985205
Last active February 2, 2018 16:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreineculau/38f6160cc9aa11956953 to your computer and use it in GitHub Desktop.
Save andreineculau/38f6160cc9aa11956953 to your computer and use it in GitHub Desktop.
a nice way to send mail from the shell in osx
#!/usr/bin/env osascript
# usage: sendmail.app from@me.com to@you.com,and@you.com "have you seen this?" "a nice way to send mail/attachment from the shell in osx (if you have configured Mail.app) https://gist.github.com/andreineculau/38f6160cc9aa11956953"
on theSplit(theString, theDelimiter)
-- save delimiters to restore old settings
set oldDelimiters to AppleScript's text item delimiters
-- set delimiters to delimiter to be used
set AppleScript's text item delimiters to theDelimiter
-- create the array
set theArray to every text item of theString
-- restore the old setting
set AppleScript's text item delimiters to oldDelimiters
-- return the result
return theArray
end theSplit
on replace(input, find, replacement)
set text item delimiters to find
set ti to text items of input
set text item delimiters to replacement
ti as text
end replace
--Variables
on run argv
-- "from@me.com"
set theSender to item 1 of argv
-- "to@you.com,and@you.com"
set theRecipients to theSplit(item 2 of argv, ",")
-- "test"
set theSubject to item 3 of argv
-- "first\nsecond\nsignature"
set theContent to item 4 of argv
set theContent to replace(theContent, "\\n", return)
if length of argv > 4 then
-- "~/some_file"
set theAttachment to item 5 of argv
else
set theAttachment to ""
end if
--Mail Tell Block
tell application "Mail"
--Create the message
set theMessage to make new outgoing message
--Set a recipient
tell theMessage
set the sender to theSender
repeat with theRecipient in theRecipients
make new to recipient with properties {address: theRecipient}
end repeat
set the subject to theSubject
set the content to theContent
if theAttachment is not equal to "" then
make new attachment with properties {file name:((theAttachment as POSIX file) as alias)}
end if
--Send the Message
send
--save
close
end tell
end tell
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment