• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Sending mail w/attachment from Terminal
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sending mail w/attachment from Terminal


  • Subject: Re: Sending mail w/attachment from Terminal
  • From: Stan Cleveland <email@hidden>
  • Date: Tue, 28 Feb 2017 12:44:35 -0800

Stan Cleveland   ❖  Color Technology Inc.  ❖  2455 NW Nicolai St.  ❖  Portland OR 97210
Workflow Developer  ❖  Cell 503.702.0721  ❖  CTI 503.294.0393  ❖  Fax 503.294.0820

> On Feb 28, 2017, at 11:26 AM, debt <email@hidden> wrote:
>
> Is there a way to send an email with an attachment from the Terminal using AppleScript?

Hi Marc,

I've held the script below rather close to the vest and consider it something of a trade secret. But since you asked, I'm posting it here, for whatever value it may have. I can't guarantee it, but I suspect that it will work with OS 10.4

Take note of the two warnings in the header. Also note that the email account credentials are in plain text, which is obviously poor security, unless the computer is inaccessible to others. Fixing this breach is left as an exercise for the reader.

For those using newer software versions than those listed, getting the system, Ruby, and the Gems to work together is also left as an exercise for the reader.

Regards,
Stan C.

(*********************************************************
	Send Email With Attachment AppleScript
	Written by Stan Cleveland
		Color Technology Inc.
		2455 NW Nicolai Street
		Portland OR 97210
**********************************************************
	Script dependencies:
		Mac OS X v10.6 or higher		<== MAY BE NEGOTIABLE
		Ruby 1.8.7 or higher
		RubyGems 1.8.11 or higher
		Gem Mail 2.5.3 or lower		<== NOTE
		Gem Pony 1.4 or higher
**********************************************************
	Notes:
	1.	WARNING! The "Mail" RubyGem MUST be version 2.5.3 or lower.
		If it is 2.5.4 or greater, sending an email with this library will fail.
		This is due to incompatibilities between this Gem and Ruby 1.8.7.
	2.	WARNING! The only character that can't be included in an email or
		email address is a carat (^), which has a low frequency of usage
		(per http://mdickens.me/typing/theory-of-letter-frequency.html).
**********************************************************
	Version history:
		v1r0  (2011/03/21) -- Initial version.
		…
		v2r1  (2015/12/16) -- Final version.
**********************************************************)


----------------------------------------------------------------------------------
------------------------     PROPERTIES FOLLOW     ------------------------
----------------------------------------------------------------------------------
property deploymentVersion : "v2r1"
-- email
property mailSMTP : "smtp.googlemail.com"
property mailAuthUID : "email@hidden" -- email address
property mailAuthPword : "yourpassword"


------------------------------------------------------------------------------------------
--------------------------------     MAIN PROGRAM     --------------------------------
------------------------------------------------------------------------------------------
set mailFrom to "Test Process <email@hidden>"
set mailTo to {"Stan Cleveland <email@hidden>"}
set mailSubject to "Email test"
set mailBody to "Now is the time for all good men to come to the aid of their country."
set mailCC to "" --	may be single string or a list of strings
set mailBCC to "" --	may be single string or a list of strings
set replyTo to "Stan Cleveland <email@hidden>"
set mailAttachPath to "MacHD:Users:stanc:Desktop:Script Libraries.zip"
set mailAttachName to "Script Libraries.zip"
set logMessage to emailSendMessageWithAttachment(mailFrom, mailTo, mailSubject, ¬
	mailBody, mailCC, mailBCC, replyTo, mailAttachPath, mailAttachName)


------------------------------------------------------------------------------------------
-----------------------------------     HANDLERS     -----------------------------------
------------------------------------------------------------------------------------------
on emailSendMessageWithAttachment(mailFrom, mailTo, mailSubject, mailBodyHTML, ¬
	mailCC, mailBCC, replyTo, mailAttachPath, mailAttachName)
	if (mailSubject is "") and (mailBodyHTML is "") then
		-- don't send empty message
		set logMessage to "Email not sent; no subject or body."
	else
		-- escape single quotation marks
		set mailSubject to escapeDoubleQuotes(mailSubject)
		set mailBodyHTML to escapeDoubleQuotes(mailBodyHTML)
		-- create email instructions
		set theScript to ("ruby -rubygems -e \"gem 'pony' ; require 'pony' ; " & ¬
			"Pony.mail(" & ¬
			":subject => %^" & mailSubject & "^, " & ¬
			":headers => { 'Content-Type' => 'text/html' }, " & ¬
			":body => %^" & mailBodyHTML & "^, " & ¬
			":to => " & prepEmailAddresses(mailTo) & ", " & ¬
			":from => %^" & mailFrom & "^, " & ¬
			":reply_to => %^" & replyTo & "^, ")
		if mailCC is not in {"", {}} then -- append CC instruction
			set preppedMailCC to prepEmailAddresses(mailCC)
			set theScript to (theScript & ":cc => " & preppedMailCC & ", ")
		end if
		if mailBCC is not in {"", {}} then -- append BCC instruction
			set preppedMailBCC to prepEmailAddresses(mailBCC)
			set theScript to (theScript & ":bcc => " & preppedMailBCC & ", ")
		end if
		if mailAttachPath is not "" then -- append attachment
			if mailAttachPath contains ":" then set mailAttachPath to POSIX path of mailAttachPath
			set theScript to (theScript & ":attachments => {" & quoted form of mailAttachName & ¬
				" => File.read(" & quoted form of mailAttachPath & ")}, ") -- more than one attachment, perhaps?
		end if
		--send the email
		set theFullScript to (theScript & ¬
			":charset => 'utf-8', " & ¬
			":via => :smtp, " & ¬
			":via_options => {" & ¬
			":address => '" & mailSMTP & "', " & ¬
			":user_name => '" & mailAuthUID & "', " & ¬
			":password => '" & mailAuthPword & "', " & ¬
			":authentication => :plain, " & ¬
			":port => '587', " & ¬
			":enable_starttls_auto => true " & ¬
			"})\"")
		set logMessage to do shell script theFullScript
	end if
	return logMessage
end emailSendMessageWithAttachment

on escapeDoubleQuotes(theText)
	if theText contains "\\\"" then
		-- in case text contains a mix of simple and complex forms
		tid("\\\"") -- escaped backslash, escaped quote mark
		set textParts to text items of theText
		tid("\"") -- escaped quote mark
		set theText to textParts as text
	end if
	if theText contains "\"" then
		-- now convert all escaped quote marks to Ruby-compatible format
		tid("\"") -- escaped quote mark
		set textParts to text items of theText
		tid("\\\"") -- escaped backslash, escaped quote mark
		set theText to textParts as text
		tid("")
	end if
	return theText
end escapeDoubleQuotes

on prepEmailAddresses(addressOrAddresses)
	if class of addressOrAddresses is list then
		return rubyizeListToArray(addressOrAddresses)
	else if class of addressOrAddresses is text then
		return ("%^" & addressOrAddresses & "^")
	else
		return ""
	end if
end prepEmailAddresses

on rubyizeListToArray(aList)
	tid("^, %^")
	set lst to aList as text
	tid("")
	return ("[%^" & lst & "^]") as string
end rubyizeListToArray

on tid(d)
	set AppleScript's text item delimiters to d
end tid


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden


  • Follow-Ups:
    • Re: Sending mail w/attachment from Terminal
      • From: debt <email@hidden>
References: 
 >Sending mail w/attachment from Terminal (From: debt <email@hidden>)

  • Prev by Date: Convert File or Path Input into NSURL, HFS path, or POSIX Path Output
  • Next by Date: Re: Restoring the Finder Selection After Moving an Item or Items.
  • Previous by thread: Re: Sending mail w/attachment from Terminal
  • Next by thread: Re: Sending mail w/attachment from Terminal
  • Index(es):
    • Date
    • Thread