shellscript an attachment?

Posted on
Wed Apr 18, 2012 10:06 pm
T-Power offline
User avatar
Posts: 220
Joined: May 10, 2010

shellscript an attachment?

Hello All,

Hoping all is well with you and your family.
Been having some issues with the mail.app sending without asking for that ol pesky password etc.

I was wondering if your time permitted if you could help with a few small lines of code to get me in the right direction.

Here is what I have that works when mail.app is behaving properly:
Code: Select all
tell application "SecuritySpy"
   capture image camera number 2 as "/Users/bob/Desktop/SecuritySpyimage.jpg"
end tell

set theSubject to "Mailbox Sensor Detection"
set theSender to "cornmeal@gmail.com"
set theAddress to "bobby@aol.com"
set theAttachment to "/Users/bob/Desktop/SecuritySpyimage.jpg"

tell application "Mail"
   set theMessage to make new outgoing message with properties {subject:theSubject, sender:theSender}
   
   tell theMessage
      set visible to false
      make new to recipient at end of to recipients with properties {address:theAddress}
      make new attachment with properties {file name:theAttachment} at after the last paragraph
   end tell
   send theMessage
end tell

tell application "Finder"
   activate
   if file "Macintosh HD:Users:bob:Desktop:SecuritySpyimage.jpg" exists then
      delete file "Macintosh HD:Users:bob:Desktop:SecuritySpyimage.jpg"
   end if
   delay 1
end tell

tell application "System Events"
   set visible of process "Mail" to false
end tel


Mail.app makes me nervous cause I never know when it will fail to send due to asking for password.

Here is what I need to work:
Code: Select all
tell application "SecuritySpy"
   capture image camera number 2 as "/Users/bob/Desktop/SecuritySpyimage.jpg"
end tell

do shell script "echo |mail -s 'Email Subject' bobby@aol.com -a /Users/bob/Desktop/SecuritySpyimage.jpg"

tell application "Finder"
   activate
   if file "Macintosh HD:Users:bob:Desktop:SecuritySpyimage.jpg" exists then
      delete file "Macintosh HD:Users:bob:Desktop:SecuritySpyimage.jpg"
   end if
   delay 1
end tell


I just need help with the shell script part. So close and yet so far :?

T.I.A.

MacMini 2.3 GHz Intel Core i7 16GB DDR3
Indigo Pro 2022.1 macOS Mojave 10.14.6

Posted on
Wed Apr 18, 2012 11:47 pm
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: shellscript an attachment?

What about those scripts aren't working (other than Mail randomly prompting for the password)?

One thing I noticed that may be an issue is that, immediately after your script tells Mail (or the command-line mail program) to send the attachment, the script tells the Finder to delete the same attachment. It takes a few seconds for both Mail.app and the command-line mail program to actually send a message with an attachment, so it's entirely possible that the Finder is occasionally (depending on system load and how the OS schedules AppleEvents under that load) deleting the attachment before the Mail program can actually attach it to a message and send it. It might be a good idea to introduce a 5 to 10 second delay between telling Mail (or "do shell script") to send the message and telling the Finder to delete the file.

Posted on
Thu Apr 19, 2012 9:18 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: shellscript an attachment?

Sending attachments via the command line is tricky. I suggest that you use the mutt command-line mail client. You can get it installed by installing MacPorts and using the port command to install it:

Code: Select all
sudo port install mutt


Once you have mutt installed, the "want to do" script should be very close (I think the email address has to be in quotes). I agree with nsheldon though - you may want to move the file deletion part of the script to the top - so it only deletes it just before SecuritySpy saves the new image. Yeah, that means that the file might be sitting there for a while but that shouldn't be a big deal.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Apr 28, 2012 7:20 pm
T-Power offline
User avatar
Posts: 220
Joined: May 10, 2010

Re: shellscript an attachment?

Hello nsheldon, and jay,

Thanks you for the reply!
The previous script posted at the very top works fine but runs into an issue when Mail.app randomly will ask for a password. I never really know when the Mail.app will do this hiccup, but it really hurts on the automation side. For this I would like to avoid the Mail.app if at all possible!

I took your advise and moved the delete part of the script to the top to look like this:
Code: Select all
tell application "Finder"
   activate
   if file "Macintosh HD:Users:bob:Desktop:SecuritySpyimage.jpg" exists then
      delete file "Macintosh HD:Users:bob:Desktop:SecuritySpyimage.jpg"
   end if
   delay 10
end tell

tell application "SecuritySpy"
   capture image camera number 2 as "/Users/bob/Desktop/SecuritySpyimage.jpg"
end tell

set theSubject to "Mailbox Sensor Detection"
set theSender to "cornmeal@gmail.com"
set theAddress to "bobby@aol.com"
set theAttachment to "/Users/bob/Desktop/SecuritySpyimage.jpg"

tell application "Mail"
   set theMessage to make new outgoing message with properties {subject:theSubject, sender:theSender}
   
   tell theMessage
      set visible to false
      make new to recipient at end of to recipients with properties {address:theAddress}
      make new attachment with properties {file name:theAttachment} at after the last paragraph
   end tell
   send theMessage
   delay 5
