2016-12-05 1 views
2

내 DB에는 전자 메일 템플릿 인 Notes Document가 있습니다. 그들은 이름, 하나의 첨부 파일 필드, html 필드를 가지고 있습니다.MIME을 사용하여 전자 메일에서 HTML 및 첨부 파일의 순서 제어

이 문서를 잡고 HTML 및 첨부 파일이있는 내부 전자 메일을 생성 할 수있는 에이전트를 코딩하려고합니다.

내 코드가 작동하지만 이메일은 다음과 같습니다

enter image description here

나는 상단에있는 첨부 파일을 원하지 않는다. 나는 그것을 html에 포함시키는 것을 선호 할 것이다. html 텍스트를 가져올 때 사용자 이름과 같은 일부 값을 바꿔 이메일이 동적이 될 수 있습니다. 난 그냥 "REPLACE1 |" 내가 어디에 사용자의 이름을 넣고 싶습니다. "FILEREPLACE1 |"과 함께 html을 작성하는 방법이 있습니까? 그래서 파일을 동적으로 첨부 할 수 있습니까?

그렇지 않은 경우 첨부 파일을 이메일 하단에 어떻게 든 삽입 할 수 있습니까?

LS 클래스 "ClsEmail"

%REM 
    Library ClsEmail 
    Created Dec 4, 2016 by Bryan Schmiedeler/Scoular 
    Description: Comments for Library 
%END REM 
Option Public 
Option Declare 

Const ERR_EMAIL_MODIFICATION_NOT_ALLOWED = "You can not make changes to an email once it has been sent." 

Dim emlView As NotesView 
Dim emlDoc As NotesDocument 
Dim object As NotesEmbeddedObject 
Dim docUNID As String 
Dim fleNme As String 

Dim bodyChild As NotesMIMEEntity 
Dim hdr As NotesMIMEHeader 
Dim Success As Boolean 

Class Email 

     Private session As NotesSession 
     Private doc As NotesDocument 
     Private body As NotesMIMEEntity 
     Private mh As NotesMIMEHeader 
     Private mc As NotesMIMEEntity 
     Private ma As NotesMIMEEntity 
     Private stream As NotesStream 
     Private isTextSet As Boolean 
     Private isHTMLSet As Boolean 
     Private isStyleSet As Boolean 
     Private isRebuildNeeded As Boolean 
     Private isMailBuilt As Boolean 
     Private rtitem As NotesRichTextItem 
     Private str_TextPart As String 
     Private str_HTMLPart As String 
     Private str_DefaultStyles As String   
     Private str_Styles As String 
     Private FromName(0 To 2) As String 

    Sub New() 
     Set Me.session = New NotesSession() 
       'Set Me.elmTrmDoc = nothing 
       'Set me.unid = "" 
       Set Me.doc = Me.session.Currentdatabase.CreateDocument 
       Me.doc.Form = "Memo" 

       Me.FromName(0) = "Sender's Name" 
       Me.FromName(1) = "[email protected]" 
       Me.FromName(2) = "DOMAIN" 

       Me.str_DefaultStyles = "body{margin:10px;font-family:verdana,arial,helvetica,sans-serif;}" 

       Me.isTextSet = False 
       Me.isHTMLSet = False 

       Me.isRebuildNeeded = True 
       Me.isMailBuilt = False 

     End Sub 

     Property Set Subject As String 
       Me.doc.subject = subject 
     End Property 

     Property Set unid As String 
       Me.unid = unid 
     End Property 

     Property Set Plain 
       Me.str_TextPart = Plain 
       Me.isTextSet = True 

       Me.isRebuildNeeded = True 
     End Property 

     Property Get Plain 
       Plain = Me.str_TextPart 
     End Property 

     Property Set HTML 
       Me.str_HTMLPart = HTML 
       Me.isHTMLSet = True   
       Me.isRebuildNeeded = True 
     End Property 

     Property Get HTML 
       HTML = Me.str_HTMLPart 
     End Property 

     Property Set Styles As String 
       Me.str_Styles = Styles 
       Me.isStyleSet = True 

       Me.isRebuildNeeded = True 
     End Property 

     Property Set CSS As String 
       Me.Styles = CSS 
     End Property 

     Property Set Sender As Variant 
       Me.FromName(0) = Sender(0) 
       Me.FromName(1) = Sender(1) 
       Me.FromName(2) = Sender(2)   
       Me.isRebuildNeeded = True 
     End Property 

     Property Set ReplyTo As String 
       Me.Doc.ReplyTo = ReplyTo 

       Me.isRebuildNeeded = True 
     End Property 

     Property Set CopyTo As String 
       Me.Doc.CopyTo = CopyTo 

       Me.isRebuildNeeded = True 
     End Property 

     Property Set BlindCopyTo As String 
       Me.Doc.BlindCopyTo = BlindCopyTo 

       Me.isRebuildNeeded = True 
     End Property 

     Sub Rebuild 

       If Me.doc.HasItem("Body") Then 
         Call Me.doc.RemoveItem("Body") 
       End If 

       If Me.isHTMLSet Then 'Send mulipart/alternative 

         'Create the MIME headers 

         Me.session.convertMIME = False 
         Set Me.body = Me.doc.CreateMIMEEntity("Body")    
         Set Me.mh = Me.body.CreateHeader({MIME-Version}) 
         Call Me.mh.SetHeaderVal("1.0")        
         Set Me.mh = Me.body.CreateHeader("Content-Type") 
         Call Me.mh.SetHeaderValAndParams({multipart/alternative;boundary="=NextPart_="}) 

         'Now send the HTML part. Order is important! 
         Set Me.mc = Me.body.createChildEntity() 
         Set Me.stream = Me.session.createStream()  
         Set Me.mc = Me.body.createChildEntity() 
         ' Call stream.WriteText(Replace(Me.str_HTMLPart, ">", ">"+Chr(10))) 
         Call stream.WriteText(Me.str_HTMLPart) 

         'Extract Attachment and add to this email 
         Set emlView = Me.session.Currentdatabase.Getview("xpViewEmailsAll") 
         Set emlDoc = emlView.getFirstDocument 
         Dim obj As NotesEmbeddedObject 
         Set obj = emlDoc.GetAttachment("To Terminate an Employee (manager).pdf") 
         fleNme = "c:/temp/" + obj.Name() 
         Call obj.ExtractFile(fleNme) 
         Set bodyChild = Me.body.Createchildentity() 
         Success = MimeAttachFileAsBase64(bodyChild,"c:/temp/",obj.Name()) 

         'Remove File 
         'Kill fleNme 

         Call Me.mc.setContentFromText(Me.stream, {text/html;charset="iso-8859-1"}, ENC_NONE) 
         Call Me.doc.Closemimeentities(True) 
         Me.session.convertMIME = True 
       End If 


       Me.doc.Principal= Me.FromName(0) +" <"+Me.FromName(1)+"@"+Me.FromName(2)+">" 
       Me.doc.InetFrom = Me.FromName(0) +" <"+Me.FromName(1)+">" 
       Me.isMailBuilt = True 
       Me.isRebuildNeeded = False 
     End Sub 


     Sub Send(sendTo As String) 

       If Me.isMailBuilt And Me.isRebuildNeeded Then 
         Error 1000, ERR_EMAIL_MODIFICATION_NOT_ALLOWED 
       ElseIf Not Me.isMailBuilt Then 
         Call Me.Rebuild() 
       End If  
       Me.Doc.SendTo = SendTo 
       Call Me.Doc.Send(False) 

     End Sub 
End Class 
Function MimeAttachFileAsBase64(mime As NotesMIMEEntity, sFolderPath As String, sFileName As String) As Boolean 

On Error GoTo ERRHANDLER 
Dim sess As New NotesSession 
Dim nsFile As NotesStream 
Dim bodyChild As NotesMIMEEntity 
Dim header As NotesMIMEHeader 
Dim sContentType As String 
Dim MimeAttachFile As Boolean 

MimeAttachFile = False 

Set nsFile = sess.CreateStream() 

If Not nsFile.Open(sFolderPath & sFileName, "Binary") Then 
Print "MimeAttachFileAsBase64 Error: Failed to open file: " & sFolderPath & sFileName 
Err = 0 
Exit Function 
End If 

