UltraRadiantx
UltraRadiantx

Reputation: 67

Changing the display name of sender mail id through python script

import smtplib
import os

EMAIL_ADDRESS = [email protected]
EMAIL_PASSWORD = os.environ.get('EMAIL_PASS')

with smtplib.SMTP('smtp.gmail.com',587) as smtp:
    smtp.ehlo()  #identifies ourselves with the mail server that we are using
    smtp.starttls() #to encrypt the traffic
    smtp.ehlo() #to re-identify ourselves as encrypted connection

    smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)

    subject = "Regarding your text role"
    body = "This is to inform u that ur text role has been updated"

    msg = f'Subject: {subject}\n\n{body}'

    smtp.sendmail('Random User <[email protected]>','[email protected]', msg)

Even though code is getting successfully executed, I'm not getting the display name as 'Random User' in [email protected]
What may be the problem??

Upvotes: 1

Views: 802

Answers (1)

&#233;tale-cohomology
&#233;tale-cohomology

Reputation: 1861

You don't wanna change the from in smtp.sendmail(), but you want to change the email content itself.

msg = 'From: Random User <[email protected]>\nTo: [email protected]\nSubject: subject\n\nbody'
smtp.sendmail('[email protected]','[email protected]', msg)

Upvotes: 2

Related Questions