add sender's name in the from field of the email in python

后端 未结 6 1559
忘掉有多难
忘掉有多难 2020-12-31 12:17

I am trying to send email with below code.

import smtplib
from email.mime.text import MIMEText

sender = \'sender@sender.com\'

def mail_me(cont, receiver):
         


        
6条回答
  •  渐次进展
    2020-12-31 12:29

    This should work:

    msg['From'] = "Your name "
    

    Example below:

    import smtplib
    from email.mime.text import MIMEText
    
    def send_email(to=['example@example.com'], f_host='example.example.com', 
    f_port=587, f_user='example@example.com', f_passwd='example-pass', 
    subject='default subject', message='content message'):
    smtpserver = smtplib.SMTP(f_host, f_port)
    smtpserver.ehlo()
    smtpserver.starttls()
    smtpserver.ehlo
    smtpserver.login(f_user, f_passwd) # from email credential
    msg = MIMEText(message, 'html')
    msg['Subject'] = 'My custom Subject'
    msg['From'] = "Your name "
    msg['To'] = ','.join(to)
    for t in to:
        smtpserver.sendmail(f_user, t, msg.as_string())  # you just need to add 
    this in for loop in your code.
        smtpserver.close()
    print('Mail is sent successfully!!')
    
    
    cont = """\
    
     
     
       

    Hi!
    How are you?
    Here is the link you wanted.

    """ try: send_email(message=cont) except: print('Mail could not be sent')

提交回复
热议问题