Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6627155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:58:48+00:00 2026-05-25T21:58:48+00:00

The following code results in a htmlbody,emailaddress = ConnEmailParser() TypeError: ‘NoneType’ object is not

  • 0

The following code results in a

htmlbody,emailaddress = ConnEmailParser()
TypeError: 'NoneType' object is not iterable 

Error but works fine in IDE. Emails are being sent and PDF files generated. But from command line just ConnEmailParser() is working.

What can I do, that the program runs smoothly?

Here the code snipped:

import imaplib
import email
import quopri
from cStringIO import StringIO
import ho.pisa as pisa
from datetime import datetime
import logging
import smtplib
from email.generator import Generator
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import formatdate
from email import Encoders

def sendPdfSurvey():

  def ConnEmailParser():
    try:
        saver = StringIO()
        SERVER = "server"
        USER  = "user"
        PASSWORD = "nono"
        subject="a certain phrase"
        imap4 = imaplib.IMAP4(SERVER)
        imap4.login(USER, PASSWORD)
        imap4.select()
        imap4.search(None, 'ALL') 
        typ, data = imap4.search(None,'(UNSEEN SUBJECT "%s")' % subject) #looking for phrase
        count = 0
        for num in data[0].split():
            count = count + 1 # this is stupid but it gets just oone messsage
            if count ==1 :
                typ, data = imap4.fetch(num,'(RFC822)')
                email7 = email.message_from_string(data[0][1])
                varSubject = email7['subject']
                # The phase from above has an emailaddress in the subject field
                emailaddressraw = varSubject[13:] 
                emailaddressmedium = emailaddressraw.replace("<","")
                emailaddress= emailaddressmedium.replace(">","")  
                msg = email.message_from_string(data[0][1])
                typ, data = imap4.store(num,'+FLAGS','\Seen')
                a=str(msg)
                i= a.decode('quopri').decode('utf-8')
                saver.write(i)
                savercontent = saver.getvalue()
                # body of the email is html and all of the header elements get deleted
                planhtml = savercontent.split('<html xmlns="http://www.w3.org/1999/xhtml">')[1].strip()
                return planhtml, emailaddress.strip()     
        saver.close()
        imap4.close()
        imap4.logout()
    except:
        "could not get participant email!"

#here somewhere is the trouble maker  

htmlbody,emailaddress = ConnEmailParser()
filenamePDF= "UmfrageEthics_%s.pdf" % datetime.utcnow().strftime('%m%d%H%M%S%f')

def printPdf():
    try:
        html = htmlbody
        result = StringIO()
        pdf = pisa.pisaDocument(StringIO(html.encode("UTF-8")), result, encoding='UTF-8')
        filetoparse=open(filenamePDF,"wb")
        filetoparse.write(result.getvalue())
        filetoparse.close()
        #pisa.startViewer(filenamePDF)
    except:
        print "could not write PDF"  


def sendPdfParticipant():
    try:
        oNachricht = MIMEMultipart()
        oNachricht['From'] = 'some@email.com'
        oNachricht['To'] =  emailaddress
        oNachricht['Date'] = formatdate(localtime= True)
        oNachricht['Subject'] = 'A subject'
        oAnhang = MIMEBase('application','octet-stream')
        oAnhang.set_payload(open(filenamePDF,'rb').read())
        Encoders.encode_base64(oAnhang)
        oAnhang.add_header('Content-Disposition', 'attachment;filename = some.pdf') 
        message_text_plain = u'Some message...'
        oNachricht.attach(MIMEText(message_text_plain.encode('utf-8'), 'plain', 'UTF-8'))
        io = StringIO()
        g = Generator(io, False)
        g.flatten(oNachricht)


        oNachricht.attach(oAnhang)


        user = 'user'
        pwd = 'pwd'
        smtpserver = smtplib.SMTP("smtp.raumopol.de",587)
        smtpserver.ehlo()
        smtpserver.starttls()
        smtpserver.ehlo
        smtpserver.login(user, pwd)
        smtpserver.sendmail('som@eemail.com',emailaddress,oNachricht.as_string())
        smtpserver.quit()
    except:
        print"could no send data"

sendPdfSurvey()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:58:49+00:00Added an answer on May 25, 2026 at 9:58 pm

    In def ConnEmailParser(), check if data is None before calling data[0].split().
    If it is None, check if imap4 is correctly initiated and if login worked. If that is the case, post a reduced part of your code pinpointing on where it actually goes wrong.

    As @Jim Garrison pointed out, a traceback would help us (and you) a lot. This is how:

    import traceback
    
    try:
        some_code...
    except TypeError:
        traceback.print_exc()
    

    edit (based on your comment)

    When you encode a unicode string, you are replacing characters that are not part of ASCII with codes as ‘\xfc’. Apparently, saver.write() tries to encode your already encoded text, after first interpreting it as unicode (you got u'\xfc'). So don’t encode yourself. If it still goes wrong, see how you can change the encoding (e.g. ‘utf-8’, ‘cp1252’). Get the current encoding of your OS with

    import locale
    encoding=locale.getlocale()[1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to overload the dereference operator, but compiling the following code results in
Can someone explain to me why the following code results in the error: According
Running the following simple code results in a Strict violation. error message. I have
The following jQuery autocomplete code is not displaying the results in MVC3. When I
I see the results from the following code, but I don't understand exactly how
The following code results in 'favlist is not defined'. Despite trying to define favlist
The following code results in undefined for lastIndex: var a = /cat/g; var l
I understand that using the === compares type, so running the following code results
I tried the following code in LINQPad and got the results given below: List<string>
I'm using the following code to try to read the results of a df

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.