Quantcast
Channel: DotNetNuke (RETIRED)
Viewing all articles
Browse latest Browse all 3

Email enhancement for DNN - Using a template

$
0
0
Don't think there's much interest in this especially since I've not seen any posts to the effect but here we go: Sometimes its nicer to send an email in HTML format with a logo and or other information. I made some minor modifications and have achieved this by simply doing the following: a) Create a text file with the html tags that I want displayed in the email message. b) Added any extra text/images/design style for the email message in the template. c) Modified the SendNotification function in Components/Globals.vb to open the template file and parse the fields in the right places. Voila - we now have a nicely formatted html email that we can send out to our customers/clients. Here's the modified SendNotification Function:Public Function SendNotification(ByVal strFrom As String, ByVal strTo As String, ByVal strBcc As String, ByVal strSubject As String, ByVal strBody As String, Optional ByVal strAttachment As String = "", Optional ByVal strBodyType As String = "", Optional ByVal emailUserName As String = "", Optional ByVal templateFile As String = "MailTemplate.txt") As String ' Obtain PortalSettings from Current Context Dim _portalSettings As PortalSettings = CType(HttpContext.Current.Items("PortalSettings"), PortalSettings) Dim mail As New MailMessage mail.From = strFrom mail.To = strTo 'Always send a copy of the email to the webmaster. If strBcc <> "" Then mail.Bcc = strBcc & "; webmaster@yourdomain.com" Else mail.Bcc = "webmaster@yourdomain.com" End If mail.Subject = strSubject If strBodyType <> "" Then Select Case LCase(strBodyType) Case "html" mail.BodyFormat = MailFormat.Html Case "text" mail.BodyFormat = MailFormat.Text End Select Else 'Assume that we're sending via HTML - use that as default. mail.BodyFormat = MailFormat.Html End If Dim templateBody As String = "" 'Now if we're sending as HTML mail, then use the template file. If mail.BodyFormat = MailFormat.Html Then Dim objReader Dim templateFound As Boolean = False Try objReader = New StreamReader(HttpContext.Current.Request.PhysicalApplicationPath& templateFile) templateBody = objReader.ReadToEnd() objReader.Close() templateFound = True 'Check that the template has atleast the EMAIL_BODY tags. If Not InStr(templateBody, "", CompareMethod.Text) > 0 Then templateFound = False End If Catch ex As Exception 'We didn't find a valid template. templateFound = False End Try objReader = Nothing 'If we find a valid template file, then do the following. If templateFound Then 'Now make the neccessary changes to this file. 'Replace the Subject templateBody = Replace(templateBody,"", strSubject) 'Replace the Time templateBody = Replace(templateBody, "", CDate(Now())) 'replace the destination person templateBody = Replace(templateBody, "", strTo) 'replace the user Name templateBody = Replace(templateBody, "", emailUserName) 'replace the body templateBody = Replace(templateBody, "", strBody) Else 'Didn't find a valid template. just add the strbody to the templatebody string templateBody = strBody End If Else 'We're sending text style email. just add the strbody to the templatebody string. templateBody = strBody End If 'Now send the new body. mail.Body = templateBody If strAttachment <> "" Then mail.Attachments.Add(New MailAttachment(strAttachment)) End If ' external SMTP server If _portalSettings.HostSettings("SMTPServer") <> "" Then SmtpMail.SmtpServer = _portalSettings.HostSettings("SMTPServer") End If Try SmtpMail.Send(mail) Catch objException As Exception ' mail configuration problem SendNotification = objException.Message End Try End Function'here's a sample email template:
From: webmaster@yourdomain.com
Sent:
To:
Subject:

Dear  ,

Thanks
webmaster@yourdomain.com

NOTE: This message is confidential to Your Company Name. If you are not the addressee, don't use or distribute this message; and please notify the sender by return e-mail.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images