I am trying to attach a link to my local file while sending an email using python smtplib.
msg.attach(MIMEText(u'Link', 'html'))
But it just comes as plain text in the email.
If I just use the link in a html page the link works.
<html>
Link
</html>
How do I solve this?
Edit:
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
....
....
....
msg = MIMEMultipart()
msg['From'] = self.username
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(text))
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(self.username, self.password)
mailServer.sendmail(self.username, to, msg.as_string())
My code snippet of sending email is very similar to this
Gmail does not support local network URL as of now. So that's why your link is not getting rendered in your mail.
You can use various alternative approaches, mentioned below in this link.
https://webapps.stackexchange.com/questions/17269/put-a-link-to-a-network-share-in-a-gmail-message
Hope this answers your question!!!
Related
I've just joined a project and have been trying to figure why some emails are showing up as they should on Gmail but when I open then with a client like Thunderbird or Outlook the attached PDFs do now show up.
As an additional detail if I forward the mail from Thunderbird/Tutlook to a Gmail account the attached pdf will appear and if I send it back to the Thunderbird/Outlook - connected account it will again appear, so Gmail fixes something that's wrong in the code.
This is my second time dealing with emails, the first being mostly copying some code to see if it works, so if you're willing to explain it to me in addition to helping me fix it, I'd appreciate it greatly.
I'll post the code here and then add some additional information on what I tried below, to keep this a bit cleaner.
So, here's the code:
from email.message import EmailMessage
from email import encoders
from email.mime.base import MIMEBase
import smtplib
msg = EmailMessage()
msg['Subject'] = subject
msg['From'] = "someaddress"
msg['To'] = user_email
msg.set_content('Certification')
msg.add_alternative("""<!DOCTYPE html>
<html>
<body>
Stuff...
</body>
</html>
""", subtype='html')
filename = 'somefilename'
pdf_filename = 'certificate.pdf'
with open(filename, "rb") as attachment:
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
"attachment", filename=pdf_filename
)
try:
msg.attach(part)
with smtplib.SMTP('someIP', port) as smtp:
smtp.send_message(msg)
I've seen some similar issues (or actually the same issue) before, and some solutions suggested using a MIMEMultipart-mixed message object instead of an email one like this:
msg=MIMEMultipart('mixed') instead of msg = EmailMessage() but because add_alternative is not a MIMEMultipart method, I cannot do that without changing that part.
(The above was suggested on this post - which is exactly the same issue as mine, but unfortunately I could not use the same fix)
What I tried doing was calling a
msg.make_mixed() below msg=Email.Message() hoping that the mixed type (I don't understand email types yet) would solve the problem, but when I tried sending an email that way I got a Status=4, Failed to send file, error.
I would also really appreciate if you could offer any suggestion for a resource to learn some more about sending and receiving emails (from a python perspective).
Thank you!
The problem here is that you have directly attached a pre-built MIME part to a multipart/alternative message. It ends in an incorrect message that may be poorly processed by mail readers.
In the email.message interface, you should use the add_attachement method. It will handle the base64 encoding and will change the message into a multipart/mixed one:
with open(filename, "rb") as attachment:
msg.add_attachment(attachment.read(), maintype='application',
subtype='octet-stream', filename=pdf_filename)
try:
with smtplib.SMTP('someIP', port) as smtp:
smtp.send_message(msg)
I'm trying to generate a mail in Python in order to send it from Outlook. This is quite easy and no problem.
However, I want to add a feature to the code, I want to show a preview of the mail before sending (and, if possible, be able to modify it).
How can I do it? I've looked for solutions, all of them include other libraries I'm not used to and I don't know how to adapt my code.
import smtplib
from email.mime.multipart import MIMEMultipart
logIN = smtplib.SMTP('smtp-mail.outlook.com', 587)
logIN.starttls()
logIN.login("me#example.com", "Password")
msg = MIMEMultipart()
msg['From'] = "me#example.com"
msg['To'] = "you#example.com"
msg['Subject'] = "Lorem ipsum"
text = msg.as_string()
#Here the preview
logIN.sendmail("me#example.com", "you#example.com", text)
Thanks in advance
I want to send an email to multiple users. Right now, I am only able to send it to one user. I want to make a property file so that whenever I need to add or remove any user from the list, I dont have to edit anything in .py file.
I am able to send the email to one user
import smtplib
import email.utils
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
me = abc#an.com
you = jud#gm.com
msg = MIMEMultipart('alternative')
msg['Subject']="subject of the email"
msg['From"]=me
msg['To']=you
text="hi"
html= '''body of email'''
part1=MIMEText(text, 'plain')
part1=MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
s = smtplib.SMTP(host,port)
s.sendmail(me,you, msg.as_string())
s.quit()
the email should go to multiple users.
The argument you can be a list of email addresses. You will need to adapt your code to set the To: header to accept a list of strings
msg['To'] = '.'.join(you)
or you can use a placeholder like
msg['To'] = 'undisclosed-recipients:;'
but other than that, your existing code should just work.
In some more detail, the latter is basically equivalent to putting all the addresses in a Bcc: header, so that the recipients cannot see each others' addresses.
To populate you from a file, try
with open(filename) as f:
you = f.readlines()
where the file contains one email address per line.
I have a python script that sends out emails. The emails are being sent out with no issues.
However, when I get the mails and open them up, their "to:" field do not show the recipients. It's just blank. I want to show all the recipients. And since I'm not using any "bcc" configuration in my script, I'm befuddled as to how this could be.
Here's the code I'm using:
#!/usr/bin/env python
you = [ "MyEmail1#MyEmail.com", "MyEmail2#MyEmail.com" ]
for eachrecord in fformatErrMessage:
# Preparing all variables #
msg = MIMEMultipart()
msg['Subject'] = subjectMsg
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
s = smtplib.SMTP('localhost')
s.sendmail(me, you, msg.as_string())
s.quit()
I have tried replacing the you with:
you = "MyEmail1#MyEmail.com"
thinking maybe the list is the problem - but that did not work.
When sending emails, servers do not look at the To:-Field. Instead, email servers have a separate way of transmitting recipient addresses, sometimes called the "envelope", which they use to route and deliver mail. The client never receives that envelope, it instead uses the To:-field in the message headers.
The sendmail() call simply sets the addresses of the SMTP envelope. If you want the To:-field to show up in the mail itself, you must set the appropriate header:
msg['To'] = "foo#bar.com"
This is, btw, how BCC works: The addresses on the envelope is simply not repeated in the message headers.
I am using flask python to send emails using sendgrid, i followed the doc, where i install pip install sendgrid and did the following, but the email is not sent and responds with a status code of 202, is there something i am missing
import sendgrid
from sendgrid.helpers.mail import Email, Content, Mail
def sendEmail():
try:
sg = sendgrid.SendGridAPIClient(apikey="hiddedAPIKey")
from_email = Email("test#example.com")
to_email = Email("myemail#gmail.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
except:
print("the was an error")
Your emails must be going to spam. There are two ways to tackle this situation
You can go into the spam folder and click on the from email address as the trusted email address. (Won't help for multiple sender addresses)
Check the sendgrid official documentation to avoid emails to land into spam.