Using Indy TidHttp Component to Send Email File Attachments via Sendgrid

I am using the Indy TIdHTTP component and can successfully send emails via the sendgrid API using the following function. The sendgrid api documentation can be found here .

I am now instructed to enable file attachments. In the API documentation it states: File content must be part of a multipart HTTP POST

I tried to change my function to include TIdMultipartFormDataStream with no success.

How do I change 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;

      

+3


source to share


1 answer


Try doing it this way (note that it is untested):



uses
  IdException, IdHTTP, IdSSLOpenSSL, IdMultipartFormData;

procedure SendGridEmailProc;
var
  HTTPClient: TIdHTTP;
  Parameters: TIdMultiPartFormDataStream;
  SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
  HTTPClient := TIdHTTP.Create(nil);
  try
    SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
    try
      SSLHandler.SSLOptions.Method := sslvSSLv3;
      SSLHandler.SSLOptions.Mode := sslmUnassigned;
      SSLHandler.SSLOptions.VerifyMode := [];
      SSLHandler.SSLOptions.VerifyDepth := 2;
      HTTPClient.IOHandler := SSLHandler;

      Parameters := TIdMultiPartFormDataStream.Create;
      try
        Parameters.AddFormField('api_user', 'xxxxx');
        Parameters.AddFormField('api_key', 'xxxxx');
        Parameters.AddFormField('to', 'xxxxxx');
        Parameters.AddFormField('from', 'xxxxx');
        Parameters.AddFormField('subject', 'test:' + DateTimeToStr(Now));
        Parameters.AddFormField('text', 'this is a test');

        // note, that you need to use the same file name for the files[]
        // identifier and that you should explicitly specify the content 
        // type for a file field to meet the HTTP dump shown in the help
        Parameters.AddFile('files[file.jpg]', 'c:\file.jpg', 
          'application/octet-stream');

        try
          HTTPClient.Post('https://sendgrid.com/api/mail.send.xml', 
            Parameters);
        except
          on E: EIdException do
            ShowMessage(E.ClassName + ': ' + E.Message);
        end;
      finally
        Parameters.Free;
      end;
    finally
      SSLHandler.Free;
    end;
  finally
    HTTPClient.free;
  end;
end;

      

+3


source







All Articles