Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
ShellExecute
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Titre:
Divers
Thread ID:
00876340
Message ID:
00876464
Vues:
8
>>I have written a routine, based on ShellExecute() indeed, that sends an email. If it fails, for example due to a missing email client, it will simply return false. So, you can tell the user that it fails, and eventually what caused the failure.
>
>Peter,
>
>I like the idea of using ShellExecute with the mailto link. I just wanted to verify with you that ShellExecute will actually return false, as opposed to producing an error. Reason I ask is because I'm curious if the ShellExecute would need to be wrapped inside of a TRY/CATCH block.

Jon, here's my SendEMail() routine. Notice that it returns true if the lnResult >= 32. You can find the result codes in the description of ShellExecute(). I say, try it for yourself. :)
*	This function will start an executable that deals with email.
*	I don't know of any file extension that can be used to test for the existence of the 
*	executable beforehand!
*	So, just try a call and conclude that there's no email server if it returns false.

* Parameters:
*	tcRecipient	Recipient. E.g. "pvalenca@viafox.nl"
*	tcSubject	Title.
*	tcBody		Body text.
*	tcAttachment	Name of to be attached file. However, advise is not to use it!

* Result:
*	Whether or not it succeeded in starting the mailserver.

* E.g.:
*	if not SendEMail( "pvalenca@viafox.nl", "Request for support", "Hi" )
*		wait wind "Could not send an email" nowait
*	endif

FUNCTION SendEMail
	lparameters tcRecipient, tcSubject, tcBody, tcAttachment

	local lcParm, lcBody, lnResult, lcRecipient, lcSubject
	
	lcRecipient  = EncodeForEMail( OrDefault( m.tcRecipient , '' ) )
	lcSubject    = EncodeForEMail( OrDefault( m.tcSubject   , '' ) )
	lcBody       = EncodeForEMail( OrDefault( m.tcBody      , '' ) )
	lcAttachment = EncodeForEMail( OrDefault( m.tcAttachment, '' ) )
	*
	lcParm = "mailto:"     + m.lcRecipient + ;
		 "?Subject="   + m.lcSubject + ;
		 "&" + "Body=" + m.lcBody + ;
		 iif( not empty( m.lcAttachment ), ' &' + 'Attach="' + m.lcAttachment + '"', "" )

	declare integer ShellExecute in shell32.dll ;
		integer nWinHandle, ;
		string  cOperation, ;
		string  cFileName, ;
		string  cParameters, ;
		string  cDirectory, ;
		integer nShowWindow 

	lnResult = ShellExecute( 0, "Open", m.lcParm, "", "", 1 ) 

	clear dlls ShellExecute

	RETURN ( m.lnResult >= 32 )

*	Inspired by the routine UrlEncode from Rick Strahl, but entirely different.
*	See also:
*	-	http://www.west-wind.com/presentations/shellapi/shellapi.asp
*	-	http://www.faqs.org/rfcs/rfc2368.html
*	-	UT: <B>Re: Automate email</B> Thread 826623 Message 832461
FUNCTION EncodeForEMail( tcString )

	local lcString

	lcString = strtran( m.tcString, "%", "%25" )	&& This one must be the first!
	lcString = strtran( m.lcString, "?", "%3F" )
	lcString = strtran( m.lcString, "&", "%26" )
	lcString = strtran( m.lcString, "=", "%3D" )
	lcString = strtran( m.lcString, '"', "%22" )
	lcString = strtran( m.lcString, chr(13)+chr(10), "%0D%0A" )
	lcString = strtran( m.lcString, chr(13)        , "%0D%0A" )

	RETURN m.lcString

*	Returns the default if the types of the two parameters differ.
*	E.g. lcAlias = OrDefault( tcAlias, alias() )
function OrDefault
	lparameter tuExpr, tuDefault
	RETURN iif( type('m.tuExpr')=type('m.tuDefault'), m.tuExpr, m.tuDefault )
Groet,
Peter de Valença

Constructive frustration is the breeding ground of genius.
If there’s no willingness to moderate for the sake of good debate, then I have no willingness to debate at all.
Let's develop superb standards that will end the holy wars.
"There are three types of people: Alphas and Betas", said the beta decisively.
If you find this message rude or offensive or stupid, please take a step away from the keyboard and try to think calmly about an eventual a possible alternative explanation of my message.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform