Jump to content

Recommended Posts

Posted

I'm no programmer, but a good bit of the time I can figure things out by poking around or trying things trial & error. But for some reason I am struggling with what is likely a very simple process -- I want to include the content of a text file in the body of an email.

If someone could suggest the process (paste from clipboard, write line-by-line, etc) or function to use, I would much appreciate it. Code snippit or pointers welcome as well. :-)

Seriously hoping it's not so simple that you opt not to help this idiot...

Posted

If you have outlook:

Local $olMailItem = 0
    Local $olFormatRichText = 3
    Local $olImportanceLow = 0
    Local $olImportanceNormal = 1
    Local $olImportanceHigh = 2
    Local $olByValue = 1
    Local $olFormatHTML = 2
    Local $olFormatPlain = 1
    $file = FileOpen(c:\Untitled.txt", 0)
    $chars = FileRead($file)
    If @error = -1 Then MsgBox(0, "Error:", $chars)
    FileClose($file)
    $oOApp = ObjCreate("Outlook.Application")
    $oOMail = $oOApp.CreateItem ($olMailItem)
    $oOMail.Save
    With $oOMail
        .To = ($to)
        .Subject = $subj
        .BodyFormat = $olFormatHTML
        .Importance = $olImportanceNormal
        .HTMLBody = $body&@CRLF&$chars
        ;.attachments.add ($FileName3)
        .Send
    EndWith
    $oOApp=0
Posted

If you have outlook:

Local $olMailItem = 0
    Local $olFormatRichText = 3
    Local $olImportanceLow = 0
    Local $olImportanceNormal = 1
    Local $olImportanceHigh = 2
    Local $olByValue = 1
    Local $olFormatHTML = 2
    Local $olFormatPlain = 1
    $file = FileOpen(c:\Untitled.txt", 0)
    $chars = FileRead($file)
    If @error = -1 Then MsgBox(0, "Error:", $chars)
    FileClose($file)
    $oOApp = ObjCreate("Outlook.Application")
    $oOMail = $oOApp.CreateItem ($olMailItem)
    $oOMail.Save
    With $oOMail
        .To = ($to)
        .Subject = $subj
        .BodyFormat = $olFormatHTML
        .Importance = $olImportanceNormal
        .HTMLBody = $body&@CRLF&$chars
        ;.attachments.add ($FileName3)
        .Send
    EndWith
    $oOApp=0

Excellent! But... I'm not getting a CRLF at the end of each line, so everything is bunched together. Also, rather than send immediately (though that may be an ultimate goal) I want the email to come up for editing.

Thanks!

Posted

Change .Send to .Display

For the CRLF - you will have to modify it a bit to use Filereadline instead of Fileread

Posted

Conceptually I understand, but implementing is yet another matter :-(

I've tried multiple variations but none seem to write anything into the body (but a single line if I omit the while loop):

$file = FileOpen($TextFilename, 0)
$oOApp = ObjCreate("Outlook.Application")
$oOMail = $oOApp.CreateItem($olMailItem)
$oOMail.Save
With $oOMail
    .To = $address
    .Subject = $subject
    .BodyFormat = $olFormatHTML
    .Importance = $olImportanceNormal
    While 1
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop
        .HTMLBody = $line
    WEnd
    FileClose($file)
    ;.attachments.add ($FileName3)
    ;.Send
    .Display
EndWith
$oOApp = 0
Ideas?
Posted

Use HTML tags for a new line.

If you text file is like:

1line 121212

2line a23213

then you should write to the HTMLbody something like:

"1line 121212 <BR> 2line a23213 <BR>."

Posted

Conceptually I understand, but implementing is yet another matter :-(

I've tried multiple variations but none seem to write anything into the body (but a single line if I omit the while loop

I'm not sure if I am explaining myself well: I've been able to write the first line of the text file but nothing beyond that. When trying my while loop it writes nothing (which means I am obviously doing something wrong). :-)
Posted

Something like that?

$file = FileOpen($TextFilename, 0)
$oOApp = ObjCreate("Outlook.Application")
$oOMail = $oOApp.CreateItem($olMailItem)
$oOMail.Save
With $oOMail
    .To = $address
    .Subject = $subject
    .BodyFormat = $olFormatHTML
    .Importance = $olImportanceNormal
    While 1
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop
        $lineFinal =$lineFinal&"<BR>"&$line
    WEnd
    FileClose($file)
    .HTMLBody = $lineFinal
    ;.attachments.add ($FileName3)
    ;.Send
    .Display
EndWith
$oOApp = 0
Posted

Something like that?

Much better. But I ask you: Is there any way to prevent it from stripping all the whitespace? The text file is formatted with tabs, etc. that make it easily readable. The email, however, has all the text together (tabs stripped).

Thanks for all your assistance.

Posted

In that case use :

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in 1 character at a time until the EOF is reached
While 1
    $chars = FileRead($file, 1)
    If @error = -1 Then ExitLoop
    MsgBox(0, "Char read:", $chars)
Wend

FileClose($file)

and change :

.BodyFormat = $olFormatHTML

to

.BodyFormat = $olFormatRichText

and

.HTMLBody

to

.Body

Posted

<sigh>

I know the file (including whitespace) is being read because the msgbox displays it, but the email body is blank.

With $oOMail
    .To = $address
    .Subject = $subject
    .BodyFormat = $olFormatRichText
    .Importance = $olImportanceNormal
    $file = FileOpen($TextFilename, 0)

    ; Check if file opened for reading OK
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf

    ; Read in 1 character at a time until the EOF is reached
    While 1
        $chars = FileRead($file, 1)
        If @error = -1 Then ExitLoop
;           MsgBox(0, "Char read:", $chars)
        .body = $chars
    WEnd

    FileClose($file)
    ;.Send
    .Display
EndWith
Posted

I stripped out everything not used in this specific example to make it easier to read, but this worked for me. Both the tabs and the carriage return line feeds remained intact in the body of the message. One thing I did notice is that if you use .BodyFormat = $olFormatRichText but then use .HTMLBody = "", the message gets converted back to HTML and that screws it up again.

Local $olMailItem = 0
Local $olFormatRichText = 3
Local $olImportanceLow = 0
Local $olImportanceNormal = 1

$file = FileOpen("c:\Untitled.txt", 0)

$chars = FileRead($file)

FileClose($file)

$oOApp = ObjCreate("Outlook.Application")
$oOMail = $oOApp.CreateItem ($olMailItem)

$body = "body text"
With $oOMail
.BodyFormat = $olFormatRichText
.Importance = $olImportanceNormal
.Body = [email="$body&@CRLF&$chars"]$body & @CRLF & $chars[/email]
.Display
EndWith
$oOApp=0

And in your last message...

While 1
        $chars = FileRead($file, 1)
        If @error = -1 Then ExitLoop
;           MsgBox(0, "Char read:", $chars)
        .body = $chars
    WEnd

I think the reason why the body is blank is because ".body = $chars" is writing a single character to the body of the message for each iteration in the loop, then overwriting it with the next character. What happens in the end is your message's body contains just the last character of your file.

Posted

I stripped out everything not used in this specific example to make it easier to read, but this worked for me.

Fantastic! Just what I needed. Thank you both for your assistance.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...