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.