ส่งเมลด้วยไพธอน หัดเขียน Python ส่งเมลผ่าน SMTP

แชร์ความรู้ภาษา Python ไพทอน การเขียนโปรแกรมภาษาไพทอน

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41232
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

ส่งเมลด้วยไพธอน หัดเขียน Python ส่งเมลผ่าน SMTP

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ส่งเมลด้วยไพธอน หัดเขียน #Python ส่งเมลด้วย #SMTP
สิ่งที่ต้องใช้คือ
1. โมดูล smtplib,email ถ้ายังไม่มีให้ติดตั้ง โมดูลเสริมกันก่อน ตามกระทู้นี้
https://www.mindphp.com/forums/viewtopic ... 44&t=38007
2. ข้อมูล Mail Server SMTP
SMTP Server:
SMTP Port:
Username:
Password:
เมื่อมีครบแล้วลงโค้ดกันเย
test_mindphp_smtp_mail.py

โค้ด: เลือกทั้งหมด

import email
import smtplib

msg = email.message_from_string('warning')
msg['From'] = "[email protected]"	# เมลผู้ส่ง
msg['To'] = "[email protected]" # เมลผู้รับ
msg['Subject'] = "Hi From mindphp.com"

s = smtplib.SMTP("smtp.live.com",465)
s.ehlo() # ชื่อต่อกับ Hostname 
s.starttls() # กำหนดใช้ใช้ SSL การติดต่อกับ SMTP server ด้วยโหมท TLS mode
s.ehlo()
s.login(msg['From'], 'pass')

s.sendmail(msg['From'], msg['To'], msg.as_string())

s.quit()
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41232
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: ส่งเมลด้วยไพธอน หัดเขียน Python ส่งเมลผ่าน SMTP

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

อีกตัวอย่างจากหนังสือ python 101

โค้ด: เลือกทั้งหมด

import os
import smtplib

from email import encoders
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.utils import formatdate

def send_email(email, pdf):
    """
    Send an email out
    """
    header0 = 'Content-Disposition'
    header1 ='attachment; filename="%s"' % os.path.basename(pdf)
    header = header0, header1
    
    host = "mail.server.com"
    server = smtplib.SMTP(host)
    subject = "Test email from Python"
    to = email
    from_addr = "[email protected]"
    body_text = "Here is the Alpha copy of Python 101, Part I"
    
    # create the message
    msg = MIMEMultipart()
    msg["From"] = from_addr
    msg["Subject"] = subject
    msg["Date"] = formatdate(localtime=True)
    msg["To"] = email
    
    msg.attach( MIMEText(body_text) )
    
    attachment = MIMEBase('application', "octet-stream")
    try:
        with open(pdf, "rb") as fh:
            data = fh.read()
        attachment.set_payload( data )
        encoders.encode_base64(attachment)
        attachment.add_header(*header)
        msg.attach(attachment)
    except IOError:
        msg = "Error opening attachment file %s" % file_to_attach
        print(msg)
        
    server.sendmail(from_addr, to, msg.as_string())
    
if __name__ == "__main__":
    send_email("[email protected]", "output/python101.pdf")
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 73