본문 바로가기

Python_Intermediate/Automation

Python 급여명세서 단체로 메일 발송

반응형
from helper import sendmail
import datetime as dt

now_time = dt.datetime.now()
now_year = now_time.year
now_month = now_time.month

from_addr = "메일 발송 주소"
subject_tpl = '{name}님의 {yy}년 {mm}월 급여명세서 입니다.'

with open("본문 추가 사항 파일 경로", "r", encoding='utf-8') as f:
content_tpl = f.read()

with open("급여 명세서 파일 경로(csv파일)", "r", encoding='euc-kr') as f:
csv_data = f.readlines()
count = len(csv_data)

result_tpl = "{0}/{1} >> {2}({3})님께 메일을 발송했습니다."

for i, line in enumerate(csv_data):
item = line.strip().split(",")
to_name = item[0].strip()
to_addr = item[1].strip()
file1 = item[2].strip()
file2 = item[3].strip()

subject = subject_tpl.format(name=to_name, yy=now_year, mm=now_month)
content = content_tpl.format(name=to_name, yy=now_year, mm=now_month)

sendmail(from_addr, to_addr, subject, content, [file1, file2])

print(result_tpl.format(i+1, count, to_name, to_addr))

print("=" * 30)
print("메일 발송이 완료되었습니다.")


1. 급여 명세서 파일은 CSV로 만든다.

발송대상이름1 발송대상이메일1 첨부파일1-1 파일경로 첨부파일1-2 파일경로
발송대상이름2 발송대상이메일2 첨부파일2-1 파일경로 첨부파일2-2 파일경로
발송대상이름3 발송대상이메일3 첨부파일3-1 파일경로 첨부파일3-2 파일경로
발송대상이름4 발송대상이메일4 첨부파일4-1 파일경로 첨부파일4-2 파일경로


2. 각 열과 행을 읽어들여 발송한다.


3. Test Code

from helper import sendmail
import datetime as dt

now_time = dt.datetime.now()
now_year = now_time.year
now_month = now_time.month

from_addr = "ankiwoongtest@gmail.com"
subject_tpl = '{name}님의 {yy}년 {mm}월 급여명세서 입니다.'

with open("mail/content.html", "r", encoding='utf-8') as f:
content_tpl = f.read()

with open("mail/mail_list.csv", "r", encoding='euc-kr') as f:
csv_data = f.readlines()
count = len(csv_data)

result_tpl = "{0}/{1} >> {2}({3})님께 메일을 발송했습니다."

for i, line in enumerate(csv_data):
item = line.strip().split(",")
to_name = item[0].strip()
to_addr = item[1].strip()
file1 = item[2].strip()
file2 = item[3].strip()

subject = subject_tpl.format(name=to_name, yy=now_year, mm=now_month)
content = content_tpl.format(name=to_name, yy=now_year, mm=now_month)

sendmail(from_addr, to_addr, subject, content, [file1, file2])

print(result_tpl.format(i+1, count, to_name, to_addr))

print("=" * 30)
print("메일 발송이 완료되었습니다.")


4. Test Code 결과물

< Code Run 결과물 >

< Gmail 결과물 >

< Naver 결과물 >


반응형