SoFunction
Updated on 2024-11-10

The python operation that automatically monitors and reads the latest emails

I'll cut to the chase, so let's get right to the code~

The #zmail library: can help us receive an email with a few lines of code
import zmail
#Enter account number and password
server=('13163964546@','jie110341')
# Get the latest email
mail=server.get_latest()
#Read the mail
#(mail)
#Read part of the message
print(mail['subject'])
......
#Read attachments, emails, storage paths, overwrite if there is a file with the same name.
zmail.save_acctachment(mail,target_path=None,overwrite=True)

You need to download the zmail library on your computer

Addendum: Python mailbox implementation to monitor computers

I'll cut to the chase, so let's get right to the code~

import smtplib
import poplib
import email
from  import MIMEApplication
from  import MIMEMultipart
from  import MIMEText
from  import decode_header
def send_email(account, password, email_title, send_text="", file_names=None, file_dir="."):
  msg = MIMEMultipart()
  # msg = MIMEText(HTML, 'html') -- Only text can be sent.
  content = MIMEText(send_text, "plain", "utf-8")
  (content)
  # Document type
  if isinstance(file_names, list):
    for file_name in file_names:
      send_file_path = file_dir + "/" + file_name
      part = MIMEApplication(open(send_file_path, 'rb').read())
      part.add_header('Content-Disposition', 'attachment', filename=file_name)
      (part)
  elif isinstance(file_names, str):
    send_file_path = file_dir + "/" + file_names
    part = MIMEApplication(open(send_file_path, 'rb').read())
    part.add_header('Content-Disposition', 'attachment', filename=file_names)
    (part)
  # msg['from'],msg['to'] Sender and recipient displayed on the receiving end
  msg['from'] = "Obama@"
  msg['to'] = account
  msg['subject'] = email_title
  try:
    server = ()
    ('smtp.')
    (account, password)
    # from_addr:send_address; to_addrs:receive_address (list of strings)
    (account, msg['to'].split(), msg.as_string())
  except Exception as e:
    print(e)
# Get email headers
def get_email_subject(addr, password):
  # Set up the connection URL, get the pop3 protocol mail reading object.
  read = poplib.POP3('pop.', timeout=3600)
  # Enter email address and email login password
  (addr) # 163 email username
  read.pass_(password) # Client authorization password in 163 mailbox settings
  # allEmails = (totalNum, totalSize)
  # Read mail messages (total number of messages, size of messages)
  total_num, total_size = ()
  # top(which,howmuch)
  # Get the latest email (how many emails, how many to get)
  top_email = (total_num, 1)
  # print("***** start *****\n received data as: {}\n***** end *****\n".format(top_email))
  #
  # print("Initial email content obtained by ***** start *****\n[before decoding]: {}\n***** end *****\n".format(top_email[1]))
  # Decode mail messages, store decoded mail messages to tmp
  tmp = []
  for s in top_email[1]:
    (())
  # print("***** start *****\n [decoded] email content is: {}\n***** end *****\n".format(tmp))
  # Splicing decoded email content into a string
  email_str = '\n'.join(tmp)
  # Parsing string types into Message types
  message = email.message_from_string(email_str)
  # print("***** start *****\n"
  # "[Before decoding] the content of the mail string is: [data type]{}\n{}\n"
  #    "--------------------------------------------\n"
  # "[Decoded] email string content is: [data type]{}\n{}\n"
  #    "***** end *****\n"
  #    .format(type(email_str), email_str, type(message), message))
  # Get the subject of the email
  subject_str = message['subject']
  # print("***** start *****\n[before decoding] email title: {}\n***** end *****\n".format(subject_str))
  subject = decode_header(subject_str)
  # print("***** start *****\n[decoded] email subject: {}\n***** end *****\n".format(subject))
  content = subject[0][0]
  enc_type = subject[0][1]
  if enc_type:
    subject_decode = (enc_type)
  else:
    subject_decode = content
  return subject_decode, read, total_num

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more. If there is any mistake or something that has not been fully considered, please do not hesitate to give me advice.