I have written a python script to send mail using smtplib.
Everything is working fine except, when the user receives the mail, he isn't able to see my google account name, instead he is getting the mail id. is there a way to make this right?
I guess it's got to do something here but am not sure.
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = ','.join(recipients_addr)
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
return msg
The first image is the one sent with smtplib. There is no username.
The second one is sent normally from gmail.
You need to provide both the email address and the account name to do this.
The email.utils.formataddr function can be used to format the data:
>>> from email import utils
>>> utils.formataddr(('Jane Smith', 'jsmith#example.com'))
'Jane Smith <jsmith#example.com>'
In the code the question, you would do something like
from_addr = 'praneeth.vasarla#example.com'
from name = 'Praneeth Vasarla'
msg['from'] = formataddr((from_name, from_addr))
Related
I am writing a python script that sends every day a bulk email (connected to an outlook.com account) with the body of a news website as an ebook to multiple kindle accounts (that have already whitelisted this outlook account).
This was working so far, but since yesterday the kindle devices couldn't get the ebooks. I've tried a lot and I found out, that the problem is because all the sent emails have the receiver in the BCC part of the email. For some reason since yesterday if the receiver is on BCC, kindle doesn't receive the email (or process it).
So I am asking, what should I change on my code so I could have the receiver addresses on the [TO] part of the email instead of the [BCC]. I don't mind if it's a single email with multiple addresses or multiple single emails, as far as the receiver is not on the BCC.
receiver_email = ["email1#mail.com", "email2#mail.com", "email3#mail.com", "email4#mail.com", "email5#mail.com"]
subj = "Issue - "+fullDate
msg = MIMEMultipart('alternative')
msg['Subject'] = subj
msg['From'] = sender_email
msg.attach(MIMEText('Forward this email to your kindle account'))
# Attach the .mobi file
print("Attaching "+epubname)
fp = open(epubname, "rb")
epub_file = MIMEApplication(fp.read())
fp.close()
encoders.encode_base64(epub_file)
epub_file.add_header('Content-Disposition', 'attachment', filename=epubname)
msg.attach(epub_file)
debug = False
if debug:
print(msg.as_string())
else:
server = smtplib.SMTP('smtp.office365.com',587)
server.ehlo()
server.starttls()
server.login("##EMAIL##", "##PASSWORD##")
text = msg.as_string()+ "\r\n" + message_text
for x in range(len(receiver_email)):
email_to = receiver_email[x]
msg['To'] = email_to #msg['To'] = ", ".join(email_to)
server.sendmail(sender_email, email_to, text.encode('utf-8'))
server.quit()
I've found this question, but with no specific answer unfortunately.
Your code seems to be written for Python 3.5 or earlier. The email library was overhauled in 3.6 and is now quite a bit more versatile and logical. Probably throw away what you have and start over with the examples from the email documentation.
The simple and obvious solution is to put all the recipients in msg["to"] instead of msg["bcc"].
Here is a refactoring for Python 3.3+ (the new API was informally introduced already in 3.3).
from email.message import EmailMessage
receiver_email = ["email1#mail.com", "email2#mail.com", "email3#mail.com", "email4#mail.com", "email5#mail.com"]
subj = "Issue - "+fullDate
msg = EmailMessage()
msg['Subject'] = subj
msg['From'] = sender_email
msg['To'] = ", ".join(receiver_email)
msg.set_content('Forward this email to your Kindle account')
with open(epubname, "rb") as fp:
msg.add_attachment(fp.read()) # XXX TODO: add proper MIME type?
debug = False
if debug:
print(msg.as_string())
else:
with smtplib.SMTP('smtp.office365.com',587) as server:
server.ehlo()
server.starttls()
server.login("##EMAIL##", "##PASSWORD##")
server.send_message(msg)
This sends a single message to all the recipients; this means they can see each others' email addresses if they view the raw message. You can avoid that by going back to looping over the recipients and sending one message for each, but you really want to avoid that if you can.
As an aside, you don't need range if you don't care where you are in the list. The idiomatic way to loop over a list in Python is simply
for email_to in receiver_email:
...
New to Python, I'm trying to write a mass-email script (it will be sent via gmail).
Basically I have got it working, however I need to edit each message body separately with a greeting to name of the e-mail recipient. Currently the body is read off a body.txt file and I am not sure how to edit it. Example:
body.txt
Hi <recipient>,
Here is your voucher code.
And this is my script, send_email.py:
import smtplib
from email.mime.text import MIMEText
from_address = "xyz#gmail.com"
file = open("body.txt","rb")
message = MIMEText(file.read())
file.close()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(from_address, "XXXXX")
server.set_debuglevel(1)
sender = from_address
recipients = ['abc#gmail.com', 'def#gmail.com']
message['Subject'] = "Voucher Code"
message['From'] = sender
message['To'] = ", ".join(recipients)
server.sendmail(sender, recipients, message.as_string())
I think most probably I will have to create another list with the names of recipients and match them to the email addresses to be sent to, but I'm not sure how to go from there. In fact, I'm not even sure this is the best way- it works though, so I'm pretty happy so far, apart from not being able to edit the body. How do I go from here?
I've seen what you did and I think it's a great start. To edit in the recipients name to the message is really quite simple. You need to format the string. Here's an example:
emailBody = "Hello {}, this is my email body. As you can see this program inserts every recipient separately. Hopefully it is helpful. \n"
recipients = ["Ann", "Josh", "Bob", "Marie"]
for recipient in recipients:
print(emailBody.format(recipient))
The main thing is that your emailBody must include symbols {} and then you can use
emailBody.format(content)
to put something into those brackets.
how can I set the sender in email in both yahoo and gmail.
msg = MIMEMultipart('alternative')
msg['Subject'] = "New Updates"
msg['From'] = "Unirises Updates!"
msg['To'] = client_mail
html_updates = """<h1>This is automated Email, Test purpose only!</h1>"""
letter = MIMEText(html_letter, "html")
msg.attach(letter)
# Then the rest of code is continue here
But in the inbox
I want to inform the user that the email is came from my email
example (no-reply#unirises.test)
You only provided how you compose the message, while SMTP.sendmail which you probably uses later, takes from_addr email as first argument - check sendmail documentation.
Usually the email sending looks like this:
smtp = smtplib.SMTP(host, port)
smtp.sendmail(from_address_here, to_address_here, msg.as_string())
While what you set as msg["From"] is more of a display name.
As per https://docs.python.org/3.4/library/email-examples.html you can do something like this:
msg['From'] = 'Foo Bar <foo.bar#gmail.com>'
I've got a script that gets some data from my website and emails me once a day. I'm trying to get this to send to more than one recipient, I've adapted the script with some code that I've found on here (in more than one solution) but I'm finding that it only sends an email to the first recipient on the list.
Here's an excerpt of what I'm using (bear in mind that the full version works 100% correctly when sending to one recipient)...
addr_to = ['me#icloud.com', 'me2#icloud.com']
addr_from = 'darren#website.co.uk'
smtp_server = 'mail.com'
smtp_user = 'darren#website.co.uk'
smtp_pass = 'password'
msg = MIMEMultipart('alternative')
msg['To'] = " ,".join(addr_to)
msg['From'] = addr_from
msg['Subject'] = " Automated email"
When I send this to two of my own email addresses or if I put the same email address in twice, I only receive one email - the received email shows both email addresses in the 'to' field.
How do I get this to work properly?
msg['To'] needs to be a string while the recipients in sendmail(sender, recipients, message) needs to be a list:
s = smtplib.SMTP('servername')
addr_to = ['me#icloud.com', 'me2#icloud.com']
addr_from = 'darren#website.co.uk'
msg = MIMEMultipart('alternative')
msg['Subject'] = "Automated email"
msg['From'] = addr_from
msg['To'] = ", ".join(addr_to)
s.sendmail(addr_from, addr_to, "bla")
I'd suggest to have a look at yagmail.
To send to multiple email addresses you can use the following:
import yagmail
yag = yagmail.SMTP()
yag.send(['me#icloud.com', 'me2#icloud.com'], "subject", "contents")
Problem solved!!
USER ERROR!
Both email addresses were coming into my iCloud account, for some reason, my iPad chooses to only show me one email....not one thread with two identical emails in it, just one email!
I tried again with one of the email addresses going to a completely separate account and it works fine!
I've written a python script to send out emails, but now I'm wondering if it's possible to send emails to Microsoft exchange groups using python? I've tried including the group in the cc and to fields but that doesn't seem to do the trick. It shows up, but doesn't seem to correspond to a group of emails; it's just plain text.
Anyone know if this is possible?
This is definitely possible. Your exchange server should recognize it if you treat it as a full address. For example if you want to send it to person1, person2, and group3, use the following:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
address_book = ['person1#company.com', 'person2#company.com', 'group3#company.com']
msg = MIMEMultipart()
sender = 'me#company.com'
subject = "My subject"
body = "This is my email body"
msg['From'] = sender
msg['To'] = ','.join(address_book)
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
text=msg.as_string()
#print text
# Send the message via our SMTP server
s = smtplib.SMTP('our.exchangeserver.com')
s.sendmail(sender,address_book, text)
s.quit()