برای ارسال ایمیل از طریق زبان برنامه نویسی پایتون می توانید از تابع زیر استفاده کنید:
def send_email(user, pwd, recipient, subject, body):
import smtplib
FROM = user
TO = recipient if isinstance(recipient, list) else [recipient]
SUBJECT = subject
TEXT = body
# Prepare actual message
message = """From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(user, pwd)
server.sendmail(FROM, TO, message)
server.close()
print 'Email was sent.'
except:
print "Failed to send the mail."
لازم به ذکر است که بایستی در تنظیمات ایمیلتان، گزینه Less secure apps access را در بخش Security فعال کنید.
درباره این سایت