Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python和boto3通过AWS SES发送格式化的html电子邮件_Python_Python 3.x_Html Email_Boto3_Amazon Ses - Fatal编程技术网

使用Python和boto3通过AWS SES发送格式化的html电子邮件

使用Python和boto3通过AWS SES发送格式化的html电子邮件,python,python-3.x,html-email,boto3,amazon-ses,Python,Python 3.x,Html Email,Boto3,Amazon Ses,我正在尝试设置电子邮件功能,以发送带有格式化数据的电子邮件。我有以下方法可以在电子邮件中发送pandas数据框,但用于将pandas数据框转换为html的格式在电子邮件中无法使用用户定义的空格数 #install the AWS SES SDK for Python (boto3) from the command line #pip install boto3 import boto3 import pandas as pd import numpy as np df = pd.DataF

我正在尝试设置电子邮件功能,以发送带有格式化数据的电子邮件。我有以下方法可以在电子邮件中发送pandas数据框,但用于将pandas数据框转换为html的格式在电子邮件中无法使用用户定义的空格数

#install the AWS SES SDK for Python (boto3) from the command line
#pip install boto3

import boto3
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10, 4), columns=['A', 'B', 'C', 'D']) * 10000

def email(bodytext = 'No data...check your function arguments', dftoconvert = None):
    region = 'us-west-2'
    user = '' #insert your access key to use when creating the client
    pw = '' #insert the secret key to use when creating the client
    client = boto3.client(service_name = 'ses', 
                          region_name = region, 
                          aws_access_key_id = user, 
                          aws_secret_access_key = pw)
    me = 'me@example.com'
    you = ['you@example.com']
    subject = 'testSUBJECT'
    COMMASPACE = ', '
    you = COMMASPACE.join(you)
    #Build email message parts

    #Build and send email
    destination = { 'ToAddresses' : [you],
                    'CcAddresses' : [],
                    'BccAddresses' : []}
    try:
        bodyhtml = dftoconvert.to_html(float_format = lambda x: '({:15,.2f})'.format(abs(x)) if x < 0 else '+{:15,.2f}+'.format(abs(x)))
        message = {'Subject' : {'Data' : subject},
                   'Body': {'Html' : {'Data' : bodyhtml}}}
    except NoneType: #If there is no data to convert to html
        message = {'Subject' : {'Data' : subject},
                   'Body': {'Text' : {'Data' : bodytext}}}
    except Exception as e:
        print(e)
    result = client.send_email(Source = me, 
                               Destination = destination, 
                               Message = message)    
    return result if 'ErrorResponse' in result else ''

