python: emailhelper

 

"""
python 发送邮件,用授权码

"""
import smtplib
from email.mime.text import MIMEText


def send():
    """
     发送邮件
    :return:
    """
    try:
        stmpserver163='smtp.163.com'
        stmpport=25
        frommail='geovindu@163.com'
        code163='geovindu'

        toemails=['463588883@qq.com','geovindu@jw28.com']
        #创建邮件:
        msg=MIMEText('<h3>hi, how are you. send mail<h3>','html','utf-8')  #邮件内容
        msg['Subject'] = 'ICT 公益培训 考试日期通知'   #主题
        msg['To']=','.join(toemails)  #收件人
        msg['From']=frommail  #发送人

        #STMP
        stmp=smtplib.SMTP(stmpserver163,stmpport)  #创建STMP
        stmp.login(frommail,code163)
        # 发送邮件,发送人,收邮件人,发送内容
        stmp.sendmail(frommail,toemails,msg.as_string())
        stmp.close()
        print("email ok")
    except Exception as ex:
        print(ex)

  

 

"""
python 发送邮件,用授权码
https://mailtrap.io/blog/python-send-email/
https://pythonalgos.com/how-to-send-an-email-with-an-attachment-in-python/
https://pythonroadmap.com/blog/guide/send-email-attachments-with-python
python send email attachment
https://pytutorial.com/how-to-send-an-email-with-attachment-in-python/


"""
import smtplib
from email.mime.text import MIMEText
from email.message import EmailMessage
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders


def sendattachment():
    """
    还附件发送邮件
    :return:
    """
    try:
        sender_email = "geovindu@163.com"
        receiver_email = "463588883@qq.com"
        message = MIMEMultipart()
        message["From"] = sender_email
        message['To'] = receiver_email
        message['Subject'] = "sending mail using python"
        file = "doc.txt"
        attachment = open(file, 'rb')
        obj = MIMEBase('application', 'octet-stream')
        obj.set_payload((attachment).read())
        encoders.encode_base64(obj)
        obj.add_header('Content-Disposition', "attachment; filename= " + file)
        message.attach(obj)
        my_message = message.as_string()
        email_session = smtplib.SMTP('smtp.163.com', 25)
        email_session.starttls()
        email_session.login(sender_email, 'geovindu')
        email_session.sendmail(sender_email, receiver_email, my_message)
        email_session.quit()
        print("YOUR MAIL HAS BEEN SENT SUCCESSFULLY")
        #没有邮件内容,只有附件发送成功

        #2
        # Creating the Email Object
        subject='hi,附件'
        message = MIMEMultipart()
        message["From"] = sender_email
        message["To"] = receiver_email
        message["Subject"] = subject

        # Attaching the File
        attachment_path = "day7-网络编程.pdf"

        # Attach the file using the MIMEBase class
        attachment = open(attachment_path, "rb")
        payload = MIMEBase("application", "octet-stream")
        payload.set_payload((attachment).read())
        encoders.encode_base64(payload)
        payload.add_header(
            "Content-Disposition", f"attachment; filename= {attachment_path.split('/')[-1]}"
        )
        message.attach(payload)

        # Establishing the SMTP Connection
        smtp_server = "smtp.163.com"
        smtp_port = 25

        with smtplib.SMTP(smtp_server, smtp_port) as server:
            server.starttls()
            server.login(sender_email, "geovindu")
            server.send_message(message)
        #附件未成功

        #3
        '''
        import smtplib
        from email.mime.multipart import MIMEMultipart
        from email.mime.text import MIMEText
        from email.mime.base import MIMEBase
        from email import encoders
        
        # Set up email details
        sender_email = "your_email@gmail.com"
        receiver_email = "recipient_email@example.com"
        subject = "Email with Attachments"
        body = "Dear recipient,\n\nPlease find the attached files.\n\nBest regards,\nSender"
    
        # Create the email object
        message = MIMEMultipart()
        message["From"] = sender_email
        message["To"] = receiver_email
        message["Subject"] = subject
    
        # Add the body of the email
        message.attach(MIMEText(body, "plain"))
    
        # Attach the files
        attachment_paths = ["path_to_file/data.csv", "path_to_file/data.xlsx"]
    
        for attachment_path in attachment_paths:
            attachment = open(attachment_path, "rb")
            part = MIMEBase("application", "octet-stream")
            part.set_payload((attachment).read())
            encoders.encode_base64(part)
            part.add_header("Content-Disposition", f"attachment; filename= {attachment_path.split('/')[-1]}")
            message.attach(part)
    
        # Connect to the SMTP server and send the email
        smtp_server = "smtp.gmail.com"
        smtp_port = 587
    
        with smtplib.SMTP(smtp_server, smtp_port) as server:
            server.starttls()
            server.login(sender_email, "your_password")  # Replace with your password or use app-specific passwords
            server.send_message(message)
        '''
    except Exception  as ex:
        print(ex)




def send():
    """
     发送邮件
    :return:
    """
    try:
        stmpserver163='smtp.163.com'
        stmpport=25
        frommail='geovindu@163.com'
        code163='YRXSNJGQNPDZTIOE'

        toemails=['463588883@qq.com','geovindu@jw28.com']
        #创建邮件:
        msg=MIMEText('<h3>hi, how are you. send mail<h3>','html','utf-8')  #邮件内容
        msg['Subject'] = 'ICT 公益培训 考试日期通知'   #主题
        msg['To']=','.join(toemails)  #收件人
        msg['From']=frommail  #发送人

        #STMP
        stmp=smtplib.SMTP(stmpserver163,stmpport)  #创建STMP
        stmp.login(frommail,code163)
        stmp.set_debuglevel(1)
        # 发送邮件,发送人,收邮件人,发送内容
        stmp.sendmail(frommail,toemails,msg.as_string())

        stmp.quit()
        stmp.close()
        return True
        print("email ok")
    except Exception as ex:
        print(ex)
        return False

  

posted @ 2023-07-23 10:16  ®Geovin Du Dream Park™  阅读(38)  评论(0)    收藏  举报