[PYTHON] Receive by standard input or send Gmail from command line operation

http://blog.livedoor.jp/vine_user/archives/51575120.html We have received from the above site entirely.

mail.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import smtplib
from email.MIMEText import MIMEText
from email.Header import Header
from email.Utils import formatdate

user = '[email protected]'
pswd = 'password'
print 'Please enter the destination. Press the enter key to confirm.'
to_addr = raw_input()
print 'Please enter the subject. The enter key confirms the subject.'
title = raw_input()
print 'Please enter the text. The enter key will be sent when the text is confirmed.'
body = raw_input()


def create_message(user, to_addr, subject, body):
    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['From'] = user
    msg['To'] = to_addr
    msg['Date'] = formatdate()
    return msg


def send_via_gmail(user, to_addr, msg):
    s = smtplib.SMTP('smtp.gmail.com', 587)
    s.ehlo()
    s.starttls()
    s.ehlo()
    s.login(user, pswd)
    s.sendmail(user, [to_addr], msg.as_string())
    s.close()

if __name__ == '__main__':
    msg = create_message(user, to_addr, title, body)
    send_via_gmail(user, to_addr, msg)

This time it seems to be received from the command line, but I hope that if you pass something else as a kick, such as the text, it will be useful as a script for sending emails. .. I am afraid of accidents with the character code, but I have not verified it. If it was the usage I expected, there was no problem with From with the logged-in account. Since it is a different gmail sending method and login method than usual, it will definitely fail the first time http://qiita.com/hilotter/items/ffa5a7d14f6d50c81dfa Please refer to the above.

If I wrote it in another article, it didn't matter to me, so I broke my heart, so I added it in the form of ... When someone specifically follows you, you send an email to your friends to brag about. Since it is only an example if you are followed, you can read the TL and say that a statement containing a specific word matches, or send an email to yourself when it is replied, but there is officially such a function. right.

mayu.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import smtplib
from email.MIMEText import MIMEText
from email.Header import Header
from email.Utils import formatdate
import tweepy
import oat
##↑ is a separate OAuth token for twitter.
##I have to keep the gmail pass etc. separately or use a non-plaintext method, but ...

user = '[email protected]'
pswd = 'password'
to_addr = 'Email address of a friend'
title = 'subject'
body = 'Text'


def create_message(user, to_addr, subject, body):
    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['From'] = user
    msg['To'] = to_addr
    msg['Date'] = formatdate()
    return msg


def send_via_gmail(user, to_addr, msg):
    s = smtplib.SMTP('smtp.gmail.com', 587)
    s.ehlo()
    s.starttls()
    s.ehlo()
    s.login(user, pswd)
    s.sendmail(user, [to_addr], msg.as_string())
    s.close()

if __name__ == '__main__':
#This time up to 5000 followers_Since it is an ids acquisition, it must be less than 5000 before and after execution.
#Example user_ids 611447337 is Mayu Nozomi, who is often talked about on the huge bulletin board where twitter remarks are made.
    chk = oat.api.followers_ids(twi)
    if 611447337 in chk:
        msg = create_message(user, to_addr, title, body)
        send_via_gmail(user, to_addr, msg)
    else:
        pass

Recommended Posts

Receive by standard input or send Gmail from command line operation
[Python] Read command line arguments from file name or stdin
Twitter post from command line