0

I'm trying to send smtp request to my smtp server installed in freebsd and read the successful or fail response? This is my code :

$url = '192.168.1.227:25/';
    $user = 'admin';
    $pass = '123';

    $params = array(
        'User'      => $user,
        'passwor'   => $pass,
        'to'        => 'example1@example.com',
        'subject'   => 'test',
        'html'      => 'body',
        'text'      => 'text',
        'from'      => 'example2@example.com',
      );


    $session = curl_init($url);
    curl_setopt ($session, CURLOPT_POST, true);
    curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
    curl_setopt($session, CURLOPT_HEADER, 1);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($session);
    $info = curl_getinfo($session);
    curl_close($session);
    echo $response;
    print_r($info);
Maysam
  • 523
  • 4
  • 16
  • You can use this example: https://gist.github.com/hdogan/8649cd9c25c75d0ab27e140d5eef5ce2 But in case of error, it seems that you can't got it. – luigifab Jul 01 '22 at 14:23

1 Answers1

3

cURL is not the right tool for this. Although it does support simple SMTP connections, it's generally not designed for more advanced use cases.

cURL is designed for HTTP, where you usually have one request with a bunch of parameters (the HTTP request headers) and one response.¹

SMTP communication however goes more like “ping-pong”. To implement a SMTP client, you would have to work on raw TCP sockets: Write a SMTP command on the socket, read/process the response, write the next command, and so on.

Also, SMTP, as it's currently implemented, has got a lot of quirks and weird side aspects across servers and clients. For example, the PHP/cURL-based approach would not be able to support greylisting, unless you would manage your own queue for temporarily rejected mail.

Unless you have a good reason to do so (and a lot of time, as well as high frustration tolerance), you should use a client library like Swiftmailer together with a real MTA for outgoing mail.

If, on the other hand, you're just curious to play with a raw connection to a mail server, use a tool like netcat from the command line and work through a howto of such a scenario.

..

¹ if you leave keep-alive etc. aside for a moment.

Community
  • 1
  • 1
lxg
  • 12,375
  • 12
  • 51
  • 73