用python实现邮件合并
时间: 2023-09-23 20:09:59 浏览: 42
以下是一个基本的python程序,用于将不同的电子邮件文本合并为一个电子邮件。该程序可以从一个文本文件中读取电子邮件文本,然后将所有文本合并为一个电子邮件。
```python
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# SMTP server configuration
smtp_server = "smtp.gmail.com"
smtp_port = 587
# Sender email configuration
sender_email = "sender@gmail.com"
sender_password = "sender_password"
# Recipient email configuration
recipient_email = "recipient@gmail.com"
# Read email text from file
with open("email.txt", "r") as f:
email_text = f.read()
# Create a message object and set the sender, recipient, and subject
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = recipient_email
message["Subject"] = "Merged Email"
# Attach the email text as a MIME text object to the message
email_body = MIMEText(email_text, "plain")
message.attach(email_body)
# Connect to the SMTP server and send the message
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, recipient_email, message.as_string())
```
在上面的代码中,我们首先配置SMTP服务器的详细信息,如服务器名称和端口号。然后,我们设置了发送者和接收者电子邮件的详细信息。接下来,我们从文件中读取电子邮件文本,并使用MIMEText对象将其附加到消息中。最后,我们连接到SMTP服务器并使用sendmail()方法将消息发送给接收者电子邮件地址。