Reply All to emails using python Imap4 - python

I have a working code that replies to email from a specific inbox.
msg = EmailMessage()
msg['In-Reply-To'] = orig_mail['message_id']
msg['From'] = sender
msg['To'] = recipient
msg['Subject'] = subject
I need to Reply All to that email instead of just Reply .
Is there any way i can implement that here? Any help would be appreciated, Thanks.

I don't think Reply ALL feature is there. But this would work out if you want to send mail to multiple people
recipients = ['john.doe#example.com', 'john.smith#example.co.uk']
msg['From'] = sender
msg['Subject'] = subject
msg['To'] = ", ".join(recipients)

Related

Embedding image in HTML email using Python

I have been trying to embed images to an email using MIMEImage but even after replicating examples I find online, I keep having the same issue...
Here is the part of the HTML that embeds the image:
<img width=779 height=467 style='width:8.1145in;height:4.8645in' src="cid:image001" alt="HRX Trend">
This is my Python code:
msg = MIMEMultipart('alternative')
msg['Subject'] = "test for daily email"
msg['From'] = me
msg['To'] = you
fp = open('path\\name.jpeg', 'rb')
msgImage = MIMEImage(fp.read())
msgImage.add_header('Content-ID', '<image001>')
part2 = MIMEText(html, 'html')
msg.attach(part2)
msg.attach(msgImage)
s = smtplib.SMTP('domain', port)
s.sendmail(me, you, msg.as_string())
s.quit()
But, when the email sends, I keep getting the error saying:
The linked image cannot be displayed. The file may have been moved, renamed, or deleted.
Screenshot of what the email ends up looking like:
I have no idea what I am doing wrong...
Thank you!
I updated the message container and the issue was resolved:
Original:
msg = MIMEMultipart('alternative')
msg['Subject'] = "test for daily email"
msg['From'] = me
msg['To'] = you
New:
msg = MIMEMultipart()
msg['Subject'] = "test for daily email"
msg['From'] = me
msg['To'] = you

How to attach email to another email with end result where top level mail content-type message/rfc822 in python 3?

I had looked into email lib of python but did not find any reference where it suggests way to attach mail to mail with content-type message/rfc822.
#Message to be attached to new mail
parsed_message = email.message_from_string(str(desearilized_message))
msg = MIMEMultipart()
msg['From'] = "somesender#send.me"
msg['To'] = "somereciver#recive.me"
msg['Subject'] = "Subject of the Mail"
body = "Body_of_the_mail"
msg.attach(parsed_message)
This results in parent mail with Content-Type: multipart and not message/rfc822.
Any reference/suggestion of how to achieve it in python3?
This works for me.
msg = EmailMessage()
msg.__setitem__("Content-type","message/rfc822")

Set Sender in sending Email

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>'

How to add sender name before sender address in python email script

Here's my code
# Import smtplib to provide email functions
import smtplib
# Import the email modules
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Define email addresses to use
addr_to = 'user#outlook.com'
addr_from = 'user#aol.com'
# Define SMTP email server details
smtp_server = 'smtp.aol.com'
smtp_user = 'user#aol.com'
smtp_pass = 'pass'
# Construct email
msg = MIMEMultipart('alternative')
msg['To'] = addr_to
msg['From'] = addr_from
msg['Subject'] = 'test test test!'
# Create the body of the message (a plain-text and an HTML version).
text = "This is a test message.\nText and html."
html = """\
"""
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
# Send the message via an SMTP server
s = smtplib.SMTP(smtp_server)
s.login(smtp_user,smtp_pass)
s.sendmail(addr_from, addr_to, msg.as_string())
s.quit()
I just want the email received to display the sender name before sender email address like this : sender_name
In the year 2020 and Python 3, you do things like this:
from email.utils import formataddr
from email.message import EmailMessage
import smtplib
msg = EmailMessage()
msg['From'] = formataddr(('Example Sender Name', 'john#example.com'))
msg['To'] = formataddr(('Example Recipient Name', 'jack#example.org'))
msg.set_content('Lorem Ipsum')
with smtplib.SMTP('localhost') as s:
s.send_message(msg)
It depends on whether the "friendly name" is basic ASCII or requires special characters.
Basic example:
msg['From'] = str(Header('Magnus Eisengrim <meisen99#gmail.com>'))
If you need to use non US-ASCII characters, it's more complex, but the attached article should help, it is very thorough: http://blog.magiksys.net/generate-and-send-mail-with-python-tutorial
This is an old question - however, I faced the same problem and came up with the following:
msg['From'] = formataddr((str(Header('Someone Somewhere', 'utf-8')), 'xxxxx#gmail.com'))
You'll need to import from email.header import Header and from email.utils import formataddr.
That would make only the sender name appear on the inbox, without the <xxxxx#gmail.com>:
While the email body would include the full pattern:
Putting the sender name and the email in one string (Sender Name <sender#server.com>) would make some email clients show the it accordingly on the receiver's inbox (unlike the first picture, showing only the name).
I took the built-in example and made it with this:
mail_body = "the email body"
mailing_list = ["user1#company.com"]
msg = MIMEText(mail_body)
me = 'John Cena <mail#company.com>'
you = mailing_list
msg['Subject'] = subject
msg['From'] = me
msg['To'] = mailing_list
# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()
I found that if I send an email with gmail and set the From header to sender name <email#gmail.com>, the email arrives with the From like:
From sender name email#gmail.com email#gmail.com.
So I guess at least with gmail you should set the From header like as follow:
msg['From'] = "sender name"
You can use below mentioned code, you just need to change sender and receiver with user name and password, it will work for you.
import smtplib
sender = 'xyz#gmail.com'
receivers = ['abc#gmail.com']
message = """From: sender_name <xyz#gmail.com>
To: reciever_name <abc#gmail.com>
Subject: sample test mail
This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP('smtp_server',port)
smtpObj.sendmail(sender, receivers, message)
smtpObj.login(user,password)
print ("Successfully sent email")
except:
print ("Error: unable to send email")
for more detail please visit https://www.datadivein.com/2018/03/how-to-auto-send-mail-using-python.html

Python: can't get email to send to multiple recipients

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!

Categories

Resources