Andy,

On Fri, 2008-05-30 at 07:38 +0100, andy wrote:
> Mats Taraldsvik wrote:
> > Hi!
> >
> > I got so annoyed with the undescriptive subject lines it's learning
> > alerts ( itslearning.com ) (and my university) uses, that I wrote a
> > Python script (attached) to take care of this.
> >
> > -> I added a filter rule to pipe the mail to this script.
> >
> > It uses an smtp-client in python standard library to send mail to
> > evolution, and regular expressions to get info from the contents and add
> > to the subject line.
> >
> > I can help with tweaking the script, if you find use for it.
> >
> > Regards,
> > Mats Taraldsvik
> >
> > On Tue, 2008-05-20 at 17:22 +0100, andy wrote:
> >   
> >> Reid Thompson wrote: 
> >>     
> >>> On Tue, 2008-05-20 at 12:20 +0000, Pete Biggs wrote:
> >>>
> >>>   
> >>>       
> >>>> Or what about 'redirect'ing the mail to yourself i.e. 
> >>>>
> >>>>   Message -> Forward As -> Redirect
> >>>>
> >>>> Then fill in your own address as the To: and change the subject line to
> >>>> whatever you want.  This will cause a new message to be created (which
> >>>> is the only way of doing what you want), but will retain the rest of the
> >>>> email intact.
> >>>>
> >>>> This would also be a way of dealing with removing attachments.
> >>>>
> >>>> Pete
> >>>>     
> >>>>         
> >>> Edit as New Message, Save Draft does create a 'new' message -- it's
> >>> essentially a copy with modifications.  The Message-id is changed
> >>> i.e. the message id from your email
> >>> Message-id: <[EMAIL PROTECTED]>
> >>>
> >>> Edit as New Message, Save Draft
> >>>
> >>> the message id from the Draft
> >>> Message-id: <[EMAIL PROTECTED]>
> >>>
> >>> and placing both emails in the same folder with threading on, lists the
> >>> draft as a thread child to your email
> >>> _______________________________________________
> >>> Evolution-list mailing list
> >>> Evolution-list@gnome.org
> >>> http://mail.gnome.org/mailman/listinfo/evolution-list
> >>>
> >>>   
> >>>       
> >> Dear Paul, Reid, Patrick & Pete
> >>
> >> First off, thank you very much for all of your replies and
> >> suggestions. Those are all very much appreciated and there are a
> >> number of pretty decent workarounds contained therein.
> >>
> >> My wife has seemingly stumbled on a fix that she feels comfortable
> >> with which is a variation of something already suggested: she will
> >> merely forward these emails to herself with the subject line she wants
> >> that will be meaningful to her and delete the original. It's a bit of
> >> a pain, but I will monitor some of those emails and see if there is
> >> some way that I can (semi-)automate this process - e.g. when an email
> >> from Companies House arrives, set a rule to forward it to her email
> >> address and so it will pop-up waiting for a new subject line.
> >>
> >> Once again, thank you so much from both of us!
> >>
> >> You're a good crowd.
> >>
> >> All the best
> >>
> >> Andy
> >>
> >> -- 
> >>
> >> "If they can get you asking the wrong questions, they don't have to worry 
> >> about the answers." - Thomas Pynchon, "Gravity's Rainbow"
> >> _______________________________________________
> >> Evolution-list mailing list
> >> Evolution-list@gnome.org
> >> http://mail.gnome.org/mailman/listinfo/evolution-list
> >>     
> Mats
> 
> Thank you for sending me this code/ programme - that's very generous of you!
> 
> Not being a programmer, but familiar with some basic Python, please let 
> me know if I have understood this properly:
> 
> The programme is executed before Evolution retrieves emails from the 
> server, iterates through the emails on the server and, according to 
> specific terms of the regular expression, will retitle subject lines. 
>  From there, it passes the function back to Evo to download the emails.
> 
> Is that a correct reading of the code's functionality?

Not exactly. Remember that this script is applied as a filter, and hence
excecutes after the retrieval of the original mail. The script recieves
the content of the mail with "pipe to program". Then, the script sends a
new mail/copy to your own address, via your smtp-server, where regex and
everything else in the program is applied.

So, what I did was to set my mail filters to pipe my mail through this
script, and then delete it (make sure everythin works before adding
delete). Then I recieve my modified mail, that my Python script sent
( make sure that the filter does not apply to this too, by changing the
sender adress or subject a bit, depending on your filters matching
criteria ).

> 
> If so, I presume that there is an option within Evo to execute code 
> before it runs its mail fetch or mail send operations, and that this 
> code would be called at that time?
> 
> Once again, many thanks Mats for your generosity in sharing this and in 
> your offer of help. That was really thoughtful of you.
> 
> All the best
> 
> Andy
> 
> -- 
> 

I've tried to comment the code more extensively.

If you can't figure it out, don't be afraid to contact me ( privately ).

Mats

'''
  Copyright (C) 2008 Mats Taraldsvik  <[EMAIL PROTECTED]>
 
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.
 
  You should have received a copy of the GNU General Public License
  along with this program; if not, see <http://www.gnu.org/licenses/>
'''

#!/usr/bin/python

import sys, re
import smtplib
from email.MIMEText import MIMEText

################## VARIABLES #######

SMTPSERVER = 'smtp.server.com' # Your smtp server
SENDERADR = "Name Surname <[EMAIL PROTECTED]>" # From: field
TOADR = '[EMAIL PROTECTED]' # To: field

###################################

def send_mail (senderadr , destinationadr , subject , contents, server='localhost') : # Defines the send_mail function. Used in the last line.
    mail = MIMEText(contents, _charset="utf-8") # Set right encoding/charset here if necessary.
    mail['From'] = senderadr
    mail['Subject'] = subject
    mail['To'] = destinationadr
    smtp = smtplib.SMTP(server)
    smtp.sendmail(senderadr , [ destinationadr ], mail.as_string())
    smtp.close()

# Execution start

message = sys.stdin.readlines() #read the mail that is parsed from stdin with "pipe to program" in evolution

headerflag = False
contentstring = ''
for line in message :
    
    if headerflag :
        contentstring += line.strip('\n')  # Collects content in a string
    if not headerflag :
        if line == '\n' : # If the last item in header list is a newline character, this works for separating headers and content
            headerflag = True

regexp = re.compile(r'med tittelen "(.*)"') #Regex to grab text

if re.search( 'har nettopp skrevet et innlegg i et emne du abonnerer' , contentstring) : #If we find this text in the content
    regex_subject = 'Diskusjon.no - Nytt innlegg' + ' [ ' + regexp.search(contentstring).groups()[0] + ' ] ' # This is the new subject
else :
    regex_subject = "Diskusjon.no [ ]" #If not, this is the subject.

send_mail(SENDERADR, TOADR, regex_subject, contentstring , SMTPSERVER) # This function, defined above, sends the mail, from python.
_______________________________________________
Evolution-list mailing list
Evolution-list@gnome.org
http://mail.gnome.org/mailman/listinfo/evolution-list

Reply via email to