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. 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 ?

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 sender’s 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.
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to