我正在使用以下方法使用SMTP从Python发送邮件.这是正确的使用方法还是我遗失了?
from smtplib import SMTP
import datetime
debuglevel = 0
smtp = SMTP()
smtp.set_debuglevel(debuglevel)
smtp.connect('YOUR.MAIL.SERVER', 26)
smtp.login('USERNAME@DOMAIN', 'PASSWORD')
from_addr = "John Doe <john@doe.net>"
to_addr = "foo@bar.com"
subj = "hello"
date = datetime.datetime.now().strftime( "%d/%m/%Y %H:%M" )
message_text = "Hello\nThis is a mail from your server\n\nBye\n"
msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s"
% ( from_addr, to_addr, subj, date, message_text )
smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()
Run Code Online (Sandbox Code Playgroud) 我正在为Mac 编写一个Bash shell脚本,它通过打开一个automator应用程序发送电子邮件通知,该应用程序使用Mail.app中的默认邮件帐户发送电子邮件.automator应用程序还附加脚本已写入的文本文件.这个解决方案的问题是
我想通过直接在脚本中输入SMTP设置,发送地址等来直接从脚本发送邮件来解决这些缺点.问题是我想在多台计算机(10.5和10.6)上部署此脚本而不在计算机上启用Postfix.是否可以在脚本中执行此操作,以便它将在10.5的基本Mac OS X安装上运行.和10.6?
更新:我找到了-bsSendmail 的选项,这似乎是我需要的,但我不知道如何指定设置.
此外,澄清一下,我想指定SMTP设置的原因是,通过Postfix发出的来自端口25的localhost的邮件将被大多数企业防火墙阻止,但如果我指定服务器和备用端口,我将无法运行进入那个问题.
I want to send a pandas dataframe data as an HTML e-mail. Based on this post I could create an html with the dataframe. Code
import pandas as pd
import numpy as np
HEADER = '''
<html>
<head>
</head>
<body>
'''
FOOTER = '''
</body>
</html>
'''
df = pd.DataFrame([[1.1, 1.1, 1.1, 2.6, 2.5, 3.4,2.6,2.6,3.4,3.4,2.6,1.1,1.1,3.3], list('AAABBBBABCBDDD')]).T
with open('test.html', 'w') as f:
f.write(HEADER)
f.write(df.to_html(classes='df'))
f.write(FOOTER)
Run Code Online (Sandbox Code Playgroud)
Now I want to send this as a html e-mail. I tried this. Can not …
我正在尝试编写一个python脚本来发送一个使用HTML格式的电子邮件,并涉及许多不间断的空格.但是,当我运行它时,一些字符串被每171个字符出现的空格打断,这个例子可以看出:
#!/usr/bin/env python
import smtplib
import socket
from email.mime.text import MIMEText
emails = ["my@email.com"]
sender = "test@{0}".format(socket.gethostname())
message = "<html><head></head><body>"
for i in range(20):
message += " " * 50
message += "<br/>"
message += "</body>"
message = MIMEText(message, "html")
message["Subject"] = "Test"
message["From"] = sender
message["To"] = ", ".join(emails)
mailer = smtplib.SMTP("localhost")
mailer.sendmail(sender, emails, message.as_string())
mailer.quit()
Run Code Online (Sandbox Code Playgroud)
该示例应该生成一个仅包含空格的空白电子邮件,但最终看起来像这样:
  ;
