0

I'm experimenting with sending emails through a bulk email sender via their API.

The code that works at the moment to send a simple email is this (INetHttp1 is of type TINetHttp):

procedure TForm1.GenerateSendMailRequest;
var
  fullURL, s : string;
  Stream: TIdMultiPartFormDataStream;
begin
  try
    Stream.AddFile('attachfile','C:\Users\Admin\Documents\age breakdown plot.pdf','<multipart/alternative>');
  except
    on E:Exception do
      Writeln(E.Classname, ': ', E.Message);
  end;

  fullURL := 'https://api.elasticemail.com/v2/email/send'
            +'?apikey=e123456-cacb-4456-112a-d145e7f55a47'
            +'&subject=test 3 from elastic mail'
            +'&from=myemail.co.uk'
            +'&To=recipient1@hotmail.com;recipient2@gmail.com'    
            +'&bodyText=this is the body of the mail';

  //?? how do I get the stream into the URL?

  try
    INetHttp1.Flags := [flSecure];
    INetHttp1.Verb := veGet;
    INetHttp1.Url := fullURL;
    INetHttp1.Open;
    INetHttp1.OpenRequest;
    INetHttp1.SendRequest;
    s := string(INetHttp1.ReadData);
  finally
    INetHttp1.Close;
    Stream.free
  end;

  ShowMessage(s) //show server reponse
end;

To include an attachment (mine will be a PDF), their API documentation says:

Attach the file as POST multipart/form-data file upload

One of the examples says

You need to provide your attachments as the Multipart/Form-Data POST field in your Send request

But I am not at all clear on how to do this in Delphi 2009. I've seen StackOverflow answers that mention it, and in particular this post explains how to make a TIdMultiPartFormDataStream.

Please, can somebody help me with the code to get that stream 'inserted' into the url that I send to the API?

A comment in another StackOverflow question said the OP should give a link to the API docs, so here it is, and here are some examples in languages which I'm not familiar with, although I can sort of understand the C# one.

I've looked at this question and this one, plus lots of others, but cannot see any relevant examples using Delphi 2009.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user2834566
  • 775
  • 9
  • 22
  • 2
    The code you say "works" doesn't actually work, because you are not creating the `TIdMultipartFormDataStream` object before adding content to it. Also, if you are going to use Indy anyway, why not use its `TIdHTTP` component? It has a `Post()` overload specifically for `TIdMultipartFormDataStream`. In any case, `multipart/form-data` cannot be sent in a URL, it must be send in an HTTP message body instead. So you need to check if/how `TInetHttp` allows you to set the body data using a `TStream` when sending a request. `TInetHttp` is a 3rd party component, and I can't find documentation for it. – Remy Lebeau Mar 22 '19 at 00:51
  • @ Remy, as usual helpful and constructive comments. By 'works' I meant it sends an email (first ever one I've tried using an API call). I added TIdMultipartFormDataStream afterwards but got stuck re what to do with it, hence the post. I'd rather not use Indy if I can help it as I have had complications with it running on different PCs. Didn't realise TInetHttp was 3rd party, must've been installed a longtime ago! I shouldn't have asked a question that used it. So I need to build a complete message first and send that as the msg part of the API call. I'll search SO for how I build the msg. – user2834566 Mar 22 '19 at 07:49
  • if you are going to build the `multipart/form-data` manually, I suggest you read [RFC 7578](https://tools.ietf.org/html/rfc7578) – Remy Lebeau Mar 22 '19 at 16:27

0 Answers0