[Tutor] smtplib mail header problem

dave s pythontut at pusspaws.net
Tue Dec 6 21:40:43 CET 2005


Hi all,

I have a python script that works out & emails my employer with parts that I 
have used. It has been great for 11ish months, now all my mail is classed as 
spam & junked :(

Apparently the reason for it being junked is ...

'No subject' and 'No Sender'

I set up a small test script to isolate the problem using the core of the 
email code ...


#!/usr/bin/env python
# -*- coding: iso8859_1 -*- 


from smtplib import SMTP
from time import sleep, strftime
from datetime import datetime
import sys


email_SMTP = 'mail.pusspaws.net'
email_addr = 'jk at pusspaws.net'


def email_test():
  

    for trys in xrange(10):
        try:
                    
            mail_server = SMTP(email_SMTP)
            mail_server.login('XXXX', 'XXXX')
            
            
            subject = 'Test Email'
            from_ = 'Dave Selby<chubb at pusspaws.net>'
            to = email_addr
            return_path = email_addr
            
            blog="""Hi,\n\nThis is just a test"""
            
            msg = mail_headers(subject, from_, to, return_path) 
+'\r\n\r\n'+blog
            
            mail_server.sendmail(subject , email_addr, msg)
            
            mail_server.quit()
            
            # If we get to here, all is well, drop out of the loop
            break
            
        except:
            print 'Mailing error ... Re-trying ... '+str(trys+1)+' of 10\n'
            sleep(300)
            
    if trys==9:
        raise 'Mail Failure\n'+str(sys.exc_type)+'\n'+str(sys.exc_value)
    
    
    
    

def mail_headers(subject, from_, to, return_path):
    """
    Generate a formatted mail header string
    """
    
    header = 'Return-Path: ' + return_path 
    header = header + '\r\nFrom: ' + from_
    header = header + '\r\nReply-To: ' + return_path
    header = header + '\r\nTo: ' + to
    header = header + '\r\nSubject: '+subject

    now=datetime.now()

    day = now.strftime('%a')
    date = now.strftime('%d %b %Y %X')
    
    header = header + '\r\nDate : ' + day + ', ' + date + ' -0000\r\n'
    
    return (header)

    
email_test()



From this I receive the following ....


Return-Path: <Test>
 Delivered-To: jk at pusspaws.user
 Received: (qmail 21696 invoked by uid 503); 6 Dec 2005 20:35:48 -0000
 Received: from unknown (HELO localhost.localdomain) (chubb at 86.130.36.111)
  by host201.com with SMTP; 6 Dec 2005 20:35:48 -0000
 Return-Path: jk at pusspaws.net
 From: Dave Selby<chubb at pusspaws.net>
 Reply-To: jk at pusspaws.net
 To: jk at pusspaws.net
 Subject: Test Email
 Date: Tue, 06 Dec 2005 20:35:47 -0000
 X-Bogosity: Ham, tests=bogofilter, spamicity=0.288675, version=0.95.2
 X-UID: 
 Status: R
 X-Status: NGC
 X-KMail-EncryptionState: 
 X-KMail-SignatureState: 
 X-KMail-MDN-Sent: 
 
Hi,

This is just a test


I cannot seem to set the top 'Return-path' correctly, any ideas how I do 
this ? also I have a sender so I am unsure why the spam filter picked this 
out.

Any suggestions

Cheers

dave


More information about the Tutor mailing list