&nb sp;
& nbsp;
&nbs p;
&n bsp;
Run Code Online (Sandbox Code Playgroud)
编辑:如果它很重要,我正在运行带有Postfix的Ubuntu 15.04用于smtp客户端,并使用python2.6.
我正在使用win32com.client,python 2.7.x并outlook 2013在windows平台上.
我需要将HTML文件的内容发布到outlook消息正文.
我按照这里的帖子,这里和这里关于如何保存excel HTML和粘贴数据outlook.
但是,当我通过win32com.client.Dispatch而不是看到消息来阅读文件时,我看到的是HTML代码.
以下是将已处理xlsx文件转换为html使用格式的代码win32.com.
#Section to convert excel workbook to html
myfile = os.getcwd()+ "\\" + outfile
newfile = os.getcwd()+ "\\" + "emailData.html"
xl = EnsureDispatch('Excel.Application')
#xl.Visible = True
wb3 = xl.Workbooks.Open(myfile)
wb3WorkSheet = wb3.Worksheets(1)
wb3WorkSheet.Activate()
wb3.SaveAs(newfile, constants.xlHtml)
wb3.Close(True)
xl.Workbooks.Close()
xl.Quit()
del xl
Run Code Online (Sandbox Code Playgroud)
上面的输出newfile …
如果从文件中读取电子邮件正文,则Python2.7输出格式无法获得预期.user_info.txt是由另一个作业生成的文件,其中包含用户的所有详细信息.user_info.txt的输出格式很好.将该文件作为电子邮件发送的地方,输出格式完全改变.从文件中读取时我做错了吗?有人可以帮我这个吗?
脚本:
#!/usr/bin/env python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart('alternative')
msg['Subject'] = "User Info"
"""Create the body of the message"""
open_file = open('/tmp/user_info.txt')
user_info = MIMEText(open_file.read().encode("utf-8"), 'plain', 'utf-8')
msg.attach(user_info)
open_file.close()
"""Send the message via gmail SMTP server."""
server = smtplib.SMTP('smtp-relay.gmail.com', 587)
server.sendmail("admin", "user1@test.com", msg.as_string())
server.quit()
Run Code Online (Sandbox Code Playgroud)
示例user_info.txt
user_name Department_no ID detail1 detail2
aaaa 4 13 25088 32.000000
bbbbbbb 5 17 33280 42.000000
ccccccccccc 3 9 16896 22.000000
dddddd 5 17 33280 42.000000
eeeeeeeee …Run Code Online (Sandbox Code Playgroud) def call_panda():
filename = 'C:\\file.csv'
cols_to_use = ['col1', 'col2', 'col3', 'col4']
df = pd.read_csv(filename, nrows= 5,usecols=cols_to_use,index_col='col1')
# Send email
me = 'me@email.com'
you = 'you@email.com'
textfile = df
with open(textfile, 'rb') as fp:
msg = MIMEText(fp.read())
msg['Subject'] = 'Contents of file'
msg['From'] = me
msg['To'] = you
s = smtplib.SMTP('mailhost.acme.net')
s.sendmail(me, [you], msg.as_string())
s.quit()
Run Code Online (Sandbox Code Playgroud)
错误消息是 open(textfile, 'rb') as fp: TypeError: Expected str, bytes or os.PathLike object, not DataFrame
我有一个Python代码,该代码为Reddit的数据框架创建报告,并将其转换为简单的HTML,然后通过电子邮件发送出去。下面是代码:
#Clean all the Dataframes
test_clean = clean(test_test_df)
brand_clean = clean(brands_df)
competitor_clean = clean(competitors_df)
#Convert to HTML
test_html = test_clean.render()
brand_html = brand_clean.render()
competitor_html = competitor_clean.render()
# In[27]:
brand_clean
# # Email Integration
# #### Import Libraries for Email
# In[ ]:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from datetime import date
# #### Send Email If No Data is Available
# In[ ]:
if test_test_df.empty:
today = str(date.today())
fromaddr = "email@email.com"
toaddr = "email@email.com"
msg …Run Code Online (Sandbox Code Playgroud) 我有一个在晚上运行的脚本,当它运行时,它写入日志文件,一旦任务完成,它写的文本也会通过电子邮件发送给我.有时候任务可能会失败,我想要发生的是当我收到电子邮件时,出现错误的行是红色和粗体.
所以我的问题是,如果满足特定条件,Python格式文本可以是某种字体/颜色吗?如果有的话有什么好的例子吗?
谢谢
我在Internet上阅读了很多书,以了解如何使用Python使用mailchimp API发送电子邮件。网站似乎是如此复杂,没有任何示例。
拜托,您能指导我介绍任何使用Python的示例吗?
我使用安装在图书馆从PIP:
pip install mailchimp;
我已经创建了露营。
但是,我不知道如何以编程方式发送电子邮件。
python ×7
email ×4
html ×2
pandas ×2
python-2.7 ×2
bash ×1
dataframe ×1
formatting ×1
macos ×1
mailchimp ×1
mime ×1
outlook ×1
python-3.x ×1
reddit ×1
shell ×1
smtp ×1
whitespace ×1
win32com ×1
windows ×1