来自 Python 脚本的电子邮件自动进入垃圾邮件(票务系统)

问题描述 投票:0回答:1

我制作了一个活动门票系统(使用Python),它将一个html文件作为字符串发送到收件人的电子邮件(使用谷歌电子表格中的信息)及其特殊条形码。它运作良好,但唯一的问题是,如果我将其发送到新电子邮件,收件人电子邮件会自动将其标记为垃圾邮件

import smtplib
import ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import gspread
from google.oauth2.service_account import Credentials
from pdf417 import encode, render_image
import time

# Load the HTML template from file
with open('ticket-format-aio.html', 'r') as file:
    html_template = file.read()

# Connect to Google Spreadsheet
credentials = Credentials.from_service_account_file('placeholder.json')
scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/spreadsheets'])
spreadsheet_id = 'placeholder123'
gc = gspread.authorize(scoped_credentials)
worksheet = gc.open_by_key(spreadsheet_id).sheet1

# Get data from the spreadsheet
data = worksheet.get_all_records()

# Email Connection
email_from = '[email protected]'
password = 'placeholder' 

# Connect to the Gmail SMTP server
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
    server.login(email_from, password)

    # Initialize the counter for total emails sent
    total_emails_sent = 0

    # List to store successfully sent email addresses
    successfully_sent_emails = []

    # Loop through each row of data and send emails
    for row_number, row in enumerate(data, start=2):  # Start from row 2 (assuming headers are in row 1)
        full_name = row['fullName']
        id_number = str(row['schoolID'])  # Convert id_number to a string
        grade_level = row['gradeLevel']
        email_to = row['emailAddress']
        uuid = row['uuid']
        email_sent = row['EmailSent']

        # Check if the email has already been sent
        if email_sent == '':
            # Generate the barcode image
            barcode_data = uuid  # Use the UUID as barcode data
            barcode_codes = encode(barcode_data)
            barcode_image = render_image(barcode_codes)
            barcode_image_path = f'barcode_{uuid}.png'
            barcode_image.save(barcode_image_path)

            email_subject = f'Halloween Dance Ticket - {full_name}'

            # Insert Barcode
            html_with_barcode = html_template.replace("scan-code-replaced-a-lot.png", "cid:barcode_image_cid") \
                                             .replace("{{FULL_NAME}}", full_name) \
                                             .replace("{{ID_NUMBER}}", id_number) \
                                             .replace("{{GRADE_LEVEL}}", grade_level) \
                                             .replace("{{EMAIL}}", email_to) \
                                             .replace("{{UUID}}", uuid)

            # Construct email message
            email_message = MIMEMultipart()
            email_message['From'] = email_from
            email_message['To'] = email_to
            email_message['Subject'] = email_subject

            # Attach the HTML content
            email_message.attach(MIMEText(html_with_barcode, "html"))

            # Attach the barcode image as CID attachment
            with open(barcode_image_path, "rb") as image_file:
                image_data = image_file.read()
                barcode_attachment = MIMEImage(image_data)
                barcode_attachment.add_header('Content-Disposition', f'inline', filename=barcode_image_path)
                barcode_attachment.add_header('Content-ID', '<barcode_image_cid>')
                email_message.attach(barcode_attachment)

            # Send the email
            server.sendmail(email_from, email_to, email_message.as_string())

            # Update the 'EmailSent' column to mark it as sent
            worksheet.update_cell(row_number, 6, 'Yes')

            # Append the successfully sent email to the list
            successfully_sent_emails.append(email_to)

            # Increment the counter for total emails sent
            total_emails_sent += 1

            # Cleanup: Delete the generated barcode image
            import os
            os.remove(barcode_image_path)

            # Wait for 2 seconds before sending the next email
            time.sleep(2)

    # After loop, print the total number of emails sent
    print(f"Total Emails Sent Successfully: {total_emails_sent}")

    # Print the list of successfully sent email addresses
    print("Successfully sent emails:")
    for email in successfully_sent_emails:
        print(email)

我找不到关于这个主题的太多信息,所以我来到这里。

python html html-email
1个回答
0
投票

有关此主题的信息稀缺是由于可用解决方案的范围有限。电子邮件被标记为垃圾邮件主要源于电子邮件提供商实施的严格反垃圾邮件措施。一些可能的解决方案如下:

  1. 自定义域:使用您自己的域而不是通用 Gmail 地址发送电子邮件。电子邮件提供商更有可能信任来自您控制的域的电子邮件。

  2. 身份验证记录:为您的自定义域设置 SPF、DKIM 和 DMARC 记录,以帮助电子邮件服务器验证您的电子邮件的合法性。

  3. 内容质量:精心设计电子邮件内容,以避免类似垃圾邮件的特征,例如过度大写、感叹号和垃圾邮件语言。确保 HTML 模板干净且结构良好。

  4. 标头准确性:正确设置电子邮件标头格式,确保“发件人”地址与发送电子邮件帐户匹配。考虑为不同的回复目的地设置“Reply-To”标头。

  5. 监管合规性:遵守 CAN-SPAM(针对美国)或 GDPR(针对欧盟)等电子邮件法规,以促进负责任的电子邮件实践。

需要注意的是,电子邮件被标记为垃圾邮件的问题与 Python 或您的电子邮件代码无关;这主要是电子邮件提供商严格的垃圾邮件预防措施的结果。这是一个令人头疼的问题。

© www.soinside.com 2019 - 2024. All rights reserved.