Author Topic: Mailing without Mail  (Read 2861 times)

0 Members and 1 Guest are viewing this topic.

Offline Jean-Marie Schwartz

  • Sr. Member
  • ****
  • Posts: 436
  • Karma: 6
  • Gender: Male
Mailing without Mail
« on: October 14, 2010, 05:16:47 AM »
Hi!
I'd like to add the possibility to the user of sending a message by mail. The point is I don't think that user uses Mail as mailing app. But I'm pretty sure a message could be sent regardless of the app the user uses (I'm thinking more of a shell script). How could I code that? How to send a mail in shell script?
Any clues?
Jean-Marie Schwartz
May the Schwartz be with you :-)

= Mac Pro Quad-Core Intel 2.8 GHz 10 Go 1066 MHz DDR3 Mac OS 10.8.5 QuarkXPress 9.5.4.1 Adobe CS5 =

Offline Publi-Script

  • Administrator
  • Hero Member
  • *****
  • Posts: 596
  • Karma: 16
  • Gender: Male
  • Automate your Life!
    • Publi-Script.com
Re: Mailing without Mail
« Reply #1 on: October 14, 2010, 09:29:38 AM »
Hi Jean-Marie,

The following works for me, however, I used this so long ago, I am not sure if I had to fiddle with systems' flags:

Quote
001     set TheSubject to "\"Email Test\""
002     set cc_Address to "" --      <-- Fill in optional info
003     set bcc_Address to "" --     <-- Fill in optional info
004     set to_Address to "" --      <-- Fill in required info
005     set theBody to "" --         <-- Fill in required info

006     set TheScript to ""
007     set TheScript to TheScript & "echo \""
008     set TheScript to TheScript & theBody
009     set TheScript to TheScript & "\" | mail -v"
010     set TheScript to TheScript & " -s " & TheSubject
011     if cc_Address is not "" then
012          set TheScript to TheScript & " -c " & cc_Address
013     end if
014     if bcc_Address is not "" then
015          set TheScript to TheScript & " -b " & bcc_Address
016     end if
017     set TheScript to TheScript & " " & to_Address
018     set ScriptResult to do shell script TheScript
Notes:
The line numbers included with this script are there to aid future discussions. In order to use this script, you will have to strip them all from the code.


HTH
Michel Lemieux
Forum Administrator
-------------------------
OS X 10.6.4
Quark 6.5 / 7.5 / 8.1.6
Adobe Design Premium CS / CS2 / CS3 / CS4 / CS5
Script Debugger 4.5

Offline Jean-Marie Schwartz

  • Sr. Member
  • ****
  • Posts: 436
  • Karma: 6
  • Gender: Male
Re: Mailing without Mail
« Reply #2 on: October 15, 2010, 11:13:00 AM »
 :'(

Unfortunately not! I've seen later on yesterday (after my post) that a mail command exists. But it doesn't send anything (sent a message to myself, I hope I did not put my own address in the Spam filter  :o
Jean-Marie Schwartz
May the Schwartz be with you :-)

= Mac Pro Quad-Core Intel 2.8 GHz 10 Go 1066 MHz DDR3 Mac OS 10.8.5 QuarkXPress 9.5.4.1 Adobe CS5 =

Offline Publi-Script

  • Administrator
  • Hero Member
  • *****
  • Posts: 596
  • Karma: 16
  • Gender: Male
  • Automate your Life!
    • Publi-Script.com
Re: Mailing without Mail
« Reply #3 on: October 15, 2010, 12:42:46 PM »
Ok, that is because your mail server is set NOT to relay messages... I will return later with a more robust version invloving TELNET. I just have to remove it from an XCode project I did.

Be assure though that this IS possible.
Michel Lemieux
Forum Administrator
-------------------------
OS X 10.6.4
Quark 6.5 / 7.5 / 8.1.6
Adobe Design Premium CS / CS2 / CS3 / CS4 / CS5
Script Debugger 4.5

Offline Publi-Script

  • Administrator
  • Hero Member
  • *****
  • Posts: 596
  • Karma: 16
  • Gender: Male
  • Automate your Life!
    • Publi-Script.com
