이번 아티클에서는 지금까지 살펴보았던 내용을 전반적으로 활용하는 응용 예제를 살펴보겠습니다. send_mail이라는 함수에서 보내는 사람, 받는 사람, 제목, 내용이라는 문자열 파라미터를 받아 일정한 내용을 출력하는 함수를 정의해 보겠습니다.
이 때 보내는 사람은 당연히 한 명이고, 받는 사람은 3명으로 정의해 보겠습니다.
def send_mail(from_email, to_email, subject, contents):
print("From:\t" + from_email)
print("To:\t" + to_email)
print("Subject: " + subject)
print("Contents")
print("-"*10)
print(contents)
print("-"*10)
print("Completed")
print("*"*10)
print(' ')
my_email = 'hello@helloworld.com'
users = []
users.append({'name':'john', 'email':'john@helloworld.com'})
users.append({'name':'kim', 'email':'kim_helloworld.com'})
users.append({'name':'doe', 'email':'doe@helloworld.com'})
contents = "Thank you for everything."
for user in users:
title = 'Dear. ' + user['name']
if '@' not in user['email']:
continue
send_mail(my_email, user['email'], title, contents)
'''
From: hello@helloworld.com
To: john@helloworld.com
Subject: Dear. john
Contents
----------
Thank you for everything.
----------
Completed
**********
From: hello@helloworld.com
To: doe@helloworld.com
Subject: Dear. doe
Contents
----------
Thank you for everything.
----------
Completed
**********
'''
'Programming > Python 업무 자동화' 카테고리의 다른 글
3. 함수(1) - 함수의 정의 (0) | 2025.03.06 |
---|---|
2. 제어문(2) - 반복문 2 : while문 [2] (1) | 2025.02.24 |
2. 제어문(2) - 반복문 2 : while문 [1] (1) | 2025.02.21 |
2. 제어문(2) - 반복문 1 : for문 (1) | 2025.02.18 |
2. 제어문(1) - 조건문 (1) | 2025.02.16 |