RTC Forums

Subscription => Support => Topic started by: giancarlo on January 04, 2017, 05:36:07 AM



Title: Send email via Mailgun API
Post by: giancarlo on January 04, 2017, 05:36:07 AM
Hi Danijel

I need to send an email via the Mailgun API. I modify the rtcClientFormPost sample to complete my test:

The Mailgun Api give the following example:

Code:
curl -s --user 'api:YOUR_API_KEY' \
    https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
    -F from='Excited User <YOU@YOUR_DOMAIN_NAME>' \
    -F to='foo@example.com' \
    -F cc='bar@example.com' \
    -F bcc='baz@example.com' \
    -F subject='Hello' \
    -F text='Testing some Mailgun awesomness!' \
    --form-string html='<html>HTML version of the body</html>'

Then I did the following in the rtcClientFormPost sample:

Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
  RtcHttpClient1.ServerAddr := 'mailgun server IP';
  RtcHttpClient1.ServerPort := '443';
  RtcHttpClient1.UseSSL := True;

  RtcDataRequest1.Request.Method:='POST';
  RtcDataRequest1.Request.FileName:='/v3/mg.mydomain.com/messages';
  RtcDataRequest1.Request.Host := 'https://api.mailgun.net';
  RtcDataRequest1.Post(); // Post the request to the request queue
end;


procedure TForm1.RtcDataRequest1BeginRequest(Sender: TRtcConnection);
  var
    Cli:TRtcDataClient absolute Sender;
    sHtml: string;
  begin
  Cli.Request.Params.Value['user']    := URL_Encode('api:key-X9t8s6sxxyh6daxuxexggbg51p765rgggc0');
  Cli.Request.Params.Value['from']    := URL_Encode('mg.mydomain.com');
  Cli.Request.Params.Value['to']      := URL_Encode('foo@example.com');
  Cli.Request.Params.Value['cc']      := URL_Encode('boo@example.com');
  Cli.Request.Params.Value['subject'] := URL_Encode('Test RTC-Mailgun');
  Cli.Request.Params.Value['text']    := URL_Encode('Testing Sending email with RTC');
  sHtml := URL_Encode('<html>HTML version of the body</html>');
  Cli.Write(sHtml);
end;

procedure TForm1.RtcDataRequest1DataReceived(Sender: TRtcConnection);
  var
    Cli:TRtcDataClient absolute Sender;
    mycontent:RtcString;
  begin
  // We want to wait for the whole response ...
  if Cli.Response.Done then
    begin
    // Since we have the whole response,
    // we can use a single "Read" call to get the complete content body
    mycontent:=Cli.Read;

    Memo1.Lines.Text := IntToStr(Cli.Response.StatusCode)+' '+Cli.Response.StatusText+ #13#10
                     + mycontent;
    end;
  end;

Then when i click the Post button nothing happened, the program say that "No Responding" , for 30 seconds and then it come back, but there is not message, no error, no respond, and no email.

What is wrong?, could i use RCT to send email in that way, using the Mailgun API.

Thanks in advanced.

Giancarlo


Title: Re: Send email via Mailgun API
Post by: D.Tkalcec (RTC) on January 04, 2017, 09:44:03 AM
You can send E-Mails using the RTC SDK via the Mailgun API, but you have a few errors in your code.
Since I don't know what else you've changed, the easiest way to fix your example Project is to ...

1. Load the original ClientFormPost example from the QuickStart folder.

2. Replace "Button1Click" and "RtcDataRequest1BeginRequest" implementations with the code below, to reflect the CURL example you've posted above (EXACTLY) ...

procedure TForm1.Button1Click(Sender: TObject);
  begin
  RtcHttpClient1.ServerAddr := 'api.mailgun.net';
  RtcHttpClient1.ServerPort := '443';
  RtcHttpClient1.UseSSL := True;

  RtcDataRequest1.Request.Method:='POST'; // Use the "HTTP POST" method
  RtcDataRequest1.Request.FileName:='/v3/YOUR_DOMAIN_NAME/messages';
  RtcDataRequest1.Request.Host:=RtcHttpClient1.ServerAddr; // Set the "Host" HTTP header
  RtcDataRequest1.Post(); // Post the request to the request queue
  end;

procedure TForm1.RtcDataRequest1BeginRequest(Sender: TRtcConnection);
  var
    Cli:TRtcDataClient absolute Sender;
  begin
  Cli.Request.ContentType:='application/x-www-form-urlencoded'; // important!!!
  // Set the "Authorization" parameter using mime/base64 encoding
  Cli.Request['Authorization'] := 'Basic '+Mime_Encode('api:YOUR_API_KEY');
  // Set all POST variables
  Cli.Request.Params['from']:=URL_Encode('Excited User <YOU@YOUR_DOMAIN_NAME>');
  Cli.Request.Params['to']:=URL_Encode('Foo <foo@example.com>');
  Cli.Request.Params['cc']:=URL_Encode('Bar <bar@example.com>');
  Cli.Request.Params['bcc']:=URL_Encode('Baz <baz@example.com>');
  Cli.Request.Params['subject']:=URL_Encode('Hello');
  Cli.Request.Params['text']:=URL_Encode('Testing some Mailgun awesomness!');
  // When sending body as text (line above), sending body as HTML (line below) is optional (not required) ...
  Cli.Request.Params['html']:=URL_Encode('<html>HTML version of the body</html>');
  // Use the "Write" method to send "Params.Text" ("FORM-URLENCODED" values) to the Server
  Cli.Write(Cli.Request.Params.Text);
  end;

3. Modify RED_BOLD_TEXT and (blue) E-Mail Addresses above to reflect YOUR parameters. If you skip this step (3), but everything else is correct, you should get a response from the Server "401 UNAUTHORIZED / Forbidden".

Best Regards,
Danijel Tkalcec


Title: Re: Send email via Mailgun API
Post by: giancarlo on January 04, 2017, 02:30:26 PM
Hi Danijel,

Now it works!!!  ;D

Thank you very much!!!  ;)

Giancarlo



Title: Re: Send email via Mailgun API
Post by: D.Tkalcec (RTC) on January 04, 2017, 02:34:07 PM
Good 8) Thanks for your feedback.

Best Regards,
Danijel Tkalcec