print(df)
email(dftoconvert = df)
#从命令行安装AWS SES Python SDK(boto3)
#pip安装boto3
进口boto3
作为pd进口熊猫
将numpy作为np导入
df=pd.DataFrame(np.random.randn(10,4),columns=['A','B','C','D'])*10000
def电子邮件(bodytext='无数据…检查函数参数',dftoconvert=无):
地区='us-west-2'
用户=“”#插入创建客户端时要使用的访问密钥
pw=''#插入创建客户端时要使用的密钥
client=bot3.client(服务名称='ses',
地区名称=地区,
aws\u访问\u密钥\u id=用户,
aws_secret_access_key=pw)
我me@example.com'
你=['you@example.com']
主题='testSUBJECT'
逗号空间=','
you=COMMASPACE.join(你)
#构建电子邮件消息部件
#生成并发送电子邮件
destination={'toaddress':[you],
“CCAddress”:[],
“BCCADRESS”:[]
尝试:
bodyhtml=dftoconvert.to_html(float_format=lambda x:'({:15,.2f})'.format(abs(x))如果x<0,则为'+{:15,.2f}+'.format(abs(x)))
消息={'Subject':{'Data':Subject},
'Body':{'Html':{'Data':bodyhtml}
非类型除外:#如果没有要转换为html的数据
消息={'Subject':{'Data':Subject},
'Body':{'Text':{'Data':bodytext}
例外情况除外,如e:
打印(e)
结果=客户。发送电子邮件(来源=我,
目的地=目的地,
消息=消息)
如果结果else“”中的“ErrorResponse”,则返回结果
打印(df)
电子邮件(dftoconvert=df)
由于某些原因,空格被忽略。我已经将有效数字标识符更改为非常小和非常大,并且html输出看起来是正确的,在Python中,浮点数的前导空格数是正确的。见下文

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10, 4), columns=['A', 'B', 'C', 'D']) * 10000


df

              A             B             C             D
0   1071.786803  -4354.776685  -3541.261466   2653.522461
1    489.865060 -12567.822512  13888.890274  14243.027303
2    471.995980   9473.174725  -1776.897694   5085.236174
3   5932.486256 -12558.720083 -17714.593696  -3503.574051
4  -8886.624311 -10286.622739  -4513.326771   2714.793954
5   5965.944055  10207.608141  19224.094501   4748.746867
6  14189.480430 -13826.251008   9847.711830  -1241.976560
7  -9044.406158 -14337.121541  19572.135090 -18146.356528
8   3057.233113 -14410.383480   -931.179975 -16273.711970
9 -14492.047676  -1150.506849  -1892.032700   -797.596310

df.to_html(float_format = lambda x: '({:15,.2f})'.format(abs(x)) if x < 0 else '+{:15,.2f}+'.format(abs(x)))
将熊猫作为pd导入
将numpy作为np导入
df=pd.DataFrame(np.random.randn(10,4),columns=['A','B','C','D'])*10000
df
A、B、C、D
0   1071.786803  -4354.776685  -3541.261466   2653.522461
1    489.865060 -12567.822512  13888.890274  14243.027303
2    471.995980   9473.174725  -1776.897694   5085.236174
3   5932.486256 -12558.720083 -17714.593696  -3503.574051
4  -8886.624311 -10286.622739  -4513.326771   2714.793954
5   5965.944055  10207.608141  19224.094501   4748.746867
6  14189.480430 -13826.251008   9847.711830  -1241.976560
7  -9044.406158 -14337.121541  19572.135090 -18146.356528
8   3057.233113 -14410.383480   -931.179975 -16273.711970
9 -14492.047676  -1150.506849  -1892.032700   -797.596310
df.to_html(float_format=lambda x:“({:15,.2f})”.format(abs(x))如果x<0,则为“+{:15,.2f}+”.format(abs(x)))
Out[3]:'\n\n\n\n A\n B\n C\n D\n\n\n\n 0\n+1071.79+\n(4354.78)\n(3541.26)\n+2653.52+\n\n\n 1\n+489.87+\n(12567.82)\n+13888.89+\n+14243.03+\n\n\n 2\n+472.00+\n+9473.17+\n(1776.90)\n+5085.24+\n\n\n\n 3\n+5932.49+\n(12558.72)\n(17714.59)\n(3503.57)\n\n\n 4\n(8886.62)\n(10286.62)\n(4513.33)\n+2714.79+\n\n\n 5\n+5965.94+\n+10207.61+\n+19224.09+\n+4748.75+\n\n\n 6\n+14189.48+\n(13826.25)\n+9847.71+\n(1241.98)\n\n\n 7\n(9044.41)\n(14337.12)\n+19572.14+\n(18146.36)\n\n\n 8\n+3057.23+\n(14410.38)\n(931.18)\n(16273.71)\n\n\n 9\n(14492.05)\n(1150.51)\n(1892.03)\n(797.60)\n\n\n'

但是电子邮件似乎忽略了任何前导空格,并以相同的方式显示html,无论前导空格有多少。我不知道如何使html格式在电子邮件中的显示方式与html在Python输出中的显示方式相同(带前导空格)。有没有更好的方法?有没有比html更好的格式发送电子邮件格式的数据帧

  • Python 3.5.2
  • 熊猫0.18.1
  • Numpy 1.11.1

我假设您希望在表格单元格中使用前导空格来格式化电子邮件中的表格。这不起作用,因为空格将被合并

你可以把两个空格替换成不带空格的空格,这会使邮件看起来有点不同,但并不理想

def email(bodytext = 'No data...check your function arguments', dftoconvert = None, replace=False):
    region = 'us-west-2'
    user = '' #insert your access key to use when creating the client
    pw = '' #insert the secret key to use when creating the client
    client = boto3.client(service_name = 'ses', 
                          region_name = region, 
                          aws_access_key_id = user, 
                          aws_secret_access_key = pw)
    me = 'me@example.com'
    you = ['you@example.com']
    subject = 'testSUBJECT'
    COMMASPACE = ', '
    you = COMMASPACE.join(you)
    #Build email message parts

    #Build and send email
    destination = { 'ToAddresses' : [you],
                    'CcAddresses' : [],
                    'BccAddresses' : []}
    try:
        bodyhtml = dftoconvert.to_html(float_format = lambda x: '({:15,.2f})'.format(abs(x)) if x < 0 else '+{:15,.2f}+'.format(abs(x)))
        # use no-break space instead of two spaces next to each other
        if replace:
            bodyhtml = bodyhtml.replace('  ', '&nbsp;')
        message = {'Subject' : {'Data' : subject},
                   'Body': {'Html' : {'Data' : bodyhtml}}}
    except NoneType: #If there is no data to convert to html
        message = {'Subject' : {'Data' : subject},
                   'Body': {'Text' : {'Data' : bodytext}}}
    except Exception as e:
        print(e)
    result = client.send_email(Source = me, 
                               Destination = destination, 
                               Message = message)    
    return result if 'ErrorResponse' in result else ''

email(dftoconvert=df)
email(dftoconvert=df, replace=True)
def email(bodytext='No data…check your function arguments',dftoconvert=None,replace=False):
地区='us-west-2'
用户=“”#插入创建客户端时要使用的访问密钥
pw=''#插入创建客户端时要使用的密钥
client=bot3.client(服务名称='ses',
地区名称=地区,
aws\u访问\u密钥\u id=用户,
aws_secret_access_key=pw)
我me@example.com'
你=['you@example.com']
主题='testSUBJECT'
逗号空间=','
you=COMMASPACE.join(你)
#构建电子邮件消息部件
#生成并发送电子邮件
destination={'toaddress':[you],
“CCAddress”:[],
'