• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Extract all sender's email addresses from emails

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    #11
    Sure.

    See the code below. Save it as .py file. It is crude but does the job.
    Change here:

    PHP Code:
    pop_conn poplib.POP3_SSL('mx.mail.com''995')
    pop_conn.user('[email protected]')
    pop_conn.pass_('password'
    It will process 100 email in 8 seconds (your speed may vary), so leave it be.
    It will put all email addresses in a text file with append option, so if it fails, delete the content of the txt file so far.
    Emails will be extracted from the 'from' header so possibly very messy. You will need to process the list in excel (other) to get rid of duplication etc.

    PHP Code:
    print >> open('emails-list.txt''a'), message['from'

    Code:
    import poplib,time
    from email import parser
    
    pop_conn = poplib.POP3_SSL('mx.mail.com', '995')
    pop_conn.user('[email protected]')
    pop_conn.pass_('password')
    
    #Get messages from server:
    stt=time.ctime()
    print 'getting messages started at %s\n' %(stt)
    
    
    messages = [pop_conn.top(i,0) for i in range(1, len(pop_conn.list()[1]) + 1)]
    
    
    endt=time.ctime()
    print 'done getting messages at %s\n' %(endt)
    
    # Concat message pieces:
    messages = ["\n".join(mssg[1]) for mssg in messages]
    
    #Parse message intom an email object:
    messages = [parser.Parser().parsestr(mssg) for mssg in messages]
    
    
    for message in messages:
    print >> open('emails-list.txt', 'a'), message['from']
    
    pop_conn.quit()
    Last edited by gisp; 26 September 2021, 08:37.

    Comment


      #12
      a) Dumping the lot to excel and binning everything but the 'From' info
      Excel VBA does it quite easily. Used to do it to check order emails until they started putting everything into PDF.

      Code below is quick untested extract from a much more complicated thing so probably got a few errors, but close enough.

      'Option Compare Text
      Public r As Integer

      Private Sub CommandButton1_Click()
      Dim fold As String

      'clear list
      Range("A15:A500").Select
      Selection.ClearContents
      Range("A15").Select

      'sheet row
      r = 1

      'For each folder in list
      fol = 1
      Do
      fold = Range("C5").Cells(fol, 1).Value
      If fold <> "" Then
      getemails (fold)
      fol = fol + 1
      End If
      Range("AF2").Value = fol
      Loop While fold <> ""

      End Sub

      Sub getemails(mailpath As String)
      Dim FSO As FileSystemObject
      Dim fileList As Object, email As Object
      Dim fname As String, fullpath As String
      Dim temp As String As String

      ' Create an object form the filesystem object
      Set FSO = CreateObject("Scripting.FileSystemObject")

      'set output list
      Set outlist = Range("A15")

      ' Traverse through the files
      nmail = 0
      For Each email In fileList

      ' Get the file name
      fname = email.Name
      If InStr(fname, ".eml") Then

      'look for key fields in file - this is mostly ascii
      fullpath = mailpath & "" & fname
      Open fullpath For Input As #1
      Do While (Not EOF(1))

      Line Input #1, temp
      temp = Trim(temp)

      'get from line
      If InStr(temp, "From: ") > 0) then
      outlist.Cells(r, 1).Value = temp
      Exit Do
      End If
      r=r+1
      Loop
      Close #1

      Next

      End Sub
      Last edited by xoggoth; 26 September 2021, 11:36.
      bloggoth

      If everything isn't black and white, I say, 'Why the hell not?'
      John Wayne (My guru, not to be confused with my beloved prophet Jeremy Clarkson)

      Comment


        #13
        Originally posted by xoggoth View Post

        Excel VBA does it quite easily. Used to do it to check order emails until they started putting everything into PDF.

        Code below is quick untested extract from a much more complicated thing so probably got a few errors, but close enough.

        'Option Compare Text
        Public r As Integer

        Private Sub CommandButton1_Click()
        Dim fold As String

        'clear list
        Range("A15:A500").Select
        Selection.ClearContents
        Range("A15").Select

        'sheet row
        r = 1

        'For each folder in list
        fol = 1
        Do
        fold = Range("C5").Cells(fol, 1).Value
        If fold <> "" Then
        getemails (fold)
        fol = fol + 1
        End If
        Range("AF2").Value = fol
        Loop While fold <> ""

        End Sub

        Sub getemails(mailpath As String)
        Dim FSO As FileSystemObject
        Dim fileList As Object, email As Object
        Dim fname As String, fullpath As String
        Dim temp As String As String

        ' Create an object form the filesystem object
        Set FSO = CreateObject("Scripting.FileSystemObject")

        'set output list
        Set outlist = Range("A15")

        ' Traverse through the files
        nmail = 0
        For Each email In fileList

        ' Get the file name
        fname = email.Name
        If InStr(fname, ".eml") Then

        'look for key fields in file - this is mostly ascii
        fullpath = mailpath & "" & fname
        Open fullpath For Input As #1
        Do While (Not EOF(1))

        Line Input #1, temp
        temp2 = Trim(temp)

        'get from line
        If InStr(temp, "From: ") > 0) then
        outlist.Cells(r, 1).Value = temp
        r=r+1
        End If
        Loop
        Close #1

        Next

        End Sub
        what are the inputs to this though? already extracted mail? in what format?

        Comment


          #14
          I used live mail on Windows at the time, emails were in Appdata\local. Don't know where Mac saves things by default but I daresay all email apps allow backup to folder of your choice. Some parts of emails are encoded but the headers are always plain text AFAIK.
          Last edited by xoggoth; 26 September 2021, 16:45.
          bloggoth

          If everything isn't black and white, I say, 'Why the hell not?'
          John Wayne (My guru, not to be confused with my beloved prophet Jeremy Clarkson)

          Comment

          Working...
          X