Re: Mailing without Mail
« Reply #4 on: October 15, 2010, 01:49:04 PM »
As promised, here are the "longer" version that will use Telnet to communicate with your mail server. This solution is composed of 3 items:
  • The AppleScript itself
  • A Command Line Script
  • A hard copy of the Email Message itself (this will get re-written every time the script is run

The AppleScript:
Quote
001     property FromAddress : {|name|:"Jean-Marie Schwartz", address:"jm@schwartz.com"}
002     property DefaultErrorRecipient : "jm@schwartz.com"

003     property pty_SMPTServer : "" -- Any mail server to which you have access to which you have access to
004     property pty_SMPTPort : "25"
005     property pty_SMPTUsername : "" -- Use ONLY if your server needs authentication
006     property pty_SMPTPassword : "" -- Use ONLY if your server needs authentication

007     property MessagePath : "...:EmailMessage.txt" -- Path to "EmailMessage.txt" file
008     property ScriptPath : "...:sendmailscpt.txt" -- Path to "sendmailscpt.txt" file

009     set the_Recipient to "jm@schwartz.com"
010     set the_Subject to "Test"
011     set the_Message to "This is only a test"


012     tell me to Send_SMTPMail(the_Recipient, the_Subject, the_Message)


013     on Send_SMTPMail(ThisRecipient, MsgSubject, MsgText)
014          my Compose_Message(ThisRecipient, MsgSubject, MsgText)
     
015          set MessagePOSIXPath to quoted form of (POSIX path of (alias MessagePath))
     
016          set ScriptPOSIXPath to quoted form of (POSIX path of (alias ScriptPath))
     
017          try
018               set TheScript to ""
019               set TheScript to TheScript & ScriptPOSIXPath & space
020               set TheScript to TheScript & pty_SMPTServer & space
021               set TheScript to TheScript & pty_SMPTPort & space
022               set TheScript to TheScript & address of FromAddress & space
023               set TheScript to TheScript & ThisRecipient & space
024               set TheScript to TheScript & MessagePOSIXPath
025               set ScriptResult to do shell script TheScript
          
026               set LastResultLine to (my GetTextItem(ScriptResult, return, -1)) as text
027               if LastResultLine starts with "250" then
028                    return "Sent OK!" & return & ScriptResult
029               else
030                    return "Sending ERROR!" & return & ScriptResult
031               end if
032          on error errMsg
033               return "Sending ERROR!" & return & errMsg
034          end try
035     end Send_SMTPMail

036     on Compose_Message(ToAddress, MsgSubject, MsgText)
037          set Msg_Text to ""
038          set Msg_Text to Msg_Text & "From: " & |name| of FromAddress & space & "<" & address of FromAddress & ">" & return
039          set Msg_Text to Msg_Text & "To: " & ToAddress & space & "<" & ToAddress & ">" & return
040          set Msg_Text to Msg_Text & "Subject: " & MsgSubject & return & return
     
041          set Msg_Text to Msg_Text & MsgText & return & return
     
042          set Msg_Text to Msg_Text & |name| of FromAddress
     
043          set aFile to open for access file MessagePath with write permission
044          set eof of aFile to 0
045          try
046               write (my UnstyledText(Msg_Text)) to aFile
047               close access aFile
048               return true
049          on error
050               close access aFile
051               return false
052          end try
053     end Compose_Message

054     on UnstyledText(styledText)
055          try
056               set clipContents to styledText as record
057               copy class ktxt of clipContents to ktxt
058          on error errMsg number errNum
059               set ktxt to styledText as text
060          end try
061          return ktxt
062     end UnstyledText

063     on GetTextItem(ThisString, ThisDelim, ThisItem)
064          -- ThisString -> String to look in
065          -- ThisDelim -> Text element that delimit the string
066          -- ThisItem -> Number of the element to return
067          copy the text item delimiters to OldDelims
068          set the text item delimiters to ThisDelim
069          if class of ThisItem is list then
070               set fromItem to (item 1 of ThisItem) as integer
071               set toitem to (item 2 of ThisItem) as integer
072               set arrItem to (text items fromItem thru toitem of ThisString)
073          else
074               set arrItem to every text item of ThisString
075          end if
076          set the text item delimiters to OldDelims
     
     
077          if class of ThisItem is list then
078               return arrItem as text
079          else
080               if ThisItem is not equal to 0 then
081                    return (item ThisItem of arrItem) as text
082               else
083                    return arrItem -- return every items
084               end if
085          end if
086     end GetTextItem

Notes:
The line numbers included with this script are there to aid future discussions. In order to use this script, you will have to strip them all from the code.


--> Follows on next post...
« Last Edit: October 15, 2010, 01:52:46 PM by Publi-Script »
Michel Lemieux
Forum Administrator
-------------------------
OS X 10.6.4
Quark 6.5 / 7.5 / 8.1.6
Adobe Design Premium CS / CS2 / CS3 / CS4 / CS5
Script Debugger 4.5

Offline Publi-Script

  • Administrator
  • Hero Member
  • *****
  • Posts: 596
  • Karma: 16
  • Gender: Male
  • Automate your Life!
    • Publi-Script.com
Re: Mailing without Mail
« Reply #5 on: October 15, 2010, 01:53:59 PM »
... Continues from last post

The Command Line Script:
Quote
#!/usr/bin/expect
## main

set svrip [lindex $argv 0]
set svrprt [lindex $argv 1]
set frmadd [lindex $argv 2]
set toadd [lindex $argv 3]
set msgfile [lindex $argv 4]

# Check for enough args, display usage if we don't have enough.
if 0==$argc {
   send_user "sendmail.exp: usage: sendmail.exp <Server IP/Name> <Server Port> <From Address> <To Address> <Message_file>\n"
   send_user "\nWhere <Message_file> is a formated email message with headers.\n"
   exit 1
}

# Set the optional timeout value, in seconds.  Default is 30 seconds.
set timeout 30

# See if we can open the input file
if [catch {open $msgfile} fh] {
#   puts "$fh"
   exit 1
} else {
   set fc [read $fh]
#   puts "--> File read"
}

set vLoop 0

#log_file "ExpectLog"

while {$vLoop < 60} {

   send_log -- "--> Connecting to $svrip on port $svrprt\n\n"
   
   spawn telnet $svrip $svrprt
   
   expect "220 *" {
      send_log -- "--> Connected to $svrip on port $svrprt\n\n"
      send "HELO VMX\n"
      expect "250 *" {
         send_log -- "--> Requested mail action okay, completed\n\n"
         send "MAIL FROM: $frmadd\n"
         expect "250 *" {
            send_log -- "--> Requested mail action okay, completed\n\n"
            send "RCPT TO: $toadd\n"
            expect "250 *" {
               send_log -- "--> Requested mail action okay, completed\n\n"
               send "DATA\n"
               expect "354 *" {
                  send_log -- "--> Start mail input; end with <CRLF>.<CRLF>\n\n"
                  send "$fc\n.\n"
                  expect "2?? *" {
                         set vLoop 5
                         exit
                  }
               } "451 *" {
                  send_log -- "--> Requested action aborted: local error in processing\n\n"
                  exp_continue
               } "554 *" {
                  send_log -- "--> Transaction failed\n\n"
                  exp_continue
               } "500 *" {
                  send_log -- "--> Syntax error, command unrecognised\n\n"
                  exit
               } "501 *" {
                  send_log -- "--> Syntax error in parameters or arguments\n\n"
                  exit
               } "503 *" {
                  send_log -- "--> Bad sequence of commands\n\n"
                  exit
               } "421 *" {
                  send_log -- "--> $svrip : Service not available, closing transmission channel received data\n\n"
                  exp_continue
               } "552 *" {
                  send_log -- "--> Requested mail action aborted: exceeded storage allocation\n\n"
                  exit
               } "554 *" {
                  send_log -- "--> Transaction failed\n\n"
                  exp_continue
               } "451 *" {
                  send_log -- "--> Requested action aborted: local error in processing\n\n"
                  exp_continue
               } "452 *" {
                  send_log -- "--> Requested action not taken: insufficient system storage\n\n"
                  exit
               }
            } "251 *" {
               send_log -- "--> User not local; will forward to <forward-path>\n\n"
               exit
            } "550 *" {
               send_log -- "--> Requested action not taken: mailbox unavailable\n\n"
               exit
            } "551 *" {
               send_log -- "--> User not local; please try <forward-path>\n\n"
               exit
            } "552 *" {
               send_log -- "--> Requested mail action aborted: exceeded storage allocation\n\n"
               exit
            } "553 *" {
               send_log -- "--> Requested action not taken: mailbox name not allowed\n\n"
               exit
            } "450 *" {
               send_log -- "--> Requested mail action not taken: mailbox unavailable\n\n"
               exp_continue
            } "451 *" {
               send_log -- "--> Requested action aborted: local error in processing\n\n"
               exp_continue
            } "452 *" {
               send_log -- "--> Requested action not taken: insufficient system storage\n\n"
               exit
            } "500 *" {
               send_log -- "--> Syntax error, command unrecognised\n\n"
               exit
            } "501 *" {
               send_log -- "--> Syntax error in parameters or arguments\n\n"
               exit
            } "503 *" {
               send_log -- "--> Bad sequence of commands\n\n"
               exit
            } "521 *" {
               send_log -- "--> $svrip : does not accept mail [rfc1846]\n\n"
               exit
            } "421 *" {
               send_log -- "--> $svrip : Service not available, closing transmission channel\n\n"
               exp_continue
            }
         } "552 *" {
            send_log -- "--> Requested mail action aborted: exceeded storage allocation\n\n"
            exit
         } "451 *" {
            send_log -- "--> Requested action aborted: local error in processing\n\n"
            exit
         } "452 *" {
            send_log -- "--> Requested action not taken: insufficient system storage\n\n"
            exit
         } "500 *" {
            send_log -- "--> Syntax error, command unrecognised\n\n"
            exit
         } "501 *" {
            send_log -- "--> Syntax error in parameters or arguments\n\n"
            exit
         } "421 *" {
            send_log -- "--> $svrip : Service not available, closing transmission channel\n\n"
            exp_continue
         }
      
      } "50? *" {
         send_log -- "--> Syntax error, command unrecognised\n\n"
         send "EHLO VMX\n"
         expect "250 *" {
            send_log -- "--> Requested mail action okay, completed\n\n"
            send "MAIL FROM: $frmadd\n"
            expect "250 *" {
               send_log -- "--> Requested mail action okay, completed\n\n"
               send "RCPT TO: $toadd\n"
               expect "250 *" {
                  send_log -- "--> Requested mail action okay, completed\n\n"
                  send "DATA\n"
                  expect "354 *" {
                     send_log -- "--> Start mail input; end with <CRLF>.<CRLF>\n\n"
                     send "$fc\n.\n"
                     expect "2?? *" {
                         set vLoop 5
                         exit
                     }
                  } "451 *" {
                     send_log -- "--> Requested action aborted: local error in processing\n\n"
                     exp_continue
                  } "554 *" {
                     send_log -- "--> Transaction failed\n\n"
                     exp_continue
                  } "500 *" {
                     send_log -- "--> Syntax error, command unrecognised\n\n"
                     exit
                  } "501 *" {
                     send_log -- "--> Syntax error in parameters or arguments\n\n"
                     exit
                  } "503 *" {
                     send_log -- "--> Bad sequence of commands\n\n"
                     exit
                  } "421 *" {
                     send_log -- "--> $svrip : Service not available, closing transmission channel received data\n\n"
                     exp_continue
                  } "552 *" {
                     send_log -- "--> Requested mail action aborted: exceeded storage allocation\n\n"
                     exit
                  } "554 *" {
                     send_log -- "--> Transaction failed\n\n"
                     exp_continue
                  } "451 *" {
                     send_log -- "--> Requested action aborted: local error in processing\n\n"
                     exp_continue
                  } "452 *" {
                     send_log -- "--> Requested action not taken: insufficient system storage\n\n"
                     exit
                  }
               } "251 *" {
                  send_log -- "--> User not local; will forward to <forward-path>\n\n"
                  exit
               } "550 *" {
                  send_log -- "--> Requested action not taken: mailbox unavailable\n\n"
                  exit
               } "551 *" {
                  send_log -- "--> User not local; please try <forward-path>\n\n"
                  exit
               } "552 *" {
                  send_log -- "--> Requested mail action aborted: exceeded storage allocation\n\n"
                  exit
               } "553 *" {
                  send_log -- "--> Requested action not taken: mailbox name not allowed\n\n"
                  exit
               } "450 *" {
                  send_log -- "--> Requested mail action not taken: mailbox unavailable\n\n"
                  exp_continue
               } "451 *" {
                  send_log -- "--> Requested action aborted: local error in processing\n\n"
                  exp_continue
               } "452 *" {
                  send_log -- "--> Requested action not taken: insufficient system storage\n\n"
                  exit
               } "500 *" {
                  send_log -- "--> Syntax error, command unrecognised\n\n"
                  exit
               } "501 *" {
                  send_log -- "--> Syntax error in parameters or arguments\n\n"
                  exit
               } "503 *" {
                  send_log -- "--> Bad sequence of commands\n\n"
                  exit
               } "521 *" {
                  send_log -- "--> $svrip : does not accept mail [rfc1846]\n\n"
                  exit
               } "421 *" {
                  send_log -- "--> $svrip : Service not available, closing transmission channel\n\n"
                  exp_continue
               }
            } "552 *" {
               send_log -- "--> Requested mail action aborted: exceeded storage allocation\n\n"
               exit
            } "451 *" {
               send_log -- "--> Requested action aborted: local error in processing\n\n"
               exit
            } "452 *" {
               send_log -- "--> Requested action not taken: insufficient system storage\n\n"
               exit
            } "500 *" {
               send_log -- "--> Syntax error, command unrecognised\n\n"
               exit
            } "501 *" {
               send_log -- "--> Syntax error in parameters or arguments\n\n"
               exit
            } "421 *" {
               send_log -- "--> $svrip : Service not available, closing transmission channel\n\n"
               exp_continue
            }
         
         } "500 *" {
            send_log -- "--> Syntax error, command unrecognised\n\n"
            exit
         } "501 *" {
            send_log -- "--> Syntax error in parameters or arguments\n\n"
            exit
         } "504 *" {
            send_log -- "--> Command parameter not implemented\n\n"
            exit
         } "521 *" {
            send_log -- "--> $svrip : does not accept mail [rfc1846]\n\n"
            exit
         } "421 *" {
            send_log -- "--> $svrip : Service not available, closing transmission channel\n\n"
            exp_continue
         }
      
      } "521 *" {
         send_log -- "--> $svrip : does not accept mail [rfc1846]\n\n"
         exit
      } "421 *" {
         send_log -- "--> $svrip : Service not available, closing transmission channel\n\n"
         exp_continue
      }
   } "421 *" {
      send_log -- "--> Server unavailable. Will retry up to 5 times at 60 sec interval\n\n"
      exp_continue
   } "refused" {
      send_log -- "--> Conection refused by the server\n\n"
      exit
   } "closed" {
      send_log -- "--> Conection refused by the server. Will retry up to 5 times at 60 secinterval\n\n"
      exp_continue
   } timeout {
      send_log -- "--> Conection timeout. Will retry up to 5 times at 60 secinterval\n\n"
      exp_continue
   }
   send "Quit\n"
   
   # Something went wrong, let's wait 5 seconds and try again
   set vLoop [expr {$vLoop + 1}]
   after [expr {1000 * 5}]
}
   
close $fh

exit

## DONE

and
The Email Message:
Quote
From: Jean-Marie Schwartz <jm@schwartz.com>
To: jm@schwartz.com <jm@schwartz.com>
Subject: Test

This is only a test

Jean-Marie Schwartz

Copy the content of both Command Line Script and Email Message to separate text file and update their path properties at the top of the AppleScript. Modify other properties as needed

Enjoy!
Michel Lemieux
Forum Administrator
-------------------------
OS X 10.6.4
Quark 6.5 / 7.5 / 8.1.6
Adobe Design Premium CS / CS2 / CS3 / CS4 / CS5
Script Debugger 4.5

Offline Jean-Marie Schwartz

  • Sr. Member
  • ****
  • Posts: 436
  • Karma: 6
  • Gender: Male
Re: Mailing without Mail
« Reply #6 on: October 18, 2010, 11:17:26 AM »
Er! Who said it's easy to send an e-mail?  :)
OK, thank you so much, Michel. I shall try it out later this week and let you know if it works for me.
Jean-Marie Schwartz
May the Schwartz be with you :-)

= Mac Pro Quad-Core Intel 2.8 GHz 10 Go 1066 MHz DDR3 Mac OS 10.8.5 QuarkXPress 9.5.4.1 Adobe CS5 =

Offline Jean-Marie Schwartz

  • Sr. Member
  • ****
  • Posts: 436
  • Karma: 6
  • Gender: Male
Re: Mailing without Mail
« Reply #7 on: April 19, 2011, 09:21:31 AM »
Well! I've had a bit of time the last days so I went back to an old thing I'd like to script.
Sorry Michel but your post was far beyond my skills so I gave up.
I found yesterday a perl script working like a charm from Terminal. So I thought a do shell script in an AppleScript would simply do what I was expecting. BUT it errors: No such file or directory!

How could I have it work from AS? Any thought?

P.S.: sendEmail is the perl command i'm talking about and is downloadable from Caspian.
The command in Terminal is: sendEmail -f "my@ddress.be" -t "sb@ddress.be" -u "Test" -m "Test. Blablabla." -s "smtp.server.net:PortNumber" -xu "my_user_name" -xp "my_password" -o tls=no
My transcription to AS: do shell script ("/usr/opt/local/bin/sendEmail.pl -f \"my@ddress.be\" -t \"sb@ddress.be\" -u \"Test\" -m \"Test. Blablabla.\" -s \"smtp.server.net:PortNumber\" -xu \"my_user_name\" -xp \"my_password\" -o tls=no")
Jean-Marie Schwartz
May the Schwartz be with you :-)

= Mac Pro Quad-Core Intel 2.8 GHz 10 Go 1066 MHz DDR3 Mac OS 10.8.5 QuarkXPress 9.5.4.1 Adobe CS5 =

Offline cdms

  • Full Member
  • ***
  • Posts: 111
  • Karma: 6
  • Gender: Male
Re: Mailing without Mail
« Reply #8 on: April 19, 2011, 10:31:39 AM »
A few thoughts,

I would expect to see the command called exactly the same in your do shell script command as from the command line. You seem to have ".pl" on the end of the path.

Does sendEmail work in Terminal regardless of which directory you are currently in? If so you should just be able to use do shell script "sendEmail -f ........"

It is a bit difficult to see spacing where you have escaped your quotes. Have you considered

set fromAddress to "my@address.be"
set toAddress to "sb@address.be
etc...
       then
do shell script "sendEmail -f " & quoted form of fromAddress & " -t " & quoted form of toAddress ....

personal preference there I guess.

If sendEmail has a help option (-h) then a quick check script of
return do shell script "sendEmail -h"
should let you know if you have access to run from within the applescript.


Offline Jean-Marie Schwartz

  • Sr. Member
  • ****
  • Posts: 436
  • Karma: 6
  • Gender: Male
Re: Mailing without Mail
« Reply #9 on: April 20, 2011, 02:22:27 AM »
Thank you for your input.
I got the answer on another forum. Since it's a sh error, I had to simply specify the command is in perl.

Code: [Select]
do shell script "/usr/bin/env perl  '/path_to_sendEmail '"

It works a treat! I'm happy!  :)
Jean-Marie Schwartz
May the Schwartz be with you :-)

= Mac Pro Quad-Core Intel 2.8 GHz 10 Go 1066 MHz DDR3 Mac OS 10.8.5 QuarkXPress 9.5.4.1 Adobe CS5 =