-
-
Save andreineculau/38f6160cc9aa11956953 to your computer and use it in GitHub Desktop.
a nice way to send mail from the shell in osx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env osascript | |
# usage: sendmail.app [email protected] [email protected],[email protected] "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 | |
-- "[email protected]" | |
set theSender to item 1 of argv | |
-- "[email protected],[email protected]" | |
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