On Mon, 10 May 2021 11:40:41 +0200 (CEST), Michael Van Canneyt via fpc-pascal <fpc-pascal@lists.freepascal.org> wrote:
> > >On Mon, 10 May 2021, Bo Berglund via fpc-pascal wrote: > >> >> Not so simple when you read scores of Internet pages on the subject... > >99% of what is written on internet is junk or outdated. > >OK, maybe it's only 98%. > >Check the synapse sources, that's all you need. Yes it really looks like the SendToRaw, SendToEx and SendTo functions at the bottom are intended as examples... I wonder if this is the structure elsewhere too... >The handling of attachments is well explained on the synapse homepage/wiki. > >http://www.ararat.cz/synapse/doku.php/public:howto >http://www.ararat.cz/synapse/doku.php/public:howto:mimeparts > >> Where is the actual sending of the email happening? >> And how do I add the attachments? > >It is explained in my article. Nothing has changed in this regard. >Didn't you read the section on attachments ? Yes I did but it involved calling the SendToRaw function inside Synapse at the end and therefore hiding the SMTP object where the SSL stuff has to be set... >in the below, aMailData is a record with MailBody, MailHTML strings, and a >list of >recipients 'Recipients' and attachment filenames in Attachments. > >var > Msg: TMimeMess; > Body,Html : TStringList; > H,PM,P,Rel,Alt : TMimePart; > Recips : TArray<String>; > >begin > Msg.Header.From := FSMTP.Username; > Msg.Header.Subject:=aMailData.Subject; > for S in Recips do > Msg.Header.ToList.Add(S); > if (aMailData.MailBody<>'') and (aMailData.MailHTMl<>'') then > begin > PM:=Msg.AddPartMultipart('mixed',Nil); > Rel := Msg.AddPartMultipart('related', P); > Alt := Msg.AddPartMultipart('alternative', Rel); > P:=Alt; > end; > if aMailData.MailBody<>'' then > begin > Body:=TStringList.Create; > Body.Text:=aMailData.MailBody; > Body.WriteBOM:=False; > Msg.AddPartText(Body,P); > end; > if aMailData.MailHTMl<>'' then > begin > HTML:=TStringList.Create; > HTML.WriteBOM:=False; > HTML.Text:=aMailData.MailHTMl; > H:=Msg.AddPartHTML(HTML,P); > H.ConvertCharset:=False; > // H.EncodingCode:=ME_BASE64; > H.EncodePart; > H.EncodePartHeader; > end; > // Todo : attachments > // Add all attachments, assume Attachments is a stringlist with > // filenames > For I:=0 to aMailData.Attachments.Count-1 do > Msg.AddPartBinaryFromFile(Attachments[I],PM); > // Compose message > Msg.EncodeMessage; > if not FSMTP.Login then > Raise EDaemon.CreateFmt(SErrLoginFailed,[Fsmtp.EnhCodeString]); > //If you successfully pass authorization to the remote server > If Not FSMTP.AuthDone Then > Raise EDaemon.CreateFmt(SErrLoginFailed,[Fsmtp.EnhCodeString]); > //Send MAIL FROM SMTP command to set senders e-mail address. > If Not FSMTP.MailFrom('ourmail@ourcompany', Length(Msg.Lines.text)) then > Raise EDaemon.CreateFmt(SErrMailFromFailed,[Fsmtp.EnhCodeString]); > For S in aMailData.Recipients do > begin > If Not FSMTP.MailTo(S) Then > Raise EDaemon.CreateFmt(SMailtoFailed,[Fsmtp.EnhCodeString]); > if not FSMTP.MailData(Msg.Lines) Then > Raise EDaemon.CreateFmt(SMailDataFailed,[Fsmtp.EnhCodeString]); > end; > >The above is actual code from a program currently in production. >This together with the sample from the article should be plenty to create >your own code. > >Michael. Thanks! Much obliged! Seems like FSMTP is a globally available object managed somewhere else... And that is where the SSL handling is defined too. So it is set up to send a lot of emails. In my case the mail sending is done in the end of the life of a task execution thread so it will not live on further. Slowly wrapping my head around Synapse, never used it before. I have used Indy on Delphi since 20 years or so and continued that path on FreePascal/Lazarus. But it does not handle openssl above 1.0.2.... :( -- Bo Berglund Developer in Sweden _______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal