이것은 smtplib와 email이라는 두 개의 라이브러리만 사용하여 Python으로 이메일 보내기를 시작할 수 있는 가장 쉬운 방법입니다.
이 예에서는 Gmail의 무료 RESTful API를 사용합니다.
코드는 다음과 같습니다.
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText message = MIMEMultipart() message["To"] = 'To line here.' message["From"] = 'From line here.' message["Subject"] = 'Subject line here.' title = '<b> Title line here. </b>' messageText = MIMEText('''Message body goes here.''','html') message.attach(messageText) email = 'Your Gmail address here.' password = 'Your app password here.' server = smtplib.SMTP('smtp.gmail.com:587') server.ehlo('Gmail') server.starttls() server.login(email,password) fromaddr = 'From line here.' toaddrs = 'Address you send to.' server.sendmail(fromaddr,toaddrs,message.as_string()) server.quit()
코드는 어떻게 작동하나요?
먼저 smtplib를 가져온 다음 email.mime.multipart 및 email.mime.text에서 각각 MIMEMultipart 및 MIMEText를 가져옵니다.
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText
그런 다음 MIMEMultiPart()를 호출하고 이를 Message 변수에 인스턴스화합니다. 그런 다음 메시지, 받는 사람, 보낸 사람 및 제목인 각 변수에 문자열 값을 제공합니다. 이는 다음과 같습니다.
Message = MIMEMultipart() Message["To"] = 'To line here.' Message["From"] = 'From line here.' Message["Subject"] = 'Subject line here.'
또한 title 변수를 통해 이메일에 제목을 지정하고 MIMEText()를 통해 메시지를 추가하고 이를 messageText 변수에 인스턴스화한 다음 마지막으로 attachment() 함수를 사용하여 이메일 메시지를 기본 변수 Message에 첨부합니다. 다음과 같아야 합니다:
title = '<b> Title line here. </b>' messageText = MIMEText('''Message body goes here.''','html') Message.attach(messageText)
Gmail 주소와 앱 비밀번호를 추가해 보겠습니다. 비밀번호를 얻는 방법을 모르신다면 여기 링크를 클릭하세요.
email = 'Your Gmail address here.' password = 'Your App password here.'
그런 다음 Gmail의 SMTP 서버에 연결해야 합니다. smtplib 라이브러리를 사용하여 연결하고 연결하려는 서버와 smtp.gmail.com인 포트라는 두 개의 변수가 필요합니다. 및 각각 587입니다.
smtplib 라이브러리를 사용하여 SMTP 함수를 호출하고 서버 및 포트 변수를 인수로 추가합니다. 다음과 같습니다.
smtplib.SMTP('smtp.gmail.com:587') (그들 사이에 :를 잊지 마세요)
그런 다음 이를 변수 서버로 인스턴스화합니다.
server = smtplib.SMTP('smtp.gmail.com:587')
그런 다음 server.ehlo('Gmail')를 사용하여 ehlo 문을 추가합니다. 이는 도메인 이름이어야 하며 ESMTP를 지원하는 다른 메일 서버로 이메일을 보낼 때 유용합니다. 간단하게 Gmail을 입력해 보겠습니다.
server.ehlo('Gmail')
또한 server.starttls()를 사용하여 TLS 프로토콜을 시작해 보겠습니다. 이는 보안 연결을 통해 이메일을 보내려는 메일 서버에 알려줍니다.
server.starttls()
다음 줄을 사용하여 메일 서버에 로그인합니다.
server.login(email,password)
fromaddr과 toaddrs를 각각 사용하여 from 주소와 to 주소를 추가해 보겠습니다.
fromaddr = 'Your Gmail address.' toaddrs = 'Destination address.'
마지막으로 server.sendmail(fromaddr,toaddrs,message.as_string())을 사용하여 이메일을 보내고 server.quit()을 사용하여 메일 서버에 대한 연결을 닫습니다.
server.sendmail(fromaddr,toaddrs,message.as_string()) server.quit()
send_email.py라는 파일에 저장하고 Linux를 사용하는 경우 터미널을 열거나 Windows를 사용하는 경우 명령 프롬프트를 열고 python send_email.py를 사용하여 실행하면 다음이 표시됩니다.
아무 일도 일어나지 않으면 좋은 소식입니다!
대상 이메일로 이메일을 받으셨을 것입니다. 제가 받은 내용은 다음과 같습니다.
Python과 Gmail의 무료 SMTP 서버를 사용하여 이메일을 보내는 것은 Python 코드 내에서 이메일을 보낼 수 있는 가장 쉬운 방법입니다. 이것이 바로 제가 처음으로 온라인 비즈니스를 구축할 때 했던 일입니다. 자세한 내용은 여기에서 읽어보실 수 있습니다.
위 내용은 Gmail의 무료 SMTP 메일 서버 API를 사용하여 Python에서 이메일을 보내는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

forhandlinglargedatasetsinpython, usenumpyarraysforbetterperformance.1) numpyarraysarememory-effic andfasterfornumericaloperations.2) leveragevectorization foredtimecomplexity.4) managemoryusage withorfications data

inpython, listsusedyammoryAllocation과 함께 할당하고, whilempyarraysallocatefixedMemory.1) listsAllocatemememorythanneedInitiality.

Inpython, youcansspecthedatatypeyfelemeremodelerernspant.1) usenpynernrump.1) usenpynerp.dloatp.ploatm64, 포모 선례 전분자.

numpyissentialfornumericalcomputinginpythonduetoitsspeed, memory-efficiency 및 comperniveMathematicaticaltions

contiguousUousUousUlorAllocationScrucialForraysbecauseItAllowsOfficationAndFastElementAccess.1) ItenableSconstantTimeAccess, o (1), DuetodirectAddressCalculation.2) Itimprovesceeffiency theMultipleementFetchespercacheline.3) Itsimplififiesmomorym

slicepaythonlistisdoneusingthesyntaxlist [start : step : step] .here'showitworks : 1) startistheindexofthefirstelementtoinclude.2) stopistheindexofthefirstelemement.3) stepisincrementbetwetweentractionsoftortionsoflists

NumpyAllowsForVariousOperationsOnArrays : 1) BasicArithmeticLikeadDition, Subtraction, A 및 Division; 2) AdvancedOperationsSuchasmatrixmultiplication; 3) extrayintondsfordatamanipulation; 5) Ag

Arraysinpython, 특히 Stroughnumpyandpandas, areestentialfordataanalysis, setingspeedandefficiency


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 Linux 새 버전
SublimeText3 Linux 최신 버전

VSCode Windows 64비트 다운로드
Microsoft에서 출시한 강력한 무료 IDE 편집기

Eclipse용 SAP NetWeaver 서버 어댑터
Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

mPDF
mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.