end tell

tell application "System Events"
   set visible of process "Mail" to false
end tell


Hopefully this will help since the computer does have other apps running.

Installed MacPorts than mutt.
Still have trouble with the shell script. I tried these combinations:
Code: Select all
do shell script "echo |mail -s 'Email Subject' bobby@aol.com -a /Users/bob/Desktop/SecuritySpyimage.jpg"

do shell script "echo |mail -s 'Email Subject' 'bobby@aol.com' -a /Users/bob/Desktop/SecuritySpyimage.jpg"

do shell script "echo |mail -s 'Email Subject' "bobby@aol.com" -a /Users/bob/Desktop/SecuritySpyimage.jpg"


I noticed Matt asking a similar question last year at:
http://www.perceptiveautomation.com/userforum/viewtopic.php?f=4&t=6725&hilit=mail+password
His solution was:
Code: Select all
do shell script "/Users/username/mutt-1.5.20/mutt -s \"Email Subject\" username@gmail.com -a /Users/username/Desktop/variables/filename.png "


I know I am close but still missing something. Any suggestions?

T.I.A.

MacMini 2.3 GHz Intel Core i7 16GB DDR3
Indigo Pro 2022.1 macOS Mojave 10.14.6

Posted on
Sun Apr 29, 2012 10:19 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: shellscript an attachment?

The mutt app won't be installed at that location you specified above if you used macports to install it. Open a terminal and type:

Code: Select all
which mutt


That will show you the full path to the mutt application (it's probably "/opt/local/bin/mutt" but may be different). For instance, this is what I see:

Code: Select all
FatMac:~ jay$ which mutt
/opt/local/bin/mutt


Use that path in your do shell script command:

Code: Select all
do shell script "/opt/local/bin/mutt -s \"Email Subject\" username@gmail.com -a /Users/username/Desktop/variables/filename.png "

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Apr 29, 2012 11:00 am
T-Power offline
User avatar
Posts: 220
Joined: May 10, 2010

Re: shellscript an attachment?

Hello jay,

Thank you for the response.
When I open script editor and input the suggested code:
Code: Select all
do shell script "/opt/local/bin/mutt -s \"Sunday test\" bobby@aol.com -a /Users/bob/Desktop/SecuritySpyimage.jpg "


At the bottom of the script editor window it gives a result of " ".
I dont think the message sends cause I never see it in the inbox.
Do I need to configure the mutt.app for anything?

T.I.A.

MacMini 2.3 GHz Intel Core i7 16GB DDR3
Indigo Pro 2022.1 macOS Mojave 10.14.6

Posted on
Sun Apr 29, 2012 11:33 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: shellscript an attachment?

You still need the echo part:

Code: Select all
do shell script "echo |/opt/local/bin/mutt -s \"Sunday test\" bobby@aol.com -a /Users/bob/Desktop/SecuritySpyimage.jpg "


Also, check your spam folder on AOL - sending an email with an attachment but no body is likely to trip the spam filter.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Apr 29, 2012 12:03 pm
T-Power offline
User avatar
Posts: 220
Joined: May 10, 2010

Re: shellscript an attachment?

jay,

Thanks again.
I looked at the logs and found this:
Code: Select all
Apr 29 13:56:53 bob-mac-g5 postfix/smtp[6065]: connect to mta6.am0.aoldns.net[ip address]: Operation timed out (port 25)
Apr 29 13:56:56 bob-mac-g5 postfix/smtp[6063]: connect to mta5.am0.aoldns.net[ip address]: Connection refused (port 25)


Seems like we are half way there, the the mail gets sent but without that attachment.
The message that is received looks like this:
Code: Select all
bob@aol.com, -a@bob-mac-g5.local, /Users/bob/Desktop/SecuritySpyimage.jpg@bob-mac-g5.local


What am I missing?
T.I.A.

MacMini 2.3 GHz Intel Core i7 16GB DDR3
Indigo Pro 2022.1 macOS Mojave 10.14.6

Posted on
Sun Apr 29, 2012 2:55 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: shellscript an attachment?

What does your completed script look like? I have no clue what that last code section in your email is or what it's from...

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Apr 29, 2012 3:16 pm
T-Power offline
User avatar
Posts: 220
Joined: May 10, 2010

Re: shellscript an attachment?

jay,

Apologies for any confusion.
The script looks like this:
Code: Select all
tell application "Finder"
   activate
   if file "Macintosh HD:Users:bob:Desktop:SecuritySpyimage.jpg" exists then
      delete file "Macintosh HD:Users:bob:Desktop:SecuritySpyimage.jpg"
   end if
   delay 10
end tell

tell application "SecuritySpy"
   capture image camera number 2 as "/Users/bob/Desktop/SecuritySpyimage.jpg"
end tell

do shell script "echo |/opt/local/bin/mutt -s \"Sunday test\" bobby@aol.com -a /Users/bob/Desktop/SecuritySpyimage.jpg "


Sometimes during the testing the above script is a reduced to:
Code: Select all
do shell script "echo |/opt/local/bin/mutt -s \"Sunday test\" bobby@aol.com -a /Users/bob/Desktop/SecuritySpyimage.jpg "


The script is partly working in that it sends the message but without the attachment.
When I go to retrieve the mail I see that the subject part is correct. I also notice that while retrieving the mail instead of the mail saying to:
Code: Select all
bob@aol.com"


Instead is says:
Code: Select all
bob@aol.com, -a@bob-mac-g5.local, /Users/bob/Desktop/SecuritySpyimage.jpg@bob-mac-g5.local


Hope that helps.

MacMini 2.3 GHz Intel Core i7 16GB DDR3
Indigo Pro 2022.1 macOS Mojave 10.14.6

Posted on
Sun Apr 29, 2012 5:25 pm
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: shellscript an attachment?

T-Power wrote:
Code: Select all
Apr 29 13:56:53 bob-mac-g5 postfix/smtp[6065]: connect to mta6.am0.aoldns.net[ip address]: Operation timed out (port 25)
Apr 29 13:56:56 bob-mac-g5 postfix/smtp[6063]: connect to mta5.am0.aoldns.net[ip address]: Connection refused (port 25)

This indicates that the attempts to send the message are being blocked by something (it could be a firewall (less likely), your ISP (more likely), or AOL's mail servers (equally likely)). Most ISPs (such as Comcast) block all communication on port 25 because of the huge amount of email generated by viruses and malware on customer computers. If your ISP is blocking port 25, pretty much the only way you're going to get an email sent out is by using a program that supports the newer SMTP ports 465 and 587. A quick Google search found this site which describes using mutt to send on those ports (quick summary: not a simple task). Even if your ISP isn't blocking port 25, AOL may be blocking, or heavily filtering, all email sent from all dynamically assigned (i.e. dial-up, cable, and DSL) IP addresses. Most email service providers now use spam filtering on their servers that rely on lists of ISP's IP address ranges to preemptively block potential spam coming from end-user machines. The only way to get around this is to send your email messages through your own ISP's email servers. For example, if you're using Comcast, you have to send your email through smpt.comcast.net (which requires an authenticated SSL connection on port 587 in order to send messages). In short, if you can get Apple's Mail application to work more reliably, it'd probably be a lot less hassle.

Posted on
Sun Apr 29, 2012 7:42 pm
T-Power offline
User avatar
Posts: 220
Joined: May 10, 2010

Re: shellscript an attachment?

Hello nsheldon,

Thanks for the reply.

I think I got the port 25 issue resolved via Mail.app cause other mail with attachments come and go.
Outgoing messages are now on ports 465 and 587.

Could this still be a port issue even though the mail (script) gets sent but not the attachment?
I am so close to almost being able to send an attachment!

MacMini 2.3 GHz Intel Core i7 16GB DDR3
Indigo Pro 2022.1 macOS Mojave 10.14.6

Posted on
Sun Apr 29, 2012 8:15 pm
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: shellscript an attachment?

T-Power wrote:
I think I got the port 25 issue resolved via Mail.app cause other mail with attachments come and go.
Outgoing messages are now on ports 465 and 587.

Cool.

Could this still be a port issue even though the mail (script) gets sent but not the attachment?

Probably not. Communication on a particular port is usually an all-or-nothing thing when it's being blocked. So if you're consistently receiving only the text portion of your emails but not the attachments, either the sending application isn't attaching the files correctly, or your email provider is stripping out the attachments for some reason (perhaps they think it's spam or a virus).

I am so close to almost being able to send an attachment!

If Mail.app is working (as you mentioned above), what's left to get working? Is it just that Mail.app isn't sending messages consistently? Perhaps it's that your email service provider isn't delivering them consistently. ;-) You might try having Mail.app send the messages to a free Gmail, Yahoo, or Hotmail account instead. AOL's somewhat well known for being rather aggressive with their spam filtering.

Posted on
Sun Apr 29, 2012 8:36 pm
T-Power offline
User avatar
Posts: 220
Joined: May 10, 2010

Re: shellscript an attachment?

I really wanted to say GOOD-BYE to the Mail.app once and for all since I never know when it will hiccup and ask for a password when I am unvailable. This takes away from the whole automation setup.

Even when using a Yahoo account instead of the AOL I still get the same thing, the message arrives minus the attachment.

Is there something that needs to be preconfigured via mutt?
I think the issue is with the script sending the attachment.
Why else when I go to retrieve the mail the "to address" is:
Code: Select all
bob@aol.com, -a@bob-mac-g5.local, /Users/bob/Desktop/SecuritySpyimage.jpg@bob-mac-g5.local

MacMini 2.3 GHz Intel Core i7 16GB DDR3
Indigo Pro 2022.1 macOS Mojave 10.14.6

Posted on
Sun Apr 29, 2012 11:29 pm
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: shellscript an attachment?

The reason mutt is sending messages with those weird addresses is because the order of arguments you're passing to mutt is incorrect. According to the mutt manual page, the address must be the last argument passed to mutt. Mutt thinks all those things you have coming after the "bob@aol.com" address are addresses as well, because it expects the "To" address/es to be the last thing it sees. Try moving your "To" address to the end of that command and see if that works.

Who is online

Users browsing this forum: No registered users and 15 guests