0

I have a piece of code that works and do:

Reads a Database , reads a template (template.htm), put data in a new file based in the template (evento.htm), read that file and send an email with the content of the file generated. Code below (I cut the database part):

<%
    NomeDoTemplate= "template.htm"
    CaminhoDoTemplate= Server.MapPath(NomeDoTemplate)
    CaminhoDoTemplateAjustado= Mid(CaminhoDoTemplate,1,InStrRev(CaminhoDoTemplate,"\"))
    NomeDoArquivo= "evento.htm"
    CaminhoDoArquivo= Server.MapPath(NomeDoArquivo)

    Set ManipulacaoDeArquivo= Server.CreateObject("Scripting.FileSystemObject")
    Set ObjetoArquivo= ManipulacaoDeArquivo.OpenTextFile(CaminhoDoTemplate, 1)
    DadosDoObjetoArquivo= ObjetoArquivo.ReadAll
    ObjetoArquivo.Close

    DadosDoObjetoArquivo= Replace(DadosDoObjetoArquivo, "[Cliente]", Um)

    Set ObjetoArquivo= ManipulacaoDeArquivo.CreateTextFile(CaminhoDoTemplateAjustado & NomeDoArquivo)
    ObjetoArquivo.Write(DadosDoObjetoArquivo)

    Set ObjetoArquivo= ManipulacaoDeArquivo.OpenTextFile(CaminhoDoTemplateAjustado & NomeDoArquivo, 1)
    DadosDoObjetoArquivo= ObjetoArquivo.ReadAll

    Dim objCDOSYSMail
    Dim objCDOSYSCon

    Set objCDOSYSMail = Server.CreateObject("CDO.Message")
    Set objCDOSYSCon = Server.CreateObject ("CDO.Configuration")

    objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.server.com"
    objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user_id"
    objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
    objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30
    objCDOSYSCon.Fields.update

    Set objCDOSYSMail.Configuration = objCDOSYSCon
    objCDOSYSMail.From = "ABC <[email protected]>"
    objCDOSYSMail.To = "[email protected]"
    objCDOSYSMail.Subject = "Contato"
    objCDOSYSMail.HTMLBody= DadosDoObjetoArquivo
    objCDOSYSMail.Send

    Set objCDOSYSMail = Nothing
    Set objCDOSYSCon = Nothing
%>

I would like to make this simple, skiping the step of generating the file in the disk. I would like to:

Read a Database, reads a template, put data in memory, send the mail with that data in memory.

Thanks

2 Answers 2

1

If I see it correctly, all you have to do is skip the part where you save the file and re-read it... I have refactored your code, gave the variables some english names so I could see what's going on, and commented out the lines you don't need:

<%
    Dim TemplateName     : TemplateName = "template.htm"
    Dim TemplateFullPath : TemplateFullPath = Server.MapPath(TemplateName)
    Dim TemplatePath     : TemplatePath = Mid(TemplateFullPath,1,InStrRev(TemplateFullPath,"\"))

    Dim ArchiveName      : ArchiveName = "evento.htm"
    Dim ArchiveFullPath  : ArchiveFullPath = Server.MapPath(ArchiveName)

    Dim FSO, TemplateFile, TemplateText
    Set FSO = Server.CreateObject("Scripting.FileSystemObject")
        Set TemplateFile = FSO.OpenTextFile(TemplateFullPath, 1)
            TemplateText = TemplateFile.ReadAll()
            TemplateText = Replace(TemplateText, "[Cliente]", Um)
            TemplateFile.Close()

'   Really simple - to do this in-memory, simply don't save and re-read the file....

'        Set TemplateFile = FSO.CreateTextFile(TemplatePath & ArchiveName)
'            TemplateFile.Write(TemplateText)

'        Set TemplateFile = FSO.OpenTextFile(TemplatePath & ArchiveName, 1)
'            TemplateText = TemplateFile.ReadAll


        Set TemplateFile = Nothing
    Set FSO = Nothing

    Dim objCDOSYSMail, objCDOSYSCon
    Set objCDOSYSMail = Server.CreateObject("CDO.Message")
        Set objCDOSYSCon = Server.CreateObject ("CDO.Configuration")
            objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.server.com"
            objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
            objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
            objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
            objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user_id"
            objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
            objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30
            objCDOSYSCon.Fields.update

            Set objCDOSYSMail.Configuration = objCDOSYSCon
                objCDOSYSMail.From = "ABC <[email protected]>"
                objCDOSYSMail.To = "[email protected]"
                objCDOSYSMail.Subject = "Contato"
                objCDOSYSMail.HTMLBody= TemplateText
                objCDOSYSMail.Send
            Set objCDOSYSMail.Configuration =  Nothing
        Set objCDOSYSMail = Nothing
    Set objCDOSYSCon = Nothing
%>

Hope this helps,

Erik

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the anwser and for been so kinda. have a nice day!
1

you could use several techniques:

  • write your own stringbuilder class
  • use the .net system.io.stringwriter class (yes you can use this from classic asp)
  • use the adodb.stream object

example stringwriter:

set sw = server.createObject("system.io.stringwriter")
sw.write_12( DadosDoObjetoArquivo )

objCDOSYSMail.HTMLBody = sw.getStringBuilder().toString()

example (adodb.stream):

set stream = server.createobject("ADODB.Stream")
with stream 
    .Open
    .WriteText DadosDoObjetoArquivo
end with

objCDOSYSMail.HTMLBody = stream.ReadText
stream.Close

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.