반응형

파이썬으로 이메일(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
:
반응형

The SendInput function inserts the events in the INPUT structures serially into the keyboard or mouse input stream.

마우스 입력을 보낸다.


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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <Windows.h>
#include <iostream>
 
using namespace std;
 
void PrintMenu()
{
    cout << "--- Mouse sender ---" << endl << endl;
    cout << "0: End" << endl;
    cout << "1: Get cursor position" << endl;
    cout << "2: Send left button click" << endl;
    cout << "3: Send right button click" << endl;
    cout << "4: Send cursor move" << endl;
    cout << "Choose: ";
}
 
void GetCursorPosition()
{
    Sleep(2000);    // Wait for 2 seconds before get the cursor position.
 
    POINT cursorPos;
    GetCursorPos(&cursorPos);
 
    cout << "Current cursor position : (x: " << cursorPos.x << ", y: " << cursorPos.y << ")" << endl;
}
 
void SendLeftClick()
{
    Sleep(5000);
 
    INPUT input;
    ZeroMemory(&input, sizeof(INPUT));
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    SendInput(1&input, sizeof(INPUT));
 
    input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
    SendInput(1&input, sizeof(INPUT));
}
 
void SendRightClick()
{
    Sleep(5000);
 
    INPUT input;
    ZeroMemory(&input, sizeof(INPUT));
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
    SendInput(1&input, sizeof(INPUT));
 
    input.mi.dwFlags = MOUSEEVENTF_RIGHTUP;
    SendInput(1&input, sizeof(INPUT));
}
 
void SendMove()
{
    int x, y;
    int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    int screenHeight = GetSystemMetrics(SM_CYSCREEN);
 
    cout << "Screen size: (" << screenWidth << ", " << screenHeight << ")" << endl;
    cout << "[x: 0~" << screenWidth - 1 << ", y: 0~" << screenHeight - 1 << "]" << endl;
    cout << "x: ";
    cin >> x;
    cout << "y: ";
    cin >> y;
 
    INPUT input;
    ZeroMemory(&input, sizeof(INPUT));
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
    // If MOUSEEVENTF_ABSOLUTE value is specified, dx and dy contain normalized absolute coordinates
    // between 0 and 65,535. The event procedure maps these coordinates onto the display surface.
    // Coordinate (0,0) maps onto the upper-left corner of the display surface; coordinate (65535,65535)
    // maps onto the lower-right corner. In a multimonitor system, the coordinates map to the primary monitor.
    //
    // If the MOUSEEVENTF_ABSOLUTE value is not specified, dxand dy specify movement relative to the previous
    // mouse event(the last reported position).Positive values mean the mouse moved right(or down); negative
    // values mean the mouse moved left(or up).
    float dx = x * (65535.0f / (screenWidth - 1));
    float dy = y * (65535.0f / (screenHeight - 1));
    input.mi.dx = LONG(dx);
    input.mi.dy = LONG(dy);
    SendInput(1&input, sizeof(INPUT));
}
 
int main()
{
    int choice = 1;
 
    while (choice)
    {
        PrintMenu();
        cin >> choice;
 
        switch (choice)
        {
        case 1:
            GetCursorPosition();
 
            break;
        case 2:
            SendLeftClick();
 
            break;
        case 3:
            SendRightClick();
 
            break;
        case 4:
            SendMove();
 
            break;
        default:
 
            return 0;
        }
    }
 
    return 0;
}



Run the program and choose the event you want to send.


반응형
Posted by J-sean
: