Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coding showing in email 2

Status
Not open for further replies.

keepingbusy

Programmer
Apr 9, 2000
1,470
GB
The below code was found on one of our Tek-Tip forum users website (Craig Boyd).

I have tried this using Fox 6 and Outlook 2007.

The only problem is that the HTML code is showing in the body of the email when it is received by the recipient. Otherwise it is working fine.

Code:
* THE FOLLOWING ARE NORMALLY FROM A TABLE
STORE "myemail@soewhere.com" TO memail
STORE "Your order details: " TO mcontent
STORE "657643219534" TO mtransact

#DEFINE olImportanceLow 0
#DEFINE olImportanceNormal 1
#DEFINE olImportanceHigh 2

LOCAL lcTo, lcSubject, lcBody, lcCC, lcBCC, llHTMLFormat, llOpenEmail, lcErrReturn

lcTo = ALLTRIM(memail)
lcSubject = "Information: "+ALLTRIM(mcontent)+" "+ ;
  ALLTRIM(mtransact)

*!* Sending the body in HTML format

llHTMLFormat = .T.

[b]* THIS WORKS[/b]
lcBody = "Dear customer: "+ALLTRIM(mcontent)+" "+ALLTRIM(mtransact)

[b]* THIS DOESN'T AND SHOWS THE HTML CODING[/b]
lcBody = "<a href='[URL unfurl="true"]http://www.websitename.com/somepages/index.htm'>"[/URL] + ;
 "Inforamtion Page" + ;
 "</a>"

*!* to automatically send email set llOpenEmail to .F.

llOpenEmail = .T. && Whether email is opened in Outlook or not

SendViaOutlook(@lcErrReturn, lcTo, lcSubject, lcBody, llHTMLFormat, olImportanceHigh, llOpenEmail)

IF EMPTY(lcErrReturn)
  MESSAGEBOX("'" + lcSubject + "'" + IIF(llOpenEmail, " opened ", " sent ") + "successfullly.", 64, "Send email via Outlook")
ELSE
  MESSAGEBOX("'" + lcSubject + "' failed to be sent.  Reason:" + CHR(13) + lcErrReturn, 64, "Send email via Outlook")
ENDIF

***********************************************************
PROCEDURE SendViaOutlook(tcReturn, tcTo, tcSubject, tcBody, taFiles, ;
  tcCC, tcBCC, tlHTMLFormat, tnImportance, tlOpenEmail)
***********************************************************

LOCAL loOutlook, loItem, lnCountAttachments, loMapi
loOutlook = CREATEOBJECT("outlook.application")
loMapi = loOutLook.GetNameSpace("MAPI")
loMapi.Logon()
loItem = loOutlook.CreateItem(0)

WITH loItem
  .Subject = tcSubject
.TO = tcTo

  IF tlHTMLFormat
    .HTMLBody = tcBody
  ELSE
    .Body = tcBody
  ENDIF

  IF TYPE("tnImportance") != "N"
    tnImportance = 1 && normal importance
  ENDIF
  
  .Importance = tnImportance
    
  IF tlOpenEmail
    .DISPLAY()
  ELSE
    .SEND()
  ENDIF
ENDWITH

RELEASE oOutlook, oItem
STORE .NULL. TO oOutlook, oItem


In some previous code we had, there was a line that looked like:
Code:
.HtmlBody = "<HTML><BODY> ;
  <br><br>Received by us on: <B>"+cdow(mdaterec)+" "+dmy(mdaterec)+"</B> ;

  edited....

  <br><br>"+TRIM(memailnot)+" ;
  </BODY> ;
  </HTML>"


We tried putting the <HTML> tags in but that doesn't work either.

We have had to change the old procedure due to circumstances beyond our control.

Can anyone shed any light on what is missing / needs amending to make it work?

Thank you

Lee
 
* THIS DOESN'T AND SHOWS THE HTML CODING
lcBody = "<a href=' + ;
"Inforamtion Page" + ;
"</a>"

I would think it’s because it’s the body (text) of the email i.e. the URL is being sent as a message, it’s some fictitious site which Craig must have put in as an example. Just remove it...
 
With your call

SendViaOutlook(@lcErrReturn, lcTo, lcSubject, lcBody, llHTMLFormat, olImportanceHigh, llOpenEmail)

You are not respecting the parameters SendViaOutlook needs.

The parameterization ist SendViaOutlook(tcReturn, tcTo, tcSubject, tcBody, taFiles, tcCC, tcBCC, tlHTMLFormat, tnImportance, tlOpenEmail)

So between tcBody parameter an tlHTMLFormat there need to be taFiles, tcCC, tcBCC. If you don't want to use these parameters you can't leave them out this way, to use the tlHTMLFormat parameter you need to pass taFiles (empty array), tcCC (empty string), tcBCC (empty string) and only after that llHTMLFormat (.T.).

VFP has no such thing as named parameters, only because you pass in llHTMLFormat it does not get in as tlHTMLFormat.

Bye, Olaf.
 
Imaginecorp

I added the website name as an example. Craigs original code is here:


Olaf

I have inserted the original line but now I get an error:
Code:
Variable 'TCRETURN' is not found

I did start this over but now the code runs but opens Outlook with a new email and does not send it automatically.

Any guidance not the sollution would be appreciated.

Thank you

Lee
 
Lee: There are too many Parameters that are being filled automatically or not at all in the code above:

If I may, The basic code for sending email is as follows. You can tweek it any way you like:

Code:
	oOutlook = Createobject('Outlook.Application')
	oNameSpace = oOutlook.getnamespace('MAPI')
	oOutbox = oNameSpace.GetDefaultFolder(4)
	oItems = oOutbox.Items
	oMailItem = oItems.Add(0)
	oMailItem.To = "support@imaginecorp.com"
	oMailItem.HTMLBody = cString              &&&&&&.Body 
	oMailItem.Subject = 'Error Report from: '+Alltrim(itssystem.in_firm)
	oMailItem.ReadReceiptRequested = .T.
	oMailItem.Display  &&&Send
we use it in our app, i.e. if an error takes place it is sent to us via email.
The above will work in VFP6, in 9 you can use Reportlistner class for more control over what you are sending.

oMailItem.HTMLBody = cString &&&&&&.Body : older versions of outlook did not have HTMLbody so use "Body" instead in them, In your case it does not apply as you are using Outlook2007.
oMailItem.Display &&&Send : To send email outomatically change ".Display" to ".Send"
cString = is the body of the message, in our case the errors encountered.

If you are not sending email automatically include Parameters that will allow the user to select "To", "Subject" and "Display or Send"


 

Imageincorp

Cant thank you enough. Your suggestion has resolved the issue. We are now able to use HTML tags with variables and table fields after:
Code:
oMailItem.HTMLBody =
This works perfectly with no errors.

If any one else reads this thread, here is part of the code with some of the HTML tags etc:
Code:
oMailItem.HTMLBody = "<HTML><BODY> ;
  <br>Order Ref: <B>"+TRIM(mtransact)+ "</B> ;
  <br><br>Received by us on: <B>"+cdow(mdaterec)+" "+dmy(mdaterec)+"</B> ;
  <br><br>You should receive your order on or around the: <B>"+ ;

More html....

  <br><br>"+TRIM(mcontact)+" ;
  </BODY> ;
  </HTML>"

A star for you and thanks to those who posted on this thread.

Lee
 
Hi Lee,

glad you solved it.

You missunderstood me. What I posted was not the corrected call but pointed out the SendViaOutlook functions signature (that means it's parameters) to give you a heads on, that you left some parameters and thus your llHTMLFormat variable did not got into Craig's function as tlHTMLFormat.

Craig Boyds code is perfectly okay, all you would have needed is to change your call and have parameters in the right place of the call and add values for the oarameters you skipped. To see it easier:

Code:
* your call (not working)
* SendViaOutlook(@lcErrReturn, lcTo, lcSubject, lcBody, llHTMLFormat, olImportanceHigh, llOpenEmail)
* Craig's parameterization (LPARAMETERS ...)
* LPARAMETERS   tcReturn,    tcTo, tcSubject, tcBody, taFiles, tcCC, tcBCC, tlHTMLFormat, tnImportance   , tlOpenEmail
* therefore the correct parameterization with YOUR variables
SendViaOutlook(@lcErrReturn, lcTo, lcSubject, lcBody, .F.     , ''  , ''   ,llHTMLFormat, olImportanceHigh, llOpenEmail)

You may even need to pass an (empty) array for the taFiles parameters instead of .F., but passing in a bool already worked, Craig does not test if taFiles is an array.

But do you now see your mistake? As you want to pass in llHTMLFormat to create a HTML mail, you can't skip the parameters before tlHTMLFormat, even if you don't have attachments, CC and/or BCC you need to pass values for these before passing in llHTMLFormat. You passed in llHTMLFormat to the taFiles parameter and it didn't hurt, but you also got no HTML mail.

It's a lengthy list of parameters, but you can alsway simply check a call to some function or mtehod by copying the parameters list as a comment above your call like I did and then it will spring to the eye (hopefully).

Bye, Olaf.
 

Hello Olaf

Thank you for your comprehensive reply which does make sense. I can see what you mean by the SendViaOutlook

This is quite new to me so I am grateful for everyone's help and guidance.

Your help is also worthy of a star.

Kind regards

Lee
 
Lee;

Thank you, glad I could help.

If you want to get away from creating an HTML string with all the formatting etc, unless you are formatting in colors or bold text etc, just create a string like we do:

Code:
Again the error report example:

cString = ""
cString = "Client Who Encountered the Error: "+Chr(13)
cString = cString + Alltrim(itssystem.in_firm) +Chr(13)
cString = cString + Alltrim(itssystem.in_address) + Chr(13)
cString = cString + Alltrim(Proper(itssystem.in_city))+", "+;
		itssystem.in_state+". "+ itssystem.in_zip + Chr(13)
cString = cString + "Phone: "+Alltrim(itssystem.phone) + Chr(13)+Chr(13)
cString = cString + "User: "+Alltrim(user_name)+Chr(13)
cString = cString + "Date & Time: "+Dmy(Datetime())+" "+Ttoc(Datetime(),2)+Chr(13)+Chr(13)
........
........More fields, message etc etc

These are fields from a table.

BUT: now you have to make it:

oMailItem.Body = cString

Insted of oMailItem.HTMLBody as it will not recogonize line breaks etc.
 
Lee, while SendViaOutlook may be new to you, there's nothing new to passing parameters to a function. If a function is defined as

function name(par1,par2,par3,par4)

and you don't want to use par2 and par3 but par4, then you'll still need to pass in values for par2 and par3 that make sense. You might also try to skip these by passing in nothing:

name(val1,,,val4)

This skipped par2 and par3.

But a call
name(val1,val4) will pass val4 to par2, not to par4. How should foxpro know, that you want to pass val4 to par4?

Bye, Olaf.
 

Olaf,

Your posts are very informative and helpful. I will certainly look at the additional coding you have posted.

Many thanks

Regards

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top