Use Applescript to send mail without mail.app - email

I'm new to Applescript and working on an app that runs in the background, and I want it to send email updates every so often WITHOUT mail.app.
I've tried googling this numerous times, and while this is the closest I've found, I don't know how to use a Python script. This app needs to work no matter what Mac OSX it is installed in, including Computers without Python. Is that possible?

I have often wanted to do the same thing and struggled myself. I ultimately landed on a python option. Python is supported back to OS X 10.5 and comes preinstalled on many of the newer versions of OS X. Is there some reason you don't feel you can use it? Are you supporting OS 10.4.x or older machines?
If they python option is still a possible option, I've included an example of how to use it here.
property PyMail : "/Users/TBD/Documents/Sample_Python_Email/PyMail.py" -- PATH TO PYTHON MAIL SCRIPT
on run
set emailTo to "youraddress#somedomain.com" -- MULTIPLE ADDRESS SHOULD BE A COMMA DELIMITED STRING LIKE THIS -- "address1#gmail.com, address2#hotmail.com"
set emailFrom to "Your Name <your.name#somedomain.com>"
set subject to "Demo Email"
set message to "Hi user,
I hope things are going well"
set pathToAttchment to "/Users/TBD/Desktop/prof.jpg" -- POSIX PATH TO FILE, LEAVE AS EMPTY STRING FOR NO ATTACHMENT
set username to "smtpusername" -- MAY OR MAY NOT BE REQUIRED IN YOUR CASE
sendPyMail(emailTo, emailFrom, subject, message, pathToAttchment, username)
end run
on sendPyMail(emailTo, emailFrom, subject, message, attachment, username)
try
do shell script "python " & quoted form of PyMail & " " & quoted form of emailTo & " " & quoted form of emailFrom & " " & quoted form of subject & " " & quoted form of message & " " & quoted form of attachment & " " & quoted form of username
return true
on error
return false
end try
end sendPyMail
Here is the python script (just copy and past it into a text editor and save as PyMail.py. You'll need to change the smtp server and possibly add the password that goes with the username you're supplying...
import sys
SMTP_TO = sys.argv[1]
SMTP_TO = SMTP_TO.split(',')
SMTP_FROM = sys.argv[2]
SUBJECT = sys.argv[3]
MESSAGE = sys.argv[4]
TEXT_FILENAME = sys.argv[5]
SMTP_USERNAME = sys.argv[6]
SMTP_SERVER = 'smtp.domainx.com'
SMTP_PORT = 25
SMTP_PASSWORD = ''
# now construct the message
import smtplib, email
from email import encoders
import os
msg = email.MIMEMultipart.MIMEMultipart()
body = email.MIMEText.MIMEText(MESSAGE)
if TEXT_FILENAME != "":
attachment = email.MIMEBase.MIMEBase('text', 'plain')
attachment.set_payload(open(TEXT_FILENAME).read())
attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(TEXT_FILENAME))
encoders.encode_base64(attachment)
msg.attach(body)
if TEXT_FILENAME != "":
msg.attach(attachment)
msg.add_header('From', SMTP_FROM)
msg.add_header('To', ';'.join(SMTP_TO))
msg.add_header('Subject', SUBJECT)
mailer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
mailer.sendmail(SMTP_FROM, SMTP_TO, msg.as_string())
mailer.close()

Run this in Terminal
echo "this is the body" | mail -s "this is the subject" "to#address"
Note: This assumes you have a locally installed MTA
Your AppleScript would be:
do shell script "echo 'this is the body' | mail -s 'this is the subject' 'to#address'"

Knowing nothing about python, I used TextWrangler and cut and paste the two scripts above.
The second one crashes at line 1, returning a syntax error, as follows:
line 1
property PyMail : "/Users/.../PyMail.py" -- PATH TO PYTHON MAIL SCRIPT
^
SyntaxError: invalid syntax
The carrot pointer is under the 'l' in 'PyMail'.
I commented out line 1, and it returned the same message for line 3.

Related

Some questions about coding ActiveX Script/VBScript in SQL Server Agent Jobs/DTS packages

I use SQL Server Agent Jobs/DTS packages, coded in ActiveX Script/VBScript.
It works fine.
But there are some issues I would need help at the moment:
First: Is there a possibility to send an html email out of the ActiveX Script code in the DTS Step?
My company doesn't want to buy a separate commercial DLL for sending smtp email .. like JMail for example.
I know there are many such DLLs I could buy which can be used to send an email using VB or other languages.
But we don't have the money for such external components.
Could I use SQL Server Database Mail?
... but it is necessary to send the mails in html ...
Is there a possibility to create a new *.txt, *.csv or *.xlsx file from the ActiveX Script/VBScript code in the DTS Step?
I would like to copy the html email body code (a string which I built in the ActiveX Script code) into these files and attach them to the email I send out of the script code. So the user/recipient gets the html content embedded in the email body and separate in files too.
Thanks in advance for your help.
Tommy
This code works. PLUS it displays any errors which tell you why it didn't work.
Set emailObj = CreateObject("CDO.Message")
emailObj.From = "dc#gmail.com"
emailObj.To = "dc#gmail.com"
emailObj.Subject = "Test CDO"
emailObj.TextBody = "Test CDO"
emailObj.AddAttachment "C:/Users/User/Desktop/err.fff"
Set emailConfig = emailObj.Configuration
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "dc"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Ss"
emailConfig.Fields.Update
On Error Resume Next
emailObj.Send
If err.number = 0 then
Msgbox "Done"
Else
Msgbox err.number & " " & err.description
err.clear
End If
Also your account at www.gmail.com needs to be set to allow SMTP access (if using gmail as many are who do this).
The configuration info comes from Outlook Express (last in WinXP, renamed to Windows Mail in Vista, and dropped from Win7 and later). This shows default configuration on your computer. You may need to install Windows Live Mail. You also have to turn on the SMTP service in Windows Features.
Set emailConfig = emailObj.Configuration
On Error Resume Next
For Each fld in emailConfig.Fields
Text = Text & vbcrlf & fld.name & " = " & fld
If err.number <> 0 then
Text = Text & vbcrlf & fld.name & " = Error - probably trying to read password - not allowed"
err.clear
End If
Next
Msgbox Replace(Text, "http://schemas.microsoft.com", "")
Also CDO for Windows 2000 is not always included in all editions/versions of windows. See http://support.microsoft.com/en-au/kb/171440.
.

batch script: newLine doesnt not appended to email but works in echo testing

So I'm new to batch scripting but I need to do something simple with it to finish my project. I am trying to send an automated email every hour (already done) to several users with a message and the attached report. I have the code for that mostly done but it looks sloppy and I haven't been able to fix it. Here is my code below
echooff
cls
rem This creates a new email message with an attachment for testing purposes.
rem Setting new line character
setlocal EnableDelayedExpansion
set NL=^
rem TWO empty lines are required
cd C:\Program Files (x86)\IBM\Lotus\Notes
notes mailto:username#whereyouplease.com?subject= My Test Email Subject^
&body= cmd test email: !NL!!NL!^
&attach=C:\Users\F400318\Desktop\testDB2.xlsx
OUTPUT:
email body: cmd test email: [attachment here]
WANTED OUTPUT:
email body: cmd test email:
[attachment]
To create a variable representing a new line character, use the following:
set "NL=& echo."
Then a command like:
echo The first line%NL%The second line%NL%The last one!
would output:
The first line
The second line
The last one!
I am not sure if this will solve all the problems you are having with your script. I don't quite understand what your script is doing, particularly the last three lines. For example, the line
notes mailto:username#whereyouplease.com?subject= My Test Email Subject^
will throw an error because notes is not a valid command prompt command. For general tips on getting started writing batch scripts, I have found http://ss64.com/nt/ to be very helpful.
Hope I could help!

Inserting a table into Outlook email using VBScript

I have a table in a Microsoft Word document that I want to insert into the middle of a message in an Outlook email. When I manually copy the table into an email draft, it preserves the formatting, but when I don't, the formatting changes into a list-style. For example, this is what the table would look like:
9898 Apple color
1394 Banana blue
with borders, but in the email that gets sent, it shows up as
9898
Apple
Color
1394
Banana
Blue
I had to add in extra lines above because Stack Overflow wasn't showing it as each element on an separate line.
Within my code, I've taken the table and concatenated it within the string that forms the message of the email. I think this may be the problem, but I'm not sure how to change this to separately include the table. Here is my code:
message = "Dear " & owner & ", " & vbCrLf & vbCrLf
message = message & "Here is the data we have: " & vbCrLf
message = message & vbCrLf & vbCrLf & textToTable(fileName, file)
message = message & "Can you please email us back with the updated data?"
message = message & vbCrLf & vbCrLf & "Thank you."
Set MyItem = ol.CreateItem(0)
With MyItem
.To = me
.CC = ""
.BCC = ""
.Subject = "Table"
.BodyFormat = 3
.Body = message
.Send
End With
I used a lot of the code here in my textToTable function, which seems to work as intended. The only problem is getting Outlook to preserve formatting of the table; does anyone have any suggestions as to how to do this? Thank you so much!
You are setting the plain text Body property. Create an HTML table and assign the HTMLBody property to a property formatted HTML string.

