Python电子邮件发送代码停止工作

时间:2021-01-02 21:38:02

标签: python-3.x

我制作了此代码用于发送批量电子邮件。最初,它工作正常,但突然间它停止工作。就像它不显示任何错误。但是,您永远不会在收件箱中收到任何电子邮件。

我已经与 GoDaddy 核实过他们是否有任何政策。他们说他们没有任何限制发送电子邮件,也没有我的域提供商。下面是代码。如果你能通过它,让我知道它在哪里以及为什么表现异常。

# Import  required libraries 
# Separately send mail using their mail Code 2 - sending the email email sent closing server

import smtplib
import csv
import ssl
from email.mime.text import MIMEText
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart  # New line
from email.mime.base import MIMEBase  # New line
from email import encoders  # New line

# User configuration
sender_email = 'no-reply@tanilicious.pk'
sender_name = 'Tani'
password = 'password' #mypassword




receiver_emails = ['nomanahmedna@gmail.com','unsubscribe@irex.pk'] 

receiver_names = ['nomanahmedna','unsubscribe']



# #for email - ids
# with open('email_2E-F.csv', 'r') as file:
#     reader = csv.reader(file, delimiter = ',')
#     for row in reader:
#         receiver_emails.extend(row)
# print(receiver_emails)

# #for email - name
# with open('email_2E-F.csv', 'r') as file:
#     reader = csv.reader(file, delimiter = ',')
#     for row in reader:
#         receiver_names.extend(row)
# print(receiver_names)



# Email body
email_html = open('tani.html')   #Example:- open('Classy.html')
email_body = email_html.read()       # Read HTML File 

filename = 'Tanilicious_Menu.pdf'

for receiver_email, receiver_name in zip(receiver_emails, receiver_names):
        print("Sending the email...")
        # Configurating user's info
        msg = MIMEMultipart()
        msg['To'] = formataddr((receiver_name, receiver_email))
        msg['From'] = formataddr((sender_name, sender_email))
        msg['Subject'] = 'New Biryani in Town'
    
        msg.attach(MIMEText(email_body, 'html'))

        try:
        # Open PDF file in binary mode
            with open(filename, "rb") as attachment:
                    part = MIMEBase("application", "octet-stream")
                    part.set_payload(attachment.read())
            # Encode file for ASCII character.
           
            encoders.encode_base64(part)

            # Add header to the email as key/value pair to attachment part
            part.add_header(
                    "Content-Disposition",
                    f"attachment; filename= {filename}",
            )

            msg.attach(part)
        except Exception as e:
                print(f'Oh no! We didn\'t found the attachment!\n{e}')
                break

        try:
                # Creating a SMTP session use 587 with TLS for gmail port.
                server = smtplib.SMTP('mail.tanilicious.pk', 587)
                # Encrypts the email
#                 context = ssl.create_default_context()
#                 server.starttls(context=context)
                # We log in into our Google account
                server.login(sender_email, password)
              
  # from sender, to receiver with the email attachment of html body
                server.sendmail(sender_email, receiver_email, msg.as_string())
                print('Email sent!')
        except Exception as e:
                print(f'Oh no! Something bad happened!\n{e}')
                break
        finally:
                print('Closing the server...')
                server.quit()

0 个答案:

没有答案