Set bodyChild = mime.CreateChildEntity() 
sContentType = |application/octet-stream| 
Call bodyChild.SetContentFromBytes (nsFile,sContentType & |; name="| & sFileName & |"|, ENC_NONE) 
Call bodyChild.EncodeContent(ENC_BASE64) 
Set header = bodyChild.createHeader("Content-Disposition") 
Call header.SetHeaderVal(|attachment; filename="| & sFileName & |"|) 
Call nsFile.Close() 
Set nsFile = Nothing 
MimeAttachFile = True 

Exit Function 

ERRHANDLER: 

Print "MimeAttachFileAsBase64 Error: " & Format$(Err) & " " & Error & " # Line: " & Format$(Erl) 

Err = 0 

Exit Function 

End Function 

에이전트 코드 :

Sub Initialize 

    Dim session As New NotesSession 
    Dim doc As NotesDocument 
    Dim db As NotesDatabase 
    Dim emlView As NotesView 
    Dim emlDoc As NotesDocument 
    Dim rti As NotesRichTextItem 
    Dim unfTxt As String 
    Dim unfTxt2 As String 
    Dim empNme As String 
    Dim docUNID As String 

    Set db = session.CurrentDatabase 
    Dim agent As NotesAgent 
    Set agent=session.Currentagent 

    'Get a handle On term 
    Set doc = db.GetDocumentByUnid("D321286EADF83DA78625807C006A7A84") 
    'Set doc = db.GetDocumentByID(agent.ParameterDocID) 

    'Get a handle on HTML Doc 
    Set emlView = db.Getview("xpViewEmailsAll") 
    Set emlDoc = emlView.getFirstDocument 
    unfTxt = emlDoc.emlBdyTxt(0) 

    empNme = doc.EmployeeName(0) 
    docUNID = emlDoc.Universalid 

    'Run REPLACE1 
    unfTxt2 = ReplaceSubstring(unfTxt,"|REPLACE1|",empNme) 

    Dim mail As New Email() 
    mail.Subject = "Termination Notification for " + empNme 
    mail.HTML = unfTxt2 
    mail.CSS = "p{margin:2em}" 
    mail.Sender = Split("IT Help Desk,[email protected],Scoular", ",") 
    mail.unid = docUNID 
    mail.Send("[email protected]") 

End Sub 
+0

IMHO이 기능은 작동하지 않습니다. HTML/Mime Mail은 첨부 파일을보기 좋게 처리합니다. 첨부 파일을 메일에 넣는 것은 notesrichtext와 함께 작동합니다 ... – umeli

답변

2

나는 이메일 클래스 사랑! 좋은 개념. 나는 그것이 당신을 위해 일했으면 좋겠다.

답변이 없지만 해결책이있을 수 있습니까? 가능한 경우 첨부 파일 대신 중앙에서 저장된 링크가 있도록 전자 메일을 변경하는 것이 좋습니다! 앞에서 말했듯이 내부 이메일이라면 중복을 만드는 이유는 무엇입니까?

/DatabasePath/xpViewEmailsAll/$First/$File/To%20Terminate%20an%20Employee%20(manager).pdf에 링크 할 수 있지만 대신/Database/AttachmentRequests/To에 링크하는 것이 좋습니다. 20Terminate % 20an % 20Employee % 20 (manager) .pdf 여기서 "AttachmentRequests"는 양식 수식이있는보기입니다. 첫 번째 정렬 된 열에는 제공되는 각 파일의 이름이 들어 있으며 지정된 양식은 열기를 가로 채고 대신 첨부 파일 자체를 실행합니다. 그렇게하면 Notes 또는 웹 클라이언트에서 작동 할 수 있으며 예측할 수 없을 정도로 혼란스러운 미래에 필요가 생기면 행동을 변경하여 "THIS HAS MOVED"라고보고하거나 일부 페이지의 올바른 페이지로 전달할 수 있습니다 존재하는 인트라넷 사이트.

+0

사실 나는 당신이 제안한 것을 친절하게했습니다. 첨부 파일에는 텍스트와 그림이 있으므로 첨부 파일을 HTML로 가져 왔습니다. 첨부 파일이있는 HTML 이메일을 여전히 메일로 보내고 싶지만 오히려 좋습니다. 언젠가는. –

+0

$ V2AttachmentOptions 필드를 사용하여 첨부 파일을 숨길 수 있습니까? 왜 내가 전에 그렇게 생각하지 않았는지 모르겠다. 그것이 효과가 있을지 전혀 모른다. – JSmart523

관련 문제