Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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发送电子邮件?_Python_Arrays_Pandas_Email_Gmail - Fatal编程技术网

如何发送;数组";通过python发送电子邮件?

如何发送;数组";通过python发送电子邮件?,python,arrays,pandas,email,gmail,Python,Arrays,Pandas,Email,Gmail,我想用gmail发封邮件。但是邮件必须特别包括数组。我需要帮助。 代码块中的消息部分,特别是接受文本类型 如果我具体说出我的目标;我有一个.csv文件,我正在从这个文件创建随机子样本。子样本包括5行。我用这个随机行创建了一个数组,我想发送这个随机行,它的类型是数组 import pandas as pd import smtplib data = pd.read_csv("Words1.csv") row1 = data.sample(n=5) A = [row1] print(A) emai

我想用gmail发封邮件。但是邮件必须特别包括数组。我需要帮助。 代码块中的消息部分,特别是接受文本类型

如果我具体说出我的目标;我有一个.csv文件,我正在从这个文件创建随机子样本。子样本包括5行。我用这个随机行创建了一个数组,我想发送这个随机行,它的类型是数组

import pandas as pd
import smtplib

data = pd.read_csv("Words1.csv")
row1 = data.sample(n=5)
A = [row1]
print(A)
email = 'sender@gmail.com'  # Your email
password = 'senderpassword'  # Your email account password
send_to_email = 'receiver@gmail.com'  # Who you are sending the message to
message = 'A'  # The message in the email 

server = smtplib.SMTP('smtp.gmail.com', 587)  # Connect to the server
server.starttls()  # Use TLS
server.login(email, password)  # Login to the email server
server.sendmail(email, send_to_email, message)  # Send the email
server.quit()  # Logout of the email server
多谢各位

溶液

import numpy as np
import pandas as pd
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

data = pd.read_csv("Words.csv")
row1 = data.sample(n=5)
A = [row1]
num = np.array(row1)

head = ['C1', 'C2', 'C3']
row = ['1','2','3','4','5']
df = pd.DataFrame(num, index=row, columns=head)
html = df.to_html()
print(html)

def py_mail(SUBJECT, BODY, TO, FROM):
    """With this function we send out our html email"""

    MESSAGE = MIMEMultipart('alternative')
    MESSAGE['subject'] = SUBJECT
    MESSAGE['To'] = TO
    MESSAGE['From'] = FROM

    HTML_BODY = MIMEText(BODY, 'html')
    MESSAGE.attach(HTML_BODY)


    password = "@YourPassword"
    server = smtplib.SMTP('smtp.gmail.com:587')
    server.starttls()
    server.login(FROM,password)
    server.sendmail(FROM, [TO], MESSAGE.as_string())
    server.quit()

if __name__ == "__main__":
    """Executes if the script is run as main script (for testing purposes)"""

    email_content = html

    TO = 'to@gmail.com'
    FROM ='from@gmail.com'

    py_mail("Test email subject", email_content, TO, FROM)
您需要“序列化”数组/列表。关于如何在pythonpandas
中执行此操作,有很多在线资源。没有输入的to_csv()
方法将返回csv字符串,因此只需执行
a=row1即可。to_csv()
您可以看到一个示例