我正在尝试使用python从CentOS发送带有附件的电子邮件。我下面的代码在Windows计算机上可以正常工作,但是,当我运行此代码时,它给我一个错误
(110,“连接超时”)
def send_email(self):
print("Sending an email...")
try:
sender = 'xxxxx@domain.com'
receiver = 'yyyyy@domain.com'
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = receiver
msg['Subject'] = self.new_folder_name + 'Portal Automation testing results'
body = 'Hi, \n\n This is my email body. \n\n Thanks.'
msg.attach(MIMEText(body, 'plain'))
filename = self.zip_file_name
attachment = open(filename, 'rb')
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
# part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= " + filename)
msg.attach(part)
server = smtplib.SMTP('mail.xxxxx.com', 587)
server.starttls()
server.login(sender, "mypassword")
text = msg.as_string()
server.sendmail(sender, receiver, text)
server.quit()
print('.......')
print("Email sent.")
print("-------------------------------------------")
except Exception as e:
print('Email can not be sent. Error ====>', e)
因此,我使用了子流程,它确实发送了带有附件的电子邮件,但是我无法根据需要修改邮件正文。这是我的代码,
def send_email(self):
print('Sending email to xxxxxxx@domain.com')
body = 'This is email body'
file_path = '/home/Path/to/file/{}/{}'.format(self.new_folder_name, self.zip_file_name)
send_email_1 = 'echo {} | mail -s "Email testing" -a {} xxxxle@nuance.com'.format(body, file_path)
send = subprocess.call(send_email_1, shell=True)
电子邮件输出:嗨,/ n / n这是示例电子邮件boby。 / n / n问候
我需要有关第一个代码示例的帮助,以使其能够在CentOS上运行,或者需要第二个代码示例,以便我可以格式化电子邮件正文。
我们非常感谢您的帮助。