Alpha Video Training
Results 1 to 3 of 3

Thread: HTML Mail Script - Can Field Variable Values be included

  1. #1
    Member
    Real Name
    tasos yerolemides
    Join Date
    Nov 2010
    Posts
    27

    Default HTML Mail Script - Can Field Variable Values be included

    The following script works ok for me to send emails.
    It is added to an update event in a Grid
    Can someone please help as to how it can be enhanced to include field values?

    -----------------------------
    function AfterUpdateRecord as v (e as p)

    vTo = "tasos@webside.com"
    vBcc = ""
    dim pm as P
    dim ps as P
    dim email_result as l

    vMessageStr = "here is may messsage."

    flag = email_smtp_open(ps, "mail.webside.com", 25, "support@webside.com", "abccde")
    if flag = .f. then
    'report an error to the user
    end
    end if
    pm._to = vTo
    pm._bcc = vBcc

    pm.from = "support@webside.com"
    pm.from_alias = "This is My Alias Name"
    pm.subject = "Subject of Email"
    pm.attachments = ""
    pm.html_message = "<HTML>Here is my message ee ee ee </HTML>"

    email_result = email_smtp_send(pm, ps)
    email_smtp_close(ps)
    end function

  2. #2
    Member
    Real Name
    Sparticuz
    Join Date
    Jun 2009
    Location
    Clearwater, FL
    Posts
    512

    Default Re: HTML Mail Script - Can Field Variable Values be included

    Here's a wrapper I wrote for sending email:

    Code:
    FUNCTION sendEmail AS P (e AS P, emailNum AS N = "0")
    	
    	'This function is pretty much a wrapper for email_smtp_send.
    	'It is written in XBASIC using a few references.
    	' http://wiki.alphasoftware.com/EMAIL_SMTP_OPEN+Function
    	' http://wiki.alphasoftware.com/EMAIL_SMTP_SEND+Function
    	' http://wiki.alphasoftware.com/EMAIL_SMTP_CLOSE+Function
    	
    	'The pointer `e` can contain:
    	'	e.from			C	=	Default = Whatever the username is set to. The sender's email address
    	'	e.from_alias	C	=	The sender's name
    	'	e.to			C	=	The recipient list. Multiple entries should be seperated via commas
    	'	e.cc			C	=	The CC list. Multiple entries should be seperated via commas
    	'	e.bcc			C	=	The BCC list. Multiple entries should be seperated via commas
    	'	e.subject		C	=	The subject of the email. THIS WILL OVERRIDE emailNum.
    	'	e.message		C	=	The body of a text message. THIS WILL OVERRIDE emailNum.
    	'	e.html_message	C	=	The body of a HTML message. THIS WILL OVERRIDE emailNum.
    	'	e.attachments	C	=	A semi-colon (;), comma, or CR-LF delimited list of filenames.
    	'	e.variables		P	=	IMPORTANT!!! These are the variables that are going to be
    	'									 merged with the emailNum that you specify.
    	'									 IT IS UP TO YOU!!! to have the correct
    	'									 variables specified!!!!!!!!!!!!!!!!!!!
    	'	e.lrelated		L	=	Default = .F. Attachments are seperate files, .T. for Embedded
    	'	e.returnreceipt	L	=	Default = .F. This will ask the sender for a read receipt.
    	
    	'emailNum is the id (Primary Key) of the Email being sent
    	'from the gen_email table in the global schema of the database.
    	'This is an OPTIONAL argument, if it is not specified, the 
    	'function will never even open the database.
    
    	'This function will return a pointer variable that contains the following
    	'	q.result		L	=	.T. if success, .F. if failed
    	'	q.resultText	C	=	This will contain ps.buffer (The result of the smtp_send)
    	'							Otherwise this will contain the error.
    	
    	' 			CONSTANTS NEED TO BE SET HERE!
    	' smtp_server			 =	 The host name of the STMP server.
    	dim smtp_server as c	 =	 "smtp.dazser.com"
    
    	' Port					 =	 The port for the smtp server
    	dim port as n			 = 	 25
    	
    	' Username				 =	 The user name of the account we're sending from
    	dim username as c		 =	 "alpha@dazser.com"
    	
    	' Password				 =	 The password of the user account
    	dim password as c		 =	 "thisisnotmypassword"
    	
    
    	' Other variables needed
    	dim flag as l
    	dim q as p
    	dim pm as p
    	dim ps as p
    	
    	if emailNum != 0 then
    
    		'This will run only if e.variables is set
    		'Basically, it will run if the email is being merged from the database
    		dim globalcn as sql::connection
    		dim rs as sql::ResultSet	
    		dim sqlCommand as c 
    	
    		'Open SQL Connection
    		flag = globalcn.Open("::Name::global")
    		if flag = .f. then
    			q.result = .F.
    			q.resultText = globalcn.CallResult.text
    			sendEmail = q
    		    end
    		end if
    
    		'get email from database
    		sqlCommand = "SELECT * FROM gen_email WHERE id = " + emailNum
    		flag = globalcn.Execute(sqlCommand)
    		if flag = .f. then
    			q.result = .F.
    			q.resultText = globalcn.CallResult.text
    			sendEmail = q
    			end
    		end if
    
    		'process email
    		rs = globalcn.ResultSet
    		
    		dim subject as c = rs.data("subject")
    		if eval_valid("subject") then
    			pm.subject = evaluate_string(subject, e.variables)	
    		end if
    		
    		dim html_msg as c = rs.data("content")
    		if eval_valid("html_msg") then
    			pm.html_message = evaluate_string(html_msg, e.variables)	
    		end if
    		
    		dim msg as c = rs.data("text")
    		if eval_valid("text") then
    			pm.message = evaluate_string(msg, e.variables)	
    		end if
    		
    		dim attachments as c = rs.data("attachments")
    		if attachments != "" then
    			pm.attachments = evaluate_string(attachments, e.variables)	
    		end if
    				
    		'cleanup
    		globalcn.FreeResult()
    		globalcn.Close()
    		delete globalcn
    		delete rs
    	
    	end if
    
    	'Go through all the variables in e!!!
    	'	e.from			C	=	The sender's email address
    	if eval_valid("e.from") then
    		pm.from = e.from
    	else
    		pm.from = username
    	end if
    
    	'	e.from_alias	C	=	The sender's name
    	if eval_valid("e.from_alias") then
    		pm.from_alias = e.from_alias
    	end if
    	
    	'	e.to			C	=	The recipient list. Multiple entries should be seperated via commas
    	if eval_valid("e.to") then
    		pm.to = e.to
    	else
    		q.result = .F.
    		q.resultText = "e.to not specified. Please give a valid CSV list."
    		sendEmail = q
    		end
    	end if
    	
    	'	e.cc			C	=	The CC list. Multiple entries should be seperated via commas
    	if eval_valid("e.cc") then
    		pm.cc = e.cc
    	end if
    	
    	'	e.bcc			C	=	The BCC list. Multiple entries should be seperated via commas
    	if eval_valid("e.bcc") then
    		pm.cc = e.bcc
    	end if
    	
    	'	e.subject		C	=	The subject of the email. THIS WILL OVERRIDE emailNum.
    	if eval_valid("e.subject") then
    		pm.subject = e.subject
    	end if
    	
    	'	e.message		C	=	The body of a text message. THIS WILL OVERRIDE emailNum.
    	if eval_valid("e.message") then
    		pm.message = e.message
    	end if
    	
    	'	e.html_message	C	=	The body of a HTML message. THIS WILL OVERRIDE emailNum.
    	if eval_valid("e.html_message") then
    		pm.html_message = e.html_message
    	end if
    	
    	'	e.attachments	C	=	A semi-colon (;), comma, or CR-LF delimited list of filenames.
    	if eval_valid("e.attachments") then
    		pm.attachments = e.attachments
    	end if
    	
    	' pm.xmailer is the sending email program name
    	pm.xmailer = "DAZSER MGMT v11 http://www.dazser.net - Complaints? kmcnally@dazser.com"
    	
    	'	e.lrelated		L	=	Default = .F. Attachments are seperate files, .T. for Embedded
    	if eval_valid("e.lrelated") then
    		pm.lrelated = e.lrelated
    	end if
    	
    	'	e.returnreceipt	L	=	Default = .F. This will ask the sender for a read receipt.
    	if eval_valid("e.returnreceipt") then
    		pm.returnreceipt = e.returnreceipt
    	end if
    	
    	'Send the Email
    	flag = email_smtp_open(ps, smtp_server, port, username, password)
    	if flag = .f. then
    		q.result = .F.
    		q.resultText = ps.buffer
    		sendEmail = q
    	    end
    	end if
    	
    	flag = email_smtp_send(pm, ps, .t.)
    	if flag = .f. then
    		q.result = .F.
    		q.resultText = ps.buffer
    		sendEmail = q
    		end
    	end if
    	
    	q.resultText = ps.buffer
    
    	flag = email_smtp_close(ps)
    	if flag = .f. then
       		q.result = .F.
    		q.resultText = "Email sent, but failed closing the connection."
    		sendEmail = q
    		end
    	end if
    	
    	q.result = .T.
    
    	sendEmail = q	
    	
    END FUNCTION
    Basically, I've got a table in my database with some 'template' emails. Here's an example:
    Code:
    (This is the html_msg for the email)
    
    
    <html>
    <head> </head>
    <body>
    <font face=Arial size=2>Dear {prospectname}, <br>Thanks for requesting information! 
    <p>What makes Jani-King clients renew their contracts year after year? Franchise Owners who have a vested interest to see that your facility is serviced correctly. 
    <a href="http://www.dazser.com/pdf/Trust.Your.Keys.Folder.pdf">Click Here</a> for more information.</p>
    <hr /><p>{sendername}<br />{senderemail}</p>
    </body>
    </html>
    Then I pass in a pointer with those variables included in curly braces.
    e.variables.prospectname
    e.variables.sendername
    etc...

    This little bit of code does the merging:
    Code:
    html_msg = evaluate_string(html_msg, e.variables)

  3. #3
    Member
    Real Name
    tasos yerolemides
    Join Date
    Nov 2010
    Posts
    27

    Default Re: HTML Mail Script - Can Field Variable Values be included

    Thank you for your repply.

    Regards
    Tasos

Similar Threads

  1. How to store field values from a filtered grid in a variable
    By gaby_h in forum Application Server Version 10 - Web/Browser Applications
    Replies: 7
    Last Post: 09-01-2011, 04:12 PM
  2. Using a script variable in a field rule...
    By mbmonk in forum Alpha Five Version 6
    Replies: 25
    Last Post: 07-30-2009, 02:35 PM
  3. Layout variable field not found by calculated values
    By Stephen Ferranti in forum Alpha Five Version 8
    Replies: 2
    Last Post: 05-07-2008, 04:47 PM
  4. Accessing field / variable values on a form
    By enstorms in forum Alpha Five Version 8
    Replies: 7
    Last Post: 05-03-2008, 09:02 AM
  5. Set Variable(s) to Field Values (External Table)
    By Alan Storey in forum Alpha Five Version 5
    Replies: 4
    Last Post: 05-31-2003, 02:16 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •