python 으로 간단하게 email 전송하기
참고로 email.py 라는 이름은 되도록 피하기 바란다.(참고 : Unable to import Python's email module at all - Stack Overflow)
# encoding=utf-8 # Import smtplib for the actual sending function import smtplib # Import the email modules we'll need from email.mime.text import MIMEText def main(): textfile = 'sampletext.txt' me = 'sender-email@hbwhale.com' you = 'receiver-email@gmail.com' # Open a plain text file for reading. For this example, assume that # the text file contains only ASCII characters. fp = open(textfile, 'rb') # Create a text/plain message msg = MIMEText(fp.read()) fp.close() # me == the sender's email address # you == the recipient's email address msg['Subject'] = 'The contents of %s' % textfile msg['From'] = me msg['To'] = you # Send the message via our own SMTP server, but don't include the # envelope header. s = smtplib.SMTP('smtp.worksmobile.com', 587) s.login(me, 'password') s.sendmail(me, [you], msg.as_string()) s.quit() if __name__ == '__main__': main()
개인적인 자료
# encoding=utf-8 from __future__ import unicode_literals # Import smtplib for the actual sending function import smtplib, sys # Import the email modules we'll need from email.mime.text import MIMEText # ================================================ # # Class # # ================================================ class SimpleEmail(object): def __init__(self, to=None): self.me = 'myemail@account.com' self.you = 'receiver@gmail.com' if to is None else to self.pw = 'myemail_password' def send(self, emailContent): # Create a text/plain message msg = MIMEText(emailContent) msg['Subject'] = 'The contents of %s' % emailContent msg['From'] = self.me msg['To'] = self.you # Send the message via our own SMTP server, but don't include the # envelope header. s = smtplib.SMTP('smtp.worksmobile.com', 587) s.ehlo() # it's for gmail s.starttls() # it's for gmail s.login(self.me, self.pw) s.sendmail(self.me, [self.you], msg.as_string()) s.quit() # ================================================ # # Decorator # # ================================================ def send_error_and_done_email(to=None): def dec(func): def dec_dec(*args, **kwargs): try: func(*args, **kwargs) except: # catch *all* exceptions e = sys.exc_info()[0] SimpleEmail(to=to).send(repr(e)) # send e-mail raise SimpleEmail().send('%s is done'%(str(func),)) return return dec_dec return dec
gmail 이용시
기본적으로 google 이 unsecured app 에 대한 접근을 막고 있다. 그래서 이것을 풀어줘야 한다. (SMTP 로 login 하는 것이 unsecure 하긴 하니..) 아래 경로로 가면 된다.
개인적으로는 이정도까지 하면 gmail 을 사용할 수 있었다.
ref. 3 에서는 그밖에 captcha 등을 꺼야하는 등의 이야기도 있으니 참고하자.
그밖의
- linux 의 sendmail 을 사용하는 경우 : email - Sending mail via sendmail from python - Stack Overflow
- image 를 삽입하는 경우 : python - Sending Multipart html emails which contain embedded images - Stack Overflow
- file attach 하는 방법 : python - How to send email attachments? - Stack Overflow
Reference
- Using Python to Send Email
- How to send an email with Gmail as provider using Python? - Stack Overflow
- How to Send Emails with Gmail using Python
댓글 없음:
댓글 쓰기