I am using the Indy TIdHTTP component and am successfully able to send emails via the sendgrid API using the following function. The sendgrid api documentation can be found here.
Now I am tasked with including file attachments. In the API documentation it states: The file contents must be part of the multipart HTTP POST
I have attempted to modify my function to include the TIdMultipartFormDataStream with no success.
How should I modify the code to support file attachments?
procedure SendGridEmailProc;
var
IdHTTP1: TIdHTTP;
IdSSLIOHandlerSocket1: TIdSSLIOHandlerSocketOpenSSL;
mString: string;
mParams: TStringList;
i: Integer;
begin
try
mParams := TStringList.Create;
IdHTTP1 := TIdHTTP.create(nil);
IdSSLIOHandlerSocket1 := TIdSSLIOHandlerSocketOpenSSL.create(nil);
with IdSSLIOHandlerSocket1 do begin
SSLOptions.Method := sslvSSLv3;
SSLOptions.Mode := sslmUnassigned;
SSLOptions.VerifyMode := [];
SSLOptions.VerifyDepth := 2;
end;
with IdHTTP1 do begin
IOHandler := IdSSLIOHandlerSocket1;
end;
mParams.Add('api_user=xxxxx');
mParams.Add('api_key=xxxxx');
mParams.Add('to=xxxxxx');
mParams.Add('from=xxxxx');
mParams.Add('subject=test:'+datetimetostr(now));
mParams.Add('text=this is a test');
IdHTTP1.Post('https://sendgrid.com/api/mail.send.xml',mParams);
finally
mParams.Free;
idhttp1.free;
IdSSLIOHandlerSocket1.Free;
end;
end;