Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Python sending a html email with a gif, prefering plain text alternative over html.
Forum Updated to NodeBB v4.3 + New Features

Python sending a html email with a gif, prefering plain text alternative over html.

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 4.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    swilliams2
    wrote on 23 May 2013, 09:24 last edited by
    #1

    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 MIMEImage

    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)
    [/code]

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 23 May 2013, 09:48 last edited by
      #2

      Hi,

      This is not Qt related, you'd better post on a Python forum

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        swilliams2
        wrote on 23 May 2013, 10:01 last edited by
        #3

        [code]
        from PyQt4 import QtGui, QtCore

        import smtplib
        import smtplib
        from email.mime.multipart import MIMEMultipart
        from email.mime.text import MIMEText
        from email.mime.image import MIMEImage

        from 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 = """\
            &lt;html&gt;
              &lt;head&gt;Test Header&lt;/head&gt;
              &lt;body&gt;
                <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>
              &lt;/body&gt;
            &lt;/html&gt;
            """
            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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 23 May 2013, 10:34 last edited by
          #4

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            swilliams2
            wrote on 23 May 2013, 10:37 last edited by
            #5

            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 :/

            1 Reply Last reply
            0

            1/5

            23 May 2013, 09:24

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved