Inserting Images in Emails

Posted By pcotgrove Mon 1 Apr 2013
Add to Favorites0
Author Message
pcotgrove
 Posted Sat 6 Apr 2013
Supreme Being

Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)

Group: Forum Members
Last Active: Sat 18 May 2013
Posts: 0, Visits: 57
As you stated earlier in this thread, even though I embedded (base64) images into the email (via the template) they still show up in the recipient mailbox as red cross broken links, an the images will not display.

I have adopted a different approach using cid data - below is an example that appears in my HTML template files within the skin:

<img src="cid:EmbeddedLogo" alt="xyz.com" title="xyz.com" />

I made some 'hardcoded' changes to the SendEmail function in kartris.vb but these could easily becoming global settings held within the DB, general.email.logo for example that specifies the local path of the image to the web application.

My change is located just after the SMTP authenticated user check and basically adds in an AlternativeView using a LinkedResource which is the image(s)

Dim objMail As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient(GetKartConfig("general.email.mailserver"), CInt(GetKartConfig("general.email.smtpportnumber")))
Dim strUserName As String = GetKartConfig("general.email.smtpauthusername")
If Not String.IsNullOrEmpty(strUserName) Then
objMail.Credentials = New System.Net.NetworkCredential(strUserName, GetKartConfig("general.email.smtpauthpassword"))
End If

Dim htmlView = AlternateView.CreateAlternateViewFromString(strBody, Encoding.UTF8, MediaTypeNames.Text.Html)

Dim mediaType As String = MediaTypeNames.Image.Jpeg
Dim imgLogo As New LinkedResource("\\Images\\Templates\\logo.jpg", mediaType)
Dim imgThankYou As New LinkedResource("\\Images\\Templates\\thanks.jpg", mediaType)

imgLogo.ContentId = "EmbeddedLogo"
imgLogo.ContentType.MediaType = mediaType
imgLogo.TransferEncoding = TransferEncoding.Base64
imgLogo.ContentType.Name = imgLogo.ContentId
imgLogo.ContentLink = New Uri("cid:" + imgLogo.ContentId)

imgThankYou.ContentId = "EmbeddedThankYou"
imgThankYou.ContentType.MediaType = mediaType
imgThankYou.TransferEncoding = TransferEncoding.Base64
imgThankYou.ContentType.Name = imgThankYou.ContentId
imgThankYou.ContentLink = New Uri("cid:" + imgThankYou.ContentId)

htmlView.LinkedResources.Add(imgLogo)
htmlView.LinkedResources.Add(imgThankYou)
objMailMessage.AlternateViews.Add(htmlView)
objMailMessage.IsBodyHtml = True

objMail.Send(objMailMessage)

Now if the cid refernce exists in the template then the image is inserted and successfully displayed within the body of the message in the recipient mailbox.

Maybe you would like to integrate this into the next release? Would be great to see it configured from the Admin pages - it will also make it easy for me to upgrade to the newer versions as they are released.

Thanks
pcotgrove
 Posted Fri 5 Apr 2013
Supreme Being

Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)

Group: Forum Members
Last Active: Sat 18 May 2013
Posts: 0, Visits: 57
Small amendment!!

strHTMLEmailText = strHTMLEmailText.Replace("[customeremail]", strEmail)
strHTMLEmailText = strHTMLEmailText.Replace("[customerpassword]", strPassword)
pcotgrove
 Posted Fri 5 Apr 2013
Supreme Being

Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)

Group: Forum Members
Last Active: Sat 18 May 2013
Posts: 0, Visits: 57
Just to let you know I have got this working, I had to amend your code snippet sent in the earlier post.

Here is my working code:

'Send new account email to new customer
If KartSettingsManager.GetKartConfig("frontend.users.emailnewaccountdetails") = "y" Then

'Build up email text
Dim strSubject As String = GetGlobalResourceObject("Email", "Config_SubjectLine5")
Dim sbEmailText As StringBuilder = New StringBuilder
sbEmailText.Append(GetGlobalResourceObject("Email", "EmailText_CustomerSignedUpHeader") & vbCrLf & vbCrLf)
sbEmailText.Append(GetGlobalResourceObject("Email", "EmailText_EmailAddress") & strEmail & vbCrLf)
sbEmailText.Append(GetGlobalResourceObject("Email", "EmailText_CustomerCode") & strPassword & vbCrLf & vbCrLf)
sbEmailText.Append(GetGlobalResourceObject("Email", "EmailText_CustomerSignedUpFooter1") & CkartrisBLL.WebShopURL & "customer.aspx" & GetGlobalResourceObject("Email", "EmailText_CustomerSignedUpFooter2"))
sbEmailText.Replace("<br>", vbCrLf).Replace("<br />", vbCrLf)

Dim blnHTMLEmail As Boolean = KartSettingsManager.GetKartConfig("general.email.enableHTML") = "y"

If blnHTMLEmail Then
Dim strHTMLEmailText As String = RetrieveHTMLEmailTemplate("NewCustomerSignUp")
'build up the HTML email if template is found

If Not String.IsNullOrEmpty(strHTMLEmailText) Then
strHTMLEmailText = strHTMLEmailText.Replace("[webshopurl]", WebShopURL)
strHTMLEmailText = strHTMLEmailText.Replace("[websitename]", GetGlobalResourceObject("Kartris", "Config_Webshopname"))
strHTMLEmailText = strHTMLEmailText.Replace("[customeremail]", GetGlobalResourceObject("Email", "EmailText_EmailAddress"))
strHTMLEmailText = strHTMLEmailText.Replace("[customerpassword]", GetGlobalResourceObject("Email", "EmailText_CustomerCode"))
sbEmailText.Clear()
sbEmailText.Append(strHTMLEmailText)
Else
blnHTMLEmail = False
End If

End If

SendEmail(LanguagesBLL.GetEmailFrom(CInt(Session("LANG"))), strEmail , strSubject, sbEmailText.ToString, , , , , blnHTMLEmail)
End If
Paul
 Posted Fri 5 Apr 2013
große Käse

große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)

Group: Administrators
Last Active: Tue 10 Sep 2024
Posts: 807, Visits: 2,748
Can you try replacing that code block with this modified one:


'Send new account email to new customer
If KartSettingsManager.GetKartConfig("frontend.users.emailnewaccountdetails") = "y" And blnNewUser Then

Dim blnHTMLEmail As Boolean = KartSettingsManager.GetKartConfig("general.email.enableHTML") = "y"
If blnHTMLEmail Then
Dim strHTMLEmailText As String = RetrieveHTMLEmailTemplate("NewCustomerSignUp")
'build up the HTML email if template is found
If Not String.IsNullOrEmpty(strHTMLEmailText) Then
strHTMLEmailText = strHTMLEmailText.Replace("[webshopurl]", WebShopURL)
strHTMLEmailText = strHTMLEmailText.Replace("[websitename]", GetGlobalResourceObject("Kartris", "Config_Webshopname"))
strHTMLEmailText = strHTMLEmailText.Replace("[customeremail]", UC_KartrisLogin.UserEmailAddress)
strHTMLEmailText = strHTMLEmailText.Replace("[customerpassword]", UC_KartrisLogin.UserPassword)
sbdNewCustomerEmailText.Clear()
sbdNewCustomerEmailText.Append(strHTMLEmailText)
Else
blnHTMLEmail = False
End If
End If
SendEmail(strFromEmail, UC_KartrisLogin.UserEmailAddress, strSubject, sbdNewCustomerEmailText.ToString, , , , , blnHTMLEmail)
End If


--
If my post solves your issue, can you 'Mark as Answer' so it's easier for other users to find in future.

If you would like to be informed of new features, new releases, developments and occasional special bonuses, please sign up to our mailing list: http://bit.ly/19sKMZb
Fri 5 Apr 2013 by Paul
Paul
 Posted Fri 5 Apr 2013
große Käse

große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)

Group: Administrators
Last Active: Tue 10 Sep 2024
Posts: 807, Visits: 2,748
Shouldn't need to precompile, things should run fine as the scripts...

I am checking the code now, I think the HTMLemail boolean is optional, but it assumes text if not there, which could explain the problem. I'll check the file now.


--
If my post solves your issue, can you 'Mark as Answer' so it's easier for other users to find in future.

If you would like to be informed of new features, new releases, developments and occasional special bonuses, please sign up to our mailing list: http://bit.ly/19sKMZb
pcotgrove
 Posted Fri 5 Apr 2013
Supreme Being

Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)

Group: Forum Members
Last Active: Sat 18 May 2013
Posts: 0, Visits: 57
Sorry to keep spamming you

I have noticed that the sendmail function is not in the same format as the other implementations, the password reset version for example - the boolean blnHTMLEmail is not being used although it is set correctly when the template is retrieved.

Could this be a problem or should I try to precompile the site? Not sure if I need to do this if the code changes, can you confirm??

From NewCustomerSignUp

SendEmail(LanguagesBLL.GetEmailFrom(CInt(Session("LANG"))), strEmail, strSubject, sbEmailText.ToString)


From PasswordReset

SendEmail(LanguagesBLL.GetEmailFrom(intCurrentLanguage), strEmailAddress, strSubjectLine, strBodyText, , , , , blnHTMLEmail)
pcotgrove
 Posted Fri 5 Apr 2013
Supreme Being

Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)

Group: Forum Members
Last Active: Sat 18 May 2013
Posts: 0, Visits: 57
Yes I believe so, I certainly get the templated email when recieving order status changes.

After making the changes, is there any requirement to run the precomile.bat file or are the changes just intepreted on runtime?
Paul
 Posted Fri 5 Apr 2013
große Käse

große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)

Group: Administrators
Last Active: Tue 10 Sep 2024
Posts: 807, Visits: 2,748
Do you have general.email.enableHTML config setting set to 'y'?

--
If my post solves your issue, can you 'Mark as Answer' so it's easier for other users to find in future.

If you would like to be informed of new features, new releases, developments and occasional special bonuses, please sign up to our mailing list: http://bit.ly/19sKMZb
pcotgrove
 Posted Thu 4 Apr 2013
Supreme Being

Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)Supreme Being - (19,236 reputation)

Group: Forum Members
Last Active: Sat 18 May 2013
Posts: 0, Visits: 57
Hi Paul

Applied the changes and the email gets sent using the template but it all appears as clear text and not translated into HMTL as below:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">



<html xmlns="http://www.w3.org/1999/xhtml"xml:lang="en" lang="en">



<head>



<styletype="text/css">



#mail_container { border: solid 1px#999; padding: 20px; width: 600px; }



#mail_container p { font-family: Segoe UI,Arial, Helvetica, Sans-Serif; font-size: 13px; font-weight: normal; color:#555; padding: 5px 0; margin: 0; }



#mail_container h1 { font-family:"Segoe UI Light", Segoe UI, Arial, Helvetica, Sans-Serif;font-weight: lighter; font-size: 180%; letter-spacing: 0px; color: #000;margin: 3px 0 10px 0; }



#mail_container h2 { font-family:"Segoe UI Light", Segoe UI, Arial, Helvetica, Sans-Serif;font-weight: lighter; font-size: 140%; letter-spacing: 0px; color: #000;margin: 8px 0 3px 0; }



#mail_container .line { margin-top: 10px;border-bottom: solid 1px #ccc; }



#mail_container table { background-color: #eee;border-collapse: collapse; width: 100%; }



#mail_container th { background-color: #fff;border-bottom: solid 1px #000; padding: 2px 5px; text-transform: uppercase;font-size: 85%; color: #999; }



#mail_container td { border-bottom: solid1px #ccc; padding: 2px 5px; text-align: right; }



#mail_container .col1 { text-align: left; }



#mail_container .col2 { text-align: right;vertical-align: top; }



#mail_container .row_totals strong { color: #f00; }



#mail_container .row_item .col2 { color: #090; }



#mail_container a { font-weight: normal;color: #04d; }



</style>



<metahttp-equiv="content-type" content="text/html;charset=utf-8" />



<title>NewAccount on xyz.com</title>



</head>



<body>





<divid="mail_container">



<h1class="email_text"></h1>



<pclass="email_text">



Anaccount has been created for you on xyz. If you return to ourshop, use the details below to log in:



</p>





<br/>





<p>Email Address: [email protected]</p>



<p>Password: xxxxx</p>





<br/>





<pclass="email_text">To view your account, go to <a href="http://xyz.com/customer.aspx">http://xyz.com/customer.aspxhttp://xyz.com/customer.aspx</a>.



Here you can view your orders,change your password, and see any saved shopping baskets.</p>





<br/>



<h2class="email_text">



</h2>



<pclass="email_text">Hope to see you again soon!</p>



</div>



</body>



</html>

Paul
 Posted Thu 4 Apr 2013
große Käse

große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)große Käse - (508,182 reputation)

Group: Administrators
Last Active: Tue 10 Sep 2024
Posts: 807, Visits: 2,748
Medz updated the email templating to include the new customer... this is the changeset on codeplex:

http://kartris.codeplex.com/SourceControl/changeset/22910

This should be in the next build 2.0002, maybe 1-2 weeks time, but you can grab the updated code from the repository now.


--
If my post solves your issue, can you 'Mark as Answer' so it's easier for other users to find in future.

If you would like to be informed of new features, new releases, developments and occasional special bonuses, please sign up to our mailing list: http://bit.ly/19sKMZb

Similar Topics

Expand / Collapse

Reading This Topic

Expand / Collapse

Back To Top