Python sending a html email with a gif, prefering plain text alternative over html.
-
This script seems to work, but it always sends the plain text email with the gif as an attachment, rather than the html email with the gif inside it, does anyone know why?
[code]
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImageme = "ajames@brecon-hs.powys.sch.uk"
you = "swilliams@brecon-hs.powys.sch.uk"msg = MIMEMultipart('related')
msg['Subject'] = 'Test Message'
msg['From'] = me
msg['To'] = you
msg.preamble = 'This is a super cool multipart message using html and python c:'msgAlt = MIMEMultipart('alternative')
msg.attach(msgAlt)msgTxt = MIMEText('You see this if it fails')
msgAlt.attach(msgTxt)thehtmlcode = """
<html>
<head>Test Header</head>
<body>
<p>This isn't plain text!<br>
If you're seeing this everything should be working as planned :D<br>
Here's the gif <a href="http://tinypic.com?ref=2wpnfnt" target="_blank"><img src="http://i43.tinypic.com/2wpnfnt.gif" border="0" alt="Image and video hosting by TinyPic"></a>
</p>
</body>
</html>
"""
msgTxt = MIMEText(thehtmlcode, msgAlt.attach(msgTxt))fp = open('coffee.gif', 'rb')
msgImg = MIMEImage(fp.read())
fp.close()msgImg.add_header('EMEGRENCY thing', '<Itsapicture>')
msg.attach(msgImg)s = smtplib.SMTP('BHS-MAIL')
s.send_message(msg)
[/code] -
Hi,
This is not Qt related, you'd better post on a Python forum
-
[code]
from PyQt4 import QtGui, QtCoreimport smtplib
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImagefrom email.mime.text import MIMEText
class Window(QtGui.QWidget):
def init(self):
QtGui.QWidget.init(self)
self.button = QtGui.QPushButton('', self)
self.button.clicked.connect(self.handleButton)
self.button.setIcon(QtGui.QIcon("./z.jpg"))
self.button.setIconSize(QtCore.QSize(300,250))
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.button)def handleButton(self): me = "ajames@brecon-hs.powys.sch.uk" you = "swilliams@brecon-hs.powys.sch.uk" msg = MIMEMultipart('related') msg['Subject'] = 'Test Message' msg['From'] = me msg['To'] = you msg.preamble = 'This is a super cool multipart message using html and python c:' msgAlt = MIMEMultipart('alternative') msg.attach(msgAlt) msgTxt = MIMEText('You see this if it fails') msgAlt.attach(msgTxt) thehtmlcode = """\ <html> <head>Test Header</head> <body> <p>This isn't plain text!<br> If you're seeing this everything should be working as planned :D<br> Here's the gif <a href="http://tinypic.com?ref=2wpnfnt" target="_blank"><img src="http://i43.tinypic.com/2wpnfnt.gif" border="0" alt="Image and video hosting by TinyPic"></a> </p> </body> </html> """ msgTxt = MIMEText(thehtmlcode, msgAlt.attach(msgTxt)) fp = open('coffee.gif', 'rb') msgImg = MIMEImage(fp.read()) fp.close() msgImg.add_header('EMEGRENCY thing', '<Itsapicture>') msg.attach(msgImg) s = smtplib.SMTP('BHS-MAIL') s.send_message(msg)
if name == 'main':
import sys app = QtGui.QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
[/code]
Is that better? =P
-
The fact that you use Qt for the GUI doesn't change the fact that you are using Python's mail library which makes your problem not Qt related.
A shot in the dark for your problem, are you sure the problem is on the sending side and not on the receiving ?
-
I know, sorry, my sense of humor is abit strange =]
I would've posted it in python-forum.org, but it keeps timing out for me today :/
Yeah, I've tested it with various different things, and other emails with plain text alternatives seem to be going through :/