簡體   English   中英

如何通過python通過電子郵件發送“數組”?

[英]How to send an "array" with e-mail via python?

我想用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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM