#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    174
    Rep Power
    11

    block string formatting in webbrowser module


    hey,

    I'm trying to automate some tedious emails I make in Outlook Express weeeee. OE doesn't have a COM server like Outlook (also OE doesn't crash my computer), so Dispatch isn't available.

    Instead I've dodged COM by using the webbrowser module to write a mailto tag, which opens in IE automatically using webbrowser.open method, which then loads OE since it's the default email program Works fine. Creates the email To, Subject, and Body headings without trouble.

    Except for one thing: string formatting in the body.

    so here's a snippet:

    Code:
    import webbrowser
    
    body = """
    Since the beginning of time blah blah blah etc
    
    instead of spacing and       tabbing working as per normal under these block quotes
    
    everythingislikethisinstead.why?
    """
    subject = "Repetative email thing"
    email = raw_input("Enter email: ")
    
    #dump variables with data into mailto formatted tag using dictionary
    
    uri = "mailto:%(email)s?subject=%(subject)s&body=%(body)s" %{'email' : email, 'subject':subject, 'body':body}
    
    #do the do..er make my email
    
    webbrowser.open(uri)
    The body variable is a """ type of string that formats correctly whenever I print it in console, but when in OE, it's all one block and has removed all formatting.

    I've tried replacing \n's with \r\n's and also tried using #&10; html linefeed ascii code, but each time the body block is all together without any formatting.

    Riddle Me This:
    How do I keep the formatting of the """ block while passing it into the webbrowser.open method?

    TIA
    sf2k
    Last edited by sf2k; July 6th, 2005 at 01:27 AM. Reason: Mongo says cogent english... Good
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Originally Posted by sf2k
    hey,

    Instead I've dodged COM by using the webbrowser module to write a mailto tag, which opens in IE automatically using webbrowser.open method, which then loads OE since it's the default email program Works fine. Creates the email To, Subject, and Body headings without trouble.

    Except for one thing: string formatting in the body.

    ....
    Riddle Me This:
    How do I keep the formatting of the """ block while passing it into the webbrowser.open method?

    TIA
    sf2k
    You are loading up the text into a web browser which tries to treat it as HTML - by default it ignores/strips CR+LF - use html tags like <br> instead. I'm sure these will get converted back to CR/LF in the mail.
    (Note: untested - not at my usual machine)
    grim



    grim
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    174
    Rep Power
    11
    Originally Posted by Grim Archon
    You are loading up the text into a web browser which tries to treat it as HTML - by default it ignores/strips CR+LF - use html tags like <br> instead. I'm sure these will get converted back to CR/LF in the mail.
    (Note: untested - not at my usual machine)
    grim

    grim
    Thanks for the suggestion!

    tried it, and it just added <BR>'s to the text without any linefeed returns...hmmm. I had also tried the html ascii for linefeed and that had not worked either.

    I think I'll create a simple mailto and send myself raw text to see what the code really is.

    any further suggestions welcome

    cheers
    sf2k
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    174
    Rep Power
    11

    Note to self


    Updating Myself:

    From microsoft website at http://support.microsoft.com/kb/q251230/
    I get:

    WORKAROUND
    When you code your Hypertext Markup Language (HTML) page, use the %25 escape character before the LF and CR characters.

    The following is an example of the CR:
    <A HREF="mailto:email1;email2&cc=email3?subject=My%20Subject&body=Line1%250dLine2"> Click Here To Invoke Mail </A>

    I tried it, and it doesn't work!!! sweet... I'll keep looking. Any tips would really help, thanks. I'll try rebuilding the string next.

    cheers
    sf2k
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    174
    Rep Power
    11
    http://www.forum4designers.com/message145899.html

    notice the %0A's in there, denoting the carriage return...this is it! using %0A to replace the \n in the body of my email, I get the formatting needed in the message.

    adding:
    Code:
    body=body.replace("\n","%0A")
    and it works. **whew**

    now all that's left to do is update the input windows.

    <edited out> Had commented on wanting to use gui instead of raw_input, but this will require a gui toolkit and beyond the simpleness I want to keep for this script.</edited out>

    Last thing to see if I can update the From header to my office IMAP server address!

    TIA for any information

    cheers
    sf2k
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    174
    Rep Power
    11

    How to Change From: address?


    Ok, so far this script works okay, sending the email I want with the right replacements.

    Thing is, I want it to use my outgoing IMAP server account not the default IMAP account. Tried setting the IMAP server as the default mail account but still any new email, script or otherwise, uses my main account instead.

    Can't use SendKeys in a WSH conversion as there is no quickKey for the From: pulldown list. argh.

    Any way to select a different From: item in Outlook Express programmically? Or maybe just change the From: address?

    TIA
    sf2k
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    174
    Rep Power
    11

    Solution


    In outlook express, although when creating a new email your personal or first folder will be selected, by selecting another folder as default, when an email is sent that will be the starting point for the program.

    I'd like to do this programmically, but I can also use python wsh SendKeys for that change.

    so running the script again but with the imap folder as default, it correctly used the imap from address, not the personal address.

    Next stage: Using with Outlook! noooo.....

    Any notes you want to add please feel free to reply. Automating email I thought would have had more responese... anyway! I'll keep my notes here for anyone to read.

    cheers
    sf2k

IMN logo majestic logo threadwatch logo seochat tools logo