相关疑难解决方法(0)

使用SMTP从Python发送邮件

我正在使用以下方法使用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)

python smtp

107
推荐指数
7
解决办法
19万
查看次数

从Bash shell脚本发送邮件

我正在为Mac 编写一个Bash shell脚本,它通过打开一个automator应用程序发送电子邮件通知,该应用程序使用Mail.app中的默认邮件帐户发送电子邮件.automator应用程序还附加脚本已写入的文本文件.这个解决方案的问题是

  1. 发送时,它在GUI中可见
  2. 如果Mail不是当前的应用程序,它会窃取焦点
  3. 这取决于Mail.app的帐户设置在将来有效

我想通过直接在脚本中输入SMTP设置,发送地址等来直接从脚本发送邮件来解决这些缺点.问题是我想在多台计算机(10.5和10.6)上部署此脚本而不在计算机上启用Postfix.是否可以在脚本中执行此操作,以便它将在10.5的基本Mac OS X安装上运行.和10.6?

更新:我找到了-bsSendmail 的选项,这似乎是我需要的,但我不知道如何指定设置.

此外,澄清一下,我想指定SMTP设置的原因是,通过Postfix发出的来自端口25的localhost的邮件将被大多数企业防火墙阻止,但如果我指定服务器和备用端口,我将无法运行进入那个问题.

email macos bash shell

35
推荐指数
7
解决办法
7万
查看次数

Send pandas dataframe data as html e-mail

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 …

html python email pandas

8
推荐指数
2
解决办法
2万
查看次数

Python邮件每171个字符插入一次空格

我正在尝试编写一个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 += "&nbsp;" * 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)

该示例应该生成一个仅包含空格的空白电子邮件,但最终看起来像这样:

         &nbsp ;                                    


                       &nb sp;                      


                                     & nbsp;        






             &nbs p;                                


                           &n bsp;          
Run Code Online (Sandbox Code Playgroud)

编辑:如果它很重要,我正在运行带有Postfix的Ubuntu 15.04用于smtp客户端,并使用python2.6.

python email whitespace

7
推荐指数
1
解决办法
1531
查看次数

Outlook 2013显示HTML代码而不是实际数据

我正在使用win32com.client,python 2.7.xoutlook 2013windows平台上.

我需要将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 …

windows outlook win32com python-2.7

7
推荐指数
1
解决办法
480
查看次数

使用smtplib发送文件时更改stdout格式 - python2.7

如果从文件中读取电子邮件正文,则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)

python python-2.7

7
推荐指数
1
解决办法
352
查看次数

Python3 如何在电子邮件中发送 Pandas Dataframe

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

dataframe pandas

6
推荐指数
2
解决办法
2万
查看次数

如何在HTML脚本中集成HTML代码?

我有一个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)

html python mime reddit python-3.x

6
推荐指数
2
解决办法
413
查看次数

python是否可以格式化它发送的电子邮件?

我有一个在晚上运行的脚本,当它运行时,它写入日志文件,一旦任务完成,它写的文本也会通过电子邮件发送给我.有时候任务可能会失败,我想要发生的是当我收到电子邮件时,出现错误的行是红色和粗体.

所以我的问题是,如果满足特定条件,Python格式文本可以是某种字体/颜色吗?如果有的话有什么好的例子吗?

谢谢

python formatting

4
推荐指数
1
解决办法
2万
查看次数

如何使用Python和Mailchimp发送电子邮件?

我在Internet上阅读了很多书,以了解如何使用Python使用mailchimp API发送电子邮件。网站似乎是如此复杂,没有任何示例。

拜托,您能指导我介绍任何使用Python的示例吗?

到目前为止我尝试过的是:

  • 我使用安装在图书馆从PIP: pip install mailchimp;

  • 我已经创建了露营。

  • 我已经创建了列表;

但是,我不知道如何以编程方式发送电子邮件。

python email mailchimp

2
推荐指数
1
解决办法
4216
查看次数