如何通过Python发送一个“数组”电子邮件?

huangapple go评论127阅读模式
英文:

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

问题

我想使用Gmail发送邮件。但邮件必须特别包含一个数组。我需要帮助。
代码块中的消息部分,特别接受文本类型。

如果我明确告诉我的目标; 我有一个**.csv**文件,我正在从这个文件中创建随机的子样本。子样本包括5行。我使用这些随机行创建了一个数组,我想发送这个类型为数组的随机行。

  1. import pandas as pd
  2. import smtplib
  3. data = pd.read_csv("Words1.csv")
  4. row1 = data.sample(n=5)
  5. A = [row1]
  6. print(A)
  7. email = 'sender@gmail.com' # 您的电子邮件
  8. password = 'senderpassword' # 您的电子邮件帐户密码
  9. send_to_email = 'receiver@gmail.com' # 发送消息的收件人
  10. message = 'A' # 电子邮件中的消息
  11. server = smtplib.SMTP('smtp.gmail.com', 587) # 连接到服务器
  12. server.starttls() # 使用TLS
  13. server.login(email, password) # 登录到电子邮件服务器
  14. server.sendmail(email, send_to_email, message) # 发送邮件
  15. server.quit() # 退出电子邮件服务器

谢谢。

英文:

I want to send a mail with gmail. But mail must specifically include an array. I need help.
Message part in the code block, specifically accept text type.

If I tell specifically my goal; I have a .csv file, and I'm creating random Sub Sample from this file. Sub Sample includes 5 rows. I created an array with this random rows and I want to send this random rows which type is array.

  1. import pandas as pd
  2. import smtplib
  3. data = pd.read_csv("Words1.csv")
  4. row1 = data.sample(n=5)
  5. A = [row1]
  6. print(A)
  7. email = 'sender@gmail.com' # Your email
  8. password = 'senderpassword' # Your email account password
  9. send_to_email = 'receiver@gmail.com' # Who you are sending the message to
  10. message = 'A' # The message in the email
  11. server = smtplib.SMTP('smtp.gmail.com', 587) # Connect to the server
  12. server.starttls() # Use TLS
  13. server.login(email, password) # Login to the email server
  14. server.sendmail(email, send_to_email, message) # Send the email
  15. server.quit() # Logout of the email server

Thank you.

答案1

得分: 0

  1. import numpy as np
  2. import pandas as pd
  3. import smtplib
  4. from email.mime.multipart import MIMEMultipart
  5. from email.mime.text import MIMEText
  6. data = pd.read_csv("Words.csv")
  7. row1 = data.sample(n=5)
  8. A = [row1]
  9. num = np.array(row1)
  10. head = ['C1', 'C2', 'C3']
  11. row = ['1', '2', '3', '4', '5']
  12. df = pd.DataFrame(num, index=row, columns=head)
  13. html = df.to_html()
  14. print(html)
  15. def py_mail(SUBJECT, BODY, TO, FROM):
  16. """With this function we send out our html email"""
  17. MESSAGE = MIMEMultipart('alternative')
  18. MESSAGE['subject'] = SUBJECT
  19. MESSAGE['To'] = TO
  20. MESSAGE['From'] = FROM
  21. HTML_BODY = MIMEText(BODY, 'html')
  22. MESSAGE.attach(HTML_BODY)
  23. password = "@YourPassword"
  24. server = smtplib.SMTP('smtp.gmail.com:587')
  25. server.starttls()
  26. server.login(FROM, password)
  27. server.sendmail(FROM, [TO], MESSAGE.as_string())
  28. server.quit()
  29. if __name__ == "__main__":
  30. """Executes if the script is run as the main script (for testing purposes)"""
  31. email_content = html
  32. TO = 'to@gmail.com'
  33. FROM = 'from@gmail.com'
  34. py_mail("Test email subject", email_content, TO, FROM)
英文:

Solution;

  1. import numpy as np
  2. import pandas as pd
  3. import smtplib
  4. from email.mime.multipart import MIMEMultipart
  5. from email.mime.text import MIMEText
  6. data = pd.read_csv("Words.csv")
  7. row1 = data.sample(n=5)
  8. A = [row1]
  9. num = np.array(row1)
  10. head = ['C1', 'C2', 'C3']
  11. row = ['1','2','3','4','5']
  12. df = pd.DataFrame(num, index=row, columns=head)
  13. html = df.to_html()
  14. print(html)
  15. def py_mail(SUBJECT, BODY, TO, FROM):
  16. """With this function we send out our html email"""
  17. MESSAGE = MIMEMultipart('alternative')
  18. MESSAGE['subject'] = SUBJECT
  19. MESSAGE['To'] = TO
  20. MESSAGE['From'] = FROM
  21. HTML_BODY = MIMEText(BODY, 'html')
  22. MESSAGE.attach(HTML_BODY)
  23. password = "@YourPassword"
  24. server = smtplib.SMTP('smtp.gmail.com:587')
  25. server.starttls()
  26. server.login(FROM,password)
  27. server.sendmail(FROM, [TO], MESSAGE.as_string())
  28. server.quit()
  29. if __name__ == "__main__":
  30. """Executes if the script is run as main script (for testing purposes)"""
  31. email_content = html
  32. TO = 'to@gmail.com'
  33. FROM ='from@gmail.com'
  34. py_mail("Test email subject", email_content, TO, FROM)

huangapple
  • 本文由 发表于 2020年1月6日 21:13:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定