mailto: with multiple addresses and real names

I have backend admin tool that manages a number of groups of people working in different sections. From time to time I need to email all the people in one group, so I created a button in my admin tool which does a simple mailto: for all the users in that section. For example:
Mail All
And this works fine. However, I wanted to add their real names to the mailto link so when I'm sending the mail I can quickly see who's in the group. So I tried formatting the link like this:
Mail All
But that seemed to only pick up the first email address and list the 'real name' as one long name with commas.
Searching the web, documentation is scant on multiple addresses with real names (only found info when sending one). So wondering whether this is (a) not possible, (b) possible, but I've got the syntax wrong or c) only possible if I use a workaround like copying all the email address data onto the clipboard and paste it into the mail.
Any email gurus out there?
I had the same question. I fooled around and got it to work using UTF-8 encoding.
"First Last" <firstlastname#example.com>
becomes
Send Email
I was also able to add a bcc field with multiple addresses by following the example above and separated them with commas.
This launched in both Outlook and in Gmail Compose by replacing "mailto:" with "https://mail.google.com/mail/?view=cm&fs=1&tf=1&to="
I say "not possible", at least the way I interpret RFC 6068:
The syntax of a 'mailto' URI is described using the ABNF of [STD68],
non-terminal definitions from [RFC5322] (dot-atom-text, quoted-
string), and non-terminal definitions from [STD66] (unreserved, pct-
encoded):
mailtoURI = "mailto:" [ to ] [ hfields ]
**to = addr-spec *("," addr-spec )
hfields = "?" hfield *( "&" hfield )
hfield = hfname "=" hfvalue
hfname = *qchar
hfvalue = *qchar
**addr-spec = local-part "#" domain
**local-part = dot-atom-text / quoted-string
domain = dot-atom-text / "[" *dtext-no-obs "]"
dtext-no-obs = %d33-90 / ; Printable US-ASCII
%d94-126 ; characters not including
; "[", "]", or "\"
qchar = unreserved / pct-encoded / some-delims
some-delims = "!" / "$" / "'" / "(" / ")" / "*"
/ "+" / "," / ";" / ":" / "#"
(I've marked the interesting rules with **)
Specifically:
<addr-spec> is a mail address as specified in [RFC5322], but excluding
<comment> from [RFC5322].
The address format you are trying to use is called name-addr in RFC5322. addr-spec is just the name#domain part.

batch file programming to get email notification with text file data as a body of e mail

I want a batch file program to get email.
For example I have a text file main.txt with some data
I want this to my mail id. Can you please help me in this programming.
Thanks in advance.
If you've got an email server you can send the emails to, I'd first recommend Blat as mentioned by PA's comment.
If you're running Microsoft Outlook email client, you can drive that with a VBScript script - not strictly a batch file but VBScript is generally part of Windows. Of course you can use a batch file to call the vbscript file with the right parameters.
(I've used this technique to schedule things in Outlook - schedule sending an email with a particular subject at a particular time.)
'SendMail.vbs
option explicit
' Script for sending mails to myself, with given subject and optionally file contents for body
' Note this only works with particular Schedule service settings, i.e.,
' it has to log on as me and have access to the Desktop
dim fso, f, oMailItem, oOlApp
' Create the mail
Set oOlApp = CreateObject("Outlook.Application")
Set oMailItem = oOlApp.CreateItem(0) '0 = olMailItem
oMailItem.Subject = WScript.Arguments(0)
oMailItem.Recipients.Add ("receiver.name#somemailserver.com")
if WScript.Arguments.Count > 1 then
Set fso = CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile(WScript.Arguments(1), 1 )
oMailItem.Body = f.ReadAll
f.Close
end if
oMailItem.Send
set f = nothing
set oMailItem = nothing
set oOlApp = nothing
Call it with a command like
sendmail.vbs My_Subject_Line contents_file.txt