This is my first time I do anything with SMTP, so please have patience if I am doing something terribly wrong here :) Normally, I just use PHPMailer, but this is not an option in this case.
I am trying to send an email to people in a mailing list. I am using fsockopen and I need to use a TLS connection to do this.
This is what I do:
- $conn = fsockopen() // my details goes in there, it connects.
- HELO myhost.com (fwrite)
- STARTTLS (fwrite)
- stream_socket_enable_crypto( $conn, true, STREAM_CRYPTO_METHOD_TLS_CLIENT );
- HELO myhost.com (fwrite)
- AUTH LOGIN (fwrite)
- base64_encoded USERNAME (fwrite)
- base64_encoded PASSWORD (fwrite)
- MAIL FROM: (fwrite)
- RCPT TO:; (fwrite)
- DATA (fwrite)
- A string with all my headers (fwrite)
- fclose($conn);
This is where something goes wrong
After using an encypted connection I can't seem to successfully read the server response. For example, when trying to read the response by using fgets($conn,1024)
after my authentication it takes forever and then finally I will get a highly encrypted code not being able to look for response "235" (to make sure my authentication succeeded).
And then when I write MAIL FROM my response will look like this:
.0.0 SMTP server ready
While I think I would want something like this:
250 2.0.0
Something gets wrong, but I don't know why. I hope any of all you experts out there can help me go to the bottom with this problem :)
Thanks in advance!
SOLVED IT
In case anyone else runs into this problem.
This order works
- $conn = fsockopen() // my details goes in there, it connects.
- EHLO myhost.com (fwrite)
- STARTTLS (fwrite)
- stream_socket_enable_crypto( $conn, true, STREAM_CRYPTO_METHOD_TLS_CLIENT );
- EHLO myhost.com (fwrite)
- AUTH LOGIN (fwrite)
- base64_encoded USERNAME (fwrite)
- base64_encoded PASSWORD (fwrite)
- MAIL FROM: (fwrite)
- RCPT TO:; (fwrite)
- DATA (fwrite)
- A string with all my headers (fwrite)
- fclose($conn);
Between every call I added the following:
stream_set_timeout($conn, 300);
set_time_limit(310);
Suddenly I got the expected replies from the server and the mail was successfully sent.