-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsendmsg.py
67 lines (55 loc) · 2.38 KB
/
sendmsg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
######################################################################
# #
# sendmsg.py -- Demo to send a message via Gmail using libgmail #
# Found on the internet and adapted for the pur- #
# poses of Project AVALON #
# #
# Authors: Original work: follower@myrealbox.com #
# #
# Project Avalon: Stefan Wismer #
# wismerst@student.ethz.ch #
# #
# License: GPL 2.0 #
# #
######################################################################
import os
import sys
import logging
# Allow us to run using installed `libgmail` or the one in parent directory.
try:
import libgmail
## Wouldn't this the preffered way?
## We shouldn't raise a warning about a normal import
##logging.warn("Note: Using currently installed `libgmail` version.")
except ImportError:
# Urghhh...
sys.path.insert(1,
os.path.realpath(os.path.join(os.path.dirname(__file__),
os.path.pardir)))
import libgmail
if __name__ == "__main__":
import sys
from getpass import getpass
try:
name = "avalontheboat"
to = "wismerst@student.ethz.ch"
subject = "Test"
msg = "Automatisch generiertes Mail aus Python Programm"
except IndexError:
print "Usage: %s <account> <to address> <subject> <body>" % sys.argv[0]
raise SystemExit
pw = "thenewcastor"
ga = libgmail.GmailAccount(name, pw)
print "\nPlease wait, logging in..."
try:
ga.login()
except libgmail.GmailLoginFailure:
print "\nLogin failed. (Wrong username/password?)"
else:
print "Log in successful.\n"
gmsg = libgmail.GmailComposedMessage(to, subject, msg)
if ga.sendMessage(gmsg):
print "Message sent `%s` successfully." % subject
else:
print "Could not send message."
print "Done."