gpt4 book ai didi

python - 使用 python 替换电子邮件的正文

转载 作者:行者123 更新时间:2023-11-30 23:06:27 24 4
gpt4 key购买 nike

我用 python 创建了一个类,它将通过我的一个私有(private)服务器发送电子邮件。它有效,但我想知道是否有一种方法可以用新的电子邮件正文替换现有的电子邮件正文?

电子邮件发送器类

class Emailer:

def __init__(self, subj=None, message=None, toAddr=None, attachment=None, image=None):
# initialize email inputs

self.msg = email.MIMEMultipart.MIMEMultipart()

self.cidNum = 0

self.message = []
if message is not None:
self.addToMessage(message,image)

# set the subject of the email if there is one specified
self.subj = []
if subj is not None:
self.setSubject(subj)

# set the body of the email and any attachements specified
self.attachment = []
if attachment is not None:
self.addAtachment(attachment)

# set the recipient list
self.toAddr = []
if toAddr is not None:
self.addRecipient(toAddr)

def addAttachment(self,attachment):
logger.debug("Adding attachement to email")
# loop through list of attachments and add them to the email
if attachment is not None:
if type(attachment) is not list:
attachment = [attachment]
for f in attachment:
part = email.MIMEBase.MIMEBase('application',"octet-stream")
part.set_payload( open(f,"rb").read() )
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="{0}"'.format(os.path.basename(f)))
self.msg.attach(part)

def addToMessage(self,message,image=None):
logger.debug("Adding to email message. Content: [%s]" % message)
# add the plain text message
self.message.append(message)
# add embedded images to message
if image is not None:
if type(image) is not list:
image = [image]
for i in image:
msgText = email.MIMEText.MIMEText('<br><img src="cid:image%s"><br>' % self.cidNum, 'html')
self.msg.attach(msgText)

fp = open(i, 'rb')
img = email.MIMEImage.MIMEImage(fp.read())
fp.close()
img.add_header('Content-ID','<image%s>' % self.cidNum)
self.msg.attach(img)
self.cidNum += 1

# method to set the subject of the email
def setSubject(self,subj):
self.msg['Subject'] = subj

# method to add recipients to the email
def addRecipient(self, toAddr):
# loop through recipient list
for x in toAddr:
self.msg['To'] = x

# method to configure server settings: the server host/port and the senders login info
def configure(self, serverLogin, serverPassword, fromAddr, toAddr, serverHost='myserver', serverPort=465):
self.server=smtplib.SMTP_SSL(serverHost,serverPort)
self.server.set_debuglevel(True)
# self.server.ehlo()
# self.server.ehlo()
self.server.login(serverLogin, serverPassword) #login to senders email
self.fromAddr = fromAddr
self.toAddr = toAddr

# method to send the email
def send(self):
logger.debug("Sending email!")
msgText = email.MIMEText.MIMEText("\n".join(self.message))
self.msg.attach(msgText)
print "Sending email to %s " % self.toAddr
text = self.msg.as_string() #conver the message contents to string format
try:
self.server.sendmail(self.fromAddr, self.toAddr, text) #send the email
except Exception as e:
logger.error(e)

目前,addToMessage() 方法用于将文本添加到电子邮件正文中。如果 addToMessage() 已被调用,但我想用新文本替换该正文,有办法吗?

最佳答案

如果addToMessage()已经被调用,但我想用新文本替换正文,有办法吗?

是的。如果您总是替换添加到 self.message 中的最后一个条目,则可以使用 self.message[-1] 引用此元素,因为它是一个列表。如果要替换特定元素,可以使用 index() 方法搜索它。

示例#1:替换正文中最后写入的文本

def replace_last_written_body_text(new_text):
if len(self.message) > 0:
self.message[-1] = new_text

示例#2:替换正文中的指定文本

def replace_specified_body_text(text_to_replace, new_text):
index_of_text_to_replace = self.message.index(text_to_replace)
if index_of_text_to_replace is not None:
self.message[index_of_text_to_replace] = new_text
else:
logger.warning("Cannot replace non-existent body text")

关于python - 使用 python 替换电子邮件的正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32636342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com