0

I am trying to attach a link to my local file while sending an email using python smtplib.

msg.attach(MIMEText(u'<a href="file:///C:\folder\file.txt">Link</a>', '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>
<a href="file:///C:\folder\file.txt">Link</a>
</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

ampat
  • 43
  • 10
  • What are you using to send mail? Could you post a code snippet here? – skaul05 Feb 22 '19 at 16:44
  • I've edited the post – ampat Feb 22 '19 at 17:01
  • You don't give valid `HTML` to `MIMEText`. Add `...`.The parameter `MIMEText(..., 'html')` is only used in the header of `MIMEText`. – stovfl Feb 22 '19 at 17:05
  • @stovfl I tried including it this way. It doesn't work. `html_text += '' + '\n' html_text += 'Link' + '\n' html_text += '' + '\n' msg.attach(MIMEText(html_text, 'html'))` – ampat Feb 22 '19 at 17:21
  • @ampat: Refer to [sending-html-email-using-python](https://stackoverflow.com/questions/882712/sending-html-email-using-python#) – stovfl Feb 22 '19 at 18:13

1 Answers1

2

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.

Hope this answers your question!!!

skaul05
  • 2,154
  • 3
  • 16
  • 26