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
Related
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")
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)
I am trying to write a function that send an email with an attached file. At the moment, it send an email but without attachment. Can you someone comment?
msg = MIMEMultipart()
msg['From'] = my email
msg['To'] = client email address
msg['Subject'] = subject
body = content
msg.attach(MIMEText(body, 'plain'))
##### Load the address of the file which should be attached*********************************
filename = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("Text
files","*.txt"),("all files","*.*")))
Myfile = open(filename)
attachment = MIMEText(Myfile.read())
attachment.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(attachment)
mail = smtplib.SMTP('smtp.gmail.com', 587)
msg = f'Subject: {subject}\n\n{content}'
mail.ehlo()
mail.starttls()
mail.login('My email address', 'password')
mail.sendmail('client email', My email address, msg)
mail.close()
Thank you all in advance
First, as mentioned in my comment, you're overwriting msg here:
msg = f'Subject: {subject}\n\n{content}'
At this point, the MIMEMultipart object msg used to point to is destroyed, and you'll send only that new string. No wonder why there's no attachment: this string obviously doesn't have an attachment.
Now, you should really be using the new API of the email module, as shown in the documentation. But since you're already using the legacy API (the MIMEMultipart class, for example), you'll need to convert msg to a string, as shown here:
# This is copied straight from the last link
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you
...
part1 = MIMEText(text, 'plain') # example attachment
# 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)
mail.sendmail(
me, you,
msg.as_string() # CONVERT TO STRING
)
I am working on a project with a raspberry pi 3 which makes a photo when something is moving. Now I want to send an email with this photo as an attachement.
I already have a code but it tooks 30 seconds to send the mail.
The code I have so far is:
msg = MIMEMultipart()
msg['Subject'] = 'Bewegung erkannt!'+' '+timestr
msg['From'] = 'surveillance.picam#gmail.com'
msg['To'] = 'fuchsfan23#gmail.com'
body = email.mime.Text.MIMEText("""Bewegung erkannt! Hier ist der Eindrinling!""")
msg.attach(body)
directory = '/home/pi/Pictures/RaspCam/bild_{}.jpg'.format(timestr)
spl_dir=directory.split('/')
filename=spl_dir[len(spl_dir)-1]
spl_type=directory.split('.')
type=spl_type[len(spl_type)-1]
fp=open(directory,'rb')
att = email.mime.application.MIMEApplication(fp.read(),_subtype=type)
fp.close()
att.add_header('Content-Disposition','attachment',filename=filename)
msg.attach(att)
s = smtplib.SMTP('smtp.gmail.com:587')
s.starttls()
s.login('mailaddress1','passwd')
s.sendmail('mailaddress1','mailaddress2', msg.as_string())
s.quit()
This code is within an callback which makes the photo when something is moving and saves it.
I hope someone can help me...
CrypticEagle
I am sending multiple recipients mail with a python script, and it works, but in the "To" in the mail, the field is empty. On every mail provider I tried.
Here is my code :
def send_mail(self, server, send_from, subject, attach, foo=None, bar=None)
msg = MIMEMultipart()
msg['From'] = send_from
msg['Subject'] = subject
body = u"""<html> My Body </html>"""
#Attach file pdf
msg.attach(MIMEText(body, 'html', 'utf-8'))
filename = 'fname.pdf'
attachment = open(attach, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename = %s" % filename)
msg.attach(part)
text = msg.as_string()
if foo != None and bar != None:
recipients = ["a#b.fr", "b#b.fr"]
msg['To'] = ", ".join(recipients)
server.sendmail(send_from, recipients, text)
else:
msg['To'] = "c#b.fr"
server.sendmail(send_from, msg['To'], text)
sleep(1)
msg['To'] should be the "To" in my mail but it stays empty. (Even in the "else" with only one recipient). I can't find any help about it on existing question, so I come to ask for your help about that.
As per the documentation, the second parameter is a LIST of recipients. You need to change your line
server.sendmail(send_from, msg['To'], text)
to
server.sendmail(send_from, [msg['To']], text)
Finally found out:
text = msg.as_string() should be done after the assignement to msg['To'] So it will appear in my if and else block juste before the server.sendmail(send_from, msg['To'], text) since all information written in the mail, even 'To', and 'From' are in the third argument text .