This post will be short, it will deal with sending out an SMS and email at the same time.
If you are a subscriber to Gmail, and who isn’t, it is a relatively simple matter to send out SMSs and emails at the same time using Python.
This is a snippet from my calling application:
1: #!/usr/bin/env python
2:
3: import sendsms2
4:
5: cMessage = "Test message"
6: cSMSRecipient = "5195551234"
7: cEmailRecipient = "recipientname@recipientisp.com"
8: carrier = "yourcarrier.com"
9: cSMSSent = sendsms2.sms("SMS",cMessage,cSMSRecipient,carrier)
10: if (cSMSSent[:7] != "Problem"):
11: sendsms2.sms("EMAIL",cMessage,cEmailRecipient,"")
12: else:
13: print "Unable to send message. The sendsms module reports: + cSMSSent
and this is the module that is being called:
1: #!/usr/bin/env python
2:
3: import smtplib
4: import sys
5:
6: def sms(messagetype,message,recipient,carrier):
7: """Login and attempt to send message"""
8: mailserver = smtplib.SMTP('smtp.gmail.com')
9: print mailserver.ehlo()
10: print mailserver.starttls()
11: print mailserver.ehlo()
12: user = 'yourname@gmail.com'
13: passw = 'yourpassword'
14: try:
15: mailserver.login(user, passw)
16: except smtplib.SMTPException:
17: print 'Problem logging in'
18: sys.exit(1)
19:
20: if (messagetype == "EMAIL"):
21: headers = ["From: yourname@gmail.com","Subject: Test","To:" + recipient ,"Mime-Version: 1.0","Content-Type: text/html"]
22: headers = "\r\n".join(headers)
23: message = headers + "\r\n\r\n" + message + '\nSent from my Raspberry Pi'
24: else:
25: recipient = recipient + '@' + carrier
26:
27: print 'Sending...\n'
28: try:
29: mailserver.sendmail(user,recipient,message)
30: except smtplib.SMTPException:
31: return 'Problem could not send message'
32: else:
33: return 'Message sent'
34: mailserver.quit()
35:
36:
37: if __name__=="__main__":
38:
39: sms(user, passw)
That’s it! As usual with software, you can expand it forever, but for now, this will have to do.
No comments:
Post a Comment