-3

I need a tiny Windows script to send a 1 line email to Gmail accounts. I have tried many utilities that claim to do this such as BLAT, but none work. The script will be executed inside a batch file if certain conditions are met. Script can be in Perl, Python, VBScript, Java, it does not matter as long as it executes from a batch file. Please only answer if you have tried your solution by sending an email to a Gmail account from either a Gmail, Hotmail or Yahoo email account. The account I am using by default is Gmail, so I am sending from a Gmail account to a Gmail account.

Helen
  • 87,344
  • 17
  • 243
  • 314
Antone
  • 75
  • 1
  • 2
  • 8
  • 4
    You need to make sure there is a title, and something in the body. Otherwise most email providers will just ignore it. – Brad Gilbert Jul 18 '09 at 04:19
  • Please re-read the question, I need a full script to plug in to batch file, not little tid bits with no direction. Thanks, Antone – Antone Jul 18 '09 at 04:24
  • 3
    Given that it's not easy to solve a problem described incompletely in a paragraph of text, and that it's equally difficult to judge a questioners level of expertise, I find that Brad Gilberts comment was perfectly relevant and potentially helpful. – Inshallah Jul 18 '09 at 04:45
  • 9
    Boy, it sure is fun when other people do your job for you, isn't it? – friedo Jul 18 '09 at 04:46
  • 3
    -1 At least show some effort. Where are your attempts with the so called "many utilities" you used? Show us the code. – Fake Code Monkey Rashid Jul 18 '09 at 06:44
  • Thanks you for your hobbist opion. In my professional opinion. My quetion is clear to anyone that has knowledge of script languages. As for myself I am BIOS engineer which for a hobbist means I program in machine language which is light years away from a script language. So, yea I would rather not reinvent the wheel as I know the script exists. Once acquired I would customize the script to my needs. – Antone Jul 18 '09 at 10:50
  • Fake code monkey - there is no code. That is why I am asking for the script. the file will simply be called from a batch file using windows if a condition is met. – Antone Jul 18 '09 at 11:06
  • Firedo - if scripting is not your forte' then don't comment. I need answers not tired old technical support call center jokes. – Antone Jul 18 '09 at 11:08
  • This question can be closed now. Thank you everyone who attempted to help me. In the end Helen went the extra mile for me and resolved the question. Helen spent at least 3 hours of trying to figure out the solution. – Antone Jul 19 '09 at 14:22

5 Answers5

10

Blat lets you send e-mails directly from batch files:

blat.exe - -f from@example.com -to to@gmail.com -s Subject -body "Text body" ^
  -server smtp.example.com:25 -u username -pw password

But it seems that Blat doesn't support SSL, so to make it work with the Gmail you need an additional tool called Stunnel (see here and here).

Anyway, you should be able to send an e-mail via GMail from VBScript using the Collaboration Data Objects (CDO) COM API:

Const schema   = "http://schemas.microsoft.com/cdo/configuration/"
Const cdoBasic = 1
Const cdoSendUsingPort = 2
Dim oMsg, oConf

' E-mail properties
Set oMsg      = CreateObject("CDO.Message")
oMsg.From     = "from@gmail.com"  ' or "Sender Name <from@gmail.com>"
oMsg.To       = "to@gmail.com"    ' or "Recipient Name <to@gmail.com>"
oMsg.Subject  = "Subject"
oMsg.TextBody = "Text body"

' GMail SMTP server configuration and authentication info
Set oConf = oMsg.Configuration
oConf.Fields(schema & "smtpserver")       = "smtp.gmail.com"
oConf.Fields(schema & "smtpserverport")   = 465
oConf.Fields(schema & "sendusing")        = cdoSendUsingPort
oConf.Fields(schema & "smtpauthenticate") = cdoBasic
oConf.Fields(schema & "smtpusessl")       = True
oConf.Fields(schema & "sendusername")     = "from@gmail.com"
oConf.Fields(schema & "sendpassword")     = "sender_password"
oConf.Fields.Update

oMsg.Send

Edit: Added the lacking sendusing parameter so it should work fine now.

See here for more CDO examples.

Helen
  • 87,344
  • 17
  • 243
  • 314
  • i tried BLAT- did not work and sendmail and many others. Most seem to be using old code before - TLS. By the way I believe gmail does not work on 25 port. They use 465. – Antone Jul 18 '09 at 11:16
  • It would help if you expanded your question and told us which tools and/or code you tried and how exactly they "did not work" (what error messages you got etc), provide more information on your environment and so on. Maybe the problem is related to your environment/STMP server configuration and can be solded by adding additional settings to the code. But without knowing the details, it's hard to come up with anything else. – Helen Jul 18 '09 at 12:07
  • @Antone: Seems that Blat and Sendmail need an extra tool called *Stunnel* in order to work with Gmail (see the updated answer). I've also modified the VBScript example to use the Gmail SMTP server. Does it work for you now? – Helen Jul 18 '09 at 14:50
  • This is my third attempt to comment to you Helen. I seem unable to save my comments. Let's see if this works. – Antone Jul 18 '09 at 15:00
  • I am trying now your code. Seems I am able to save comments again. I logged off and back on earlier and that did not work either. Thank you for your help Helen! – Antone Jul 18 '09 at 15:03
  • Is this the complete code? It is not working for me. I will created a new email address in gmail and paste everthing to you. Incase I am not setting up parameters up correctly. Thanks! – Antone Jul 18 '09 at 15:10
  • c:\Python26\Lib\site-packages\libgmail>python setup.py Traceback (most recent call last): File "setup.py", line 7, in import libgmail File "C:\Users\Antone\Desktop\libgmail-0.1.11\libgmail.py", line 36, in import mechanize as ClientCookie ImportError: No module named mechanize – Antone Jul 18 '09 at 16:02
  • @Antone: Yes, it's the complete code. What do you mean by "not working"? Does a runtime error occurs? If so, what does the error say? – Helen Jul 18 '09 at 16:02
  • Helen, I try to past the code to you but i can not. So look below and you will see the email addres I created. If you could use this and test it for me that would be great! from: deltesting@gmail.com to: deltesting@gmail.com password test1234 – Antone Jul 18 '09 at 16:08
  • error is "send configuration is invalid" this is what i am using - i am sending and receving from the same gmail account for testing. I can not past all the code in because on limit of characters. I will post the code here as the link below. Give me 1 minute. Then you can comment on what is wrong. thanks!!! www.cardshock.com/helen.txt – Antone Jul 18 '09 at 16:17
  • the code is there now. i can also use another gmail address to send from if that is an issue. – Antone Jul 18 '09 at 16:19
  • @Antone: It works just fine for me (check the inbox). How do you actually launch the script? Does it work if you run the script's .VBS file itself (by double-clicking on it or using the `wscript filename.vbs` command)? Also, the error you posted above is Python's, how is it related to the present case? – Helen Jul 18 '09 at 16:37
  • i launch the script from the windows desktop by clicking the icon – Antone Jul 18 '09 at 16:48
  • @helen - ok so i see your email, did you send that with the code I posted on the web? – Antone Jul 18 '09 at 16:53
  • I am using the exact code I pasted on to the website I gave you and it is not working. I keep receiving the same error. My filename is x.vbs - i click it and then a messagebox comes up and says -"Error msg - line 20 char 1: Code: 80040220 source CDO.Message1 – Antone Jul 18 '09 at 17:02
  • @helen - here is line 19 char 1 "oMsg.Send" – Antone Jul 18 '09 at 17:07
  • @helen - can you paste the exact code you used to make it work? Thanks! – Antone Jul 18 '09 at 17:08
  • yes - well very busy, i am doctor now not programmer anymore! – Antone Jul 18 '09 at 17:08
  • @Helen thanks for your valuable post,really it's working.i need your help i want IF oMsg.Send THEN response.write("success") else response.write("Failed") END IF but your above syntax not work for if condition .will u plz help me to set the if condition on oMsg.Send – shamim Feb 16 '12 at 17:21
  • 1
    @shamim: Your syntax doesn't work because `Send` doesn't return a value. Please ask your question as a [new question](http://stackoverflow.com/questions/ask) instead - this way you'll get better response that buried somewhere in another question's comments. – Helen Feb 17 '12 at 11:42
  • @Helen thanks for reply,After execute oMsg.Send command , how to know email send is successful or fail .Thanks for reply again. – shamim Feb 17 '12 at 12:41
  • @shamim: Sorry, I don't know. You should really ask this as a [new question](http://stackoverflow.com/questions/ask) instead of posting questions as comments. Check out the FAQ - [How do I ask?](http://stackoverflow.com/faq#howtoask) – Helen Mar 07 '12 at 08:09
5

Have a look at this script on perlmonks which details IMAP access on a GMail account. The post covers everything you need to login into a GMail account through Perl.

Alternatively you could try the Mail::Webmail::Gmail module in CPAN. From the looks of it the module lets you skip most of the intricate details concerning connecting and authenticating with the mail server leaving you with something as simple as -

my $gmail = Mail::Webmail::Gmail->new( username => 'username', password => 'password', );
$gmail->send_message( to => 'user@domain.com', subject => 'Test Message', msgbody => 'This is a test.' );

There's also Email::Send::Gmail in case you need to 'only' send emails from a Gmail account.

aks
  • 24,359
  • 3
  • 32
  • 35
  • Thanks- if the Python code does not work out, but I think it will I will check this next. – Antone Jul 18 '09 at 11:54
  • Thank you muteW for the help, I will still get your ideas and certainly plan on looking into Perl or Python to replace the VBScript I have now for the module of the application. Thanks again! Antone – Antone Jul 19 '09 at 14:25
3
#!c:/Python/python.exe -u
import libgmail
ga = libgmail.GmailAccount("username@gmail.com", "password")
ga.login()
msg=libgmail.GmailComposedMessage("friend@gmail.com", "SubjectHere", "BodyHere")
ga.sendMessage(msg)

That should run on Windows using python. Make sure you change the shebang at the top to point to your python installation. Other than that, just enter your name and password, along with the email you want to send.

Dan Lorenc
  • 5,376
  • 1
  • 23
  • 34
  • 1
    This will send an email *from* a Gmail account, to another Gmail or any other address... – David Z Jul 18 '09 at 04:46
  • 1
    ...and I guess the point of that comment would be, does the OP have a Gmail account available to send from? If not (or if he/she prefers not to use it), an SMTP or Sendmail API solution would be called for. – David Z Jul 18 '09 at 04:48
  • 2
    Well, the OP's login name is unknown (google), which led me to believe he/she had a gmail account... – Dan Lorenc Jul 18 '09 at 04:51
  • Thanks David, the account is gmail- correct. This may be the ticket! I will try and let you know. Never used Perl, but I hear it is very robust and the programmers of Perl are very loyal! Thanks Again! – Antone Jul 18 '09 at 11:38
  • Thank you Dan for the help, I will still get your ideas and certainly plan on looking into Perl or Python to replace the VBScript I have now for the module of the application. Thanks again! Antone – Antone Jul 19 '09 at 14:25
3

If you need just to send to GMail using some SMTP, use MIME::Lite Perl module in SMTP mode - I use it to send notifications to my GMail account.

Alexandr Ciornii
  • 7,346
  • 1
  • 25
  • 29
  • Well - seems everyone has a solution without trying it out. I was hoping Python would do the trick, but it needs mechanize module as well and I can not find it. I guess I will download Perl next and try your solution now. I am amazed at all the experience here and it has now been 6 hours trying to find a solution to a simple script that sends email from a batch file. Wow! Thanks! – Antone Jul 18 '09 at 16:45
  • The closes anyone came it seems was a 24 year old girl from Russia. She was very helpful, but I think I have error in my script then she disappeared. So here I go with Perl! – Antone Jul 18 '09 at 16:47
  • Thank you Alexandr for the help, I will still get your ideas and certainly plan on looking into Perl or Python to replace the VBScript I have now for the module of the application. Thanks again! Antone – Antone Jul 19 '09 at 14:24
1

In Linux (I'm not sure what environment you're on) you can use mail:

some_command| mail foo@gmail.com bar@gmail.com -s "subject"
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319