반응형

파이썬으로 이메일(Gmail)을 보내보자.


Gmail 설정 화면. IMAP 사용으로 설정되어 있다. 이메일 클라이언트 구성 설정 방법을 확인해 보자.


발신 메일(SMTP) 서버 내용을 사용한다.


Gmail은 계정 비밀번호를 그대로 사용할 수 없다. Google 계정에서 앱 비밀번호를 생성해 사용해야 한다.


'생성할 앱 - 메일', '기기 - Windows 컴퓨터'를 선택하고 생성한다.



생선된 앱 비밀번호를 복사해 둔다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import smtplib
from email.mime.text import MIMEText
 
email_from = 'your email address'
email_to = 'recipient's email address'
email_subject = 'Email Test.'
email_content = 'Sending an email test.'
 
msg = MIMEText(email_content)
msg['From'= email_from
msg ['To'= email_to
msg['Subject'= email_subject
 
smtp = smtplib.SMTP('smtp.gmail.com'587)
smtp.starttls()
smtp.login('your email address''password')
smtp.sendmail("your email address""recipient's email address", msg.as_string())
 
print(msg.as_string())
 
smtp.quit()


위 코드를 입력하고 실행한다. smtp.login()의 'password'에는 위에서 생성한 앱 비밀번호를 입력한다.


에러 없이 실행되면 이메일이 보내진다.


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
# Import smtplib for the actual sending function
import smtplib
 
# Import the email modules we'll need
from email.message import EmailMessage
 
email_from = 'your email address'
email_to = 'recipient's email address'
email_subject = 'Email Test.'
email_content = 'Sending an email test.'
 
# Create a text/plain message
msg = EmailMessage()
msg.set_content(email_content)
 
# From == the sender's email address
# To == the recipient's email address
msg['From'= email_from
msg['To'= email_to
msg['Subject'= email_subject
 
# Send the message via our own SMTP server.
smtp = smtplib.SMTP('smtp.gmail.com'587)
smtp.starttls()
smtp.login('your email address''password')
smtp.send_message(msg)
 
print(msg.as_string())
 
smtp.quit()


위 코드로도 동일하게 진행 할 수 있다.



내용은 동일하지만 실행 결과를 확인해 보면 charset이 utf-8로 설정된다는 차이가 있다.


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
# Import smtplib for the actual sending function
import smtplib
# Import the email modules we'll need
from email.message import EmailMessage
# And imghdr to find the types of our images
import imghdr
 
email_from = 'your email address'
email_to = 'recipient's email address'
email_cc = 'cc1 email address, cc2 email address, ...'
email_subject = 'Email Test.'
email_content = 'Sending an email test.'
 
# Create a text/plain message
msg = EmailMessage()
msg.set_content(email_content)
 
# From == the sender's email address
# To == the recipient's email address
msg['From'= email_from
msg['To'= email_to
msg['Cc'= email_cc
msg['Subject'= email_subject
 
# Open the file in binary mode. Use imghdr to figure out the MIME subtype for the image.
with open('image.png''rb') as fp:
    img_data = fp.read()
    msg.add_attachment(img_data, maintype = 'image', subtype = imghdr.what(None, img_data), filename = 'image.png')
 
# Send the message via SMTP server.
smtp = smtplib.SMTP('smtp.gmail.com'587)
smtp.starttls()
smtp.login('your email address''password')
smtp.send_message(msg)
 
smtp.quit()


이번엔 Cc(Carbon copy)와 첨부파일을 추가해 보자.


참조와 첨부파일 모두 잘 처리 되었다. (feat. Lena)


반응형
Posted by J-sean
: