By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

cannot send mail using PHP beyond domain

P: 3
I WAS trying to send mail using PHP from within IIS, the SMTP server had refused to send mail beyond the domain without authentication, but php mail() could not provide that. The hosting firm does not have Pear, I could not find another way ?

UPDATE/STOP PRESS: Using 'fsockopen' I was eventually able to get it working. I have pasted the working solution here.

Thank you to the people who viewed my query, but in the end I solved it myself BUT only because of the many selfless folks who inhabit the Web. In fact the best sample I found still did not work on my hosted server until it was tweaked, so I shall take a little credit.
Mar 14 '12 #1

✓ answered by Ajarn Chan

1st file, uses 2nd file [includes it] place both in same folder for simplicity, move one to /includes/ if you want to be tidy, then change path.

save the first file as 'test_auth_mail.php', or some memorable name
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <head>
  3.     <title>Simple SMTP mail test</title>
  4.   </head>
  5.   <body>
  6. Testing SMTP mail sending ...
  7. <br><hr><br>
  8.  
  9. <?php
  10. include_once('auth_mail.php');
  11. $user_email = "somebody@somewhere.com";
  12. $from = "legituser@yourdomain.com";  // Needs a valid email address able to use SMTP
  13. $subject = "Test Email";
  14. $message = "This is NOT a fluke !! IT REALLY WORKS !!";
  15. authSendEmail($from, 'yourdomain.com', $user_email, '-', $subject, $message);
  16. ?>
  17. <br><hr><br>
  18. Test completed, hopefuly IT WORKED for you too !!  :)
  19. <br>
  20. If not then double check that you setup the domain, username, password, etc correctly
  21. </body>
  22.  
save the second file as 'auth_mail.php', or some memorable name, it gets included, make sure the name matches of course
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // NOTE: this has been edited to remove private information, hopefully it still works
  4. //  if not send a private message via this site, I may try to assist you, if I am not too busy
  5.  
  6. /* * * * * * * * * * * * * * SEND EMAIL FUNCTIONS * * * * * * * * * * * * * */
  7.  
  8. // Authenticate Send - from 21st March 2005, updated 15th March 2012 .. 7 years on !!
  9. // This will send an email using auth smtp and output a log array
  10.  
  11. // OUTPUT of this script goes to your screen, comment the ECHO commands to stop that
  12.  
  13. function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
  14. {
  15.  
  16.    /* * * * CONFIGURATION START * * * */
  17.  
  18.    /* Change the following lines to reflect your smtp server settings */
  19.  
  20.    $smtpServer = "mail.yourdomain.com";
  21.    $port = "25";    // NOTE: this varies quite a lot, mine is 45, some are 25, they all vary
  22.    $timeout = "60";
  23. // NOTE - the username and password need to be sent in Base64 format, ie. emcrypted
  24. //     I found it easiest to use http://www5.rptea.com/base64/ and use the actual base64 version
  25. //   on my server the function 'base64_encode' failed to produce the correct output
  26.    $username = "validuser@yourdomain.com";
  27.    $password = "validpassword";
  28.  
  29.    $localhost = "yourdomain.com";
  30.    $newLine = "\r\n";
  31.  
  32.    /* * * * CONFIGURATION END * * * * */
  33.  
  34.    /* Connect to the host on the specified port */
  35.    $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
  36.    $smtpResponse = fgets($smtpConnect, 515);
  37.    if(empty($smtpConnect))
  38.    {
  39.        $output = "Failed to connect: $smtpResponse";
  40.        echo "$output .";
  41.        return $output;
  42.    }
  43.    else
  44.    {
  45.        $logArray['connection'] = "Connected: $smtpResponse";
  46.  
  47. echo "<br>CONNECTION: ".$logArray['connection']." ";
  48.  
  49.    }
  50.  
  51.    /* Request Auth Login */
  52.    fputs($smtpConnect,"EHLO" . $newLine);
  53.    $smtpResponse = fgets($smtpConnect, 515);
  54.    $logArray['authrequest'] = "$smtpResponse";
  55.  
  56. echo "<br>EHLO: ".$logArray['authrequest']."";
  57.  
  58.    /* Request Auth Login */
  59.    fputs($smtpConnect,"AUTH LOGIN" . $newLine);
  60.    $smtpResponse = fgets($smtpConnect, 515);
  61.    $logArray['authrequest'] = "$smtpResponse";
  62.  
  63. echo "<br>AUTH LOGIN: ".$logArray['authrequest']."";
  64.  
  65.    /* Send username */
  66. // NOTE - use base64 direct if you get errors from UID and PASSWORD - I had to
  67.    fputs($smtpConnect, base64_encode($username) . $newLine);
  68. // ie.  fputs($smtpConnect, "W5AdmFuY291dmVyY29uZG9yZXBvcnQu" . $newLine);
  69.    $smtpResponse = fgets($smtpConnect, 515);
  70.    $logArray['authusername'] = "$smtpResponse";
  71.  
  72. echo "<br>UID: ".$logArray['authusername']."";
  73.  
  74.    /* Send password */
  75. // NOTE - use base64 direct if you get errors from UID and PASSWORD - I had to
  76.    fputs($smtpConnect, base64_encode($password) . $newLine);
  77. // ie.   fputs($smtpConnect, "F0YWx1bm" . $newLine);
  78.    $smtpResponse = fgets($smtpConnect, 515);
  79.    $logArray['authpassword'] = "$smtpResponse";
  80.  
  81. echo "<br>PASSWORD: ".$logArray['authpassword']."";
  82.  
  83.    /* Say Hello to SMTP */
  84.    fputs($smtpConnect, "HELO $localhost" . $newLine);
  85.    $smtpResponse = fgets($smtpConnect, 515);
  86.    $logArray['heloresponse'] = "$smtpResponse";
  87.  
  88. echo "<br>HELO: ".$logArray['heloresponse']."";
  89.  
  90.    /* Email From */
  91.    fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
  92.    $smtpResponse = fgets($smtpConnect, 515);
  93.    $logArray['mailfromresponse'] = "$smtpResponse";
  94.  
  95. echo "<br>MAIL FROM: ".$logArray['mailfromresponse']."";
  96.  
  97.    /* Email To */
  98.    fputs($smtpConnect, "RCPT TO: $to" . $newLine);
  99.    $smtpResponse = fgets($smtpConnect, 515);
  100.    $logArray['mailtoresponse'] = "$smtpResponse";
  101.  
  102. echo "<br>RCPT TO: ".$logArray['mailtoresponse']."";
  103.  
  104.    /* The Email */
  105.    fputs($smtpConnect, "DATA" . $newLine);
  106.    $smtpResponse = fgets($smtpConnect, 515);
  107.    $logArray['data1response'] = "$smtpResponse";
  108.  
  109. echo "<br>DATA: ".$logArray['data1response']."";
  110.  
  111.    /* Construct Headers */
  112.    $headers  = "MIME-Version: 1.0" . $newLine;
  113.    $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
  114.    // The following 2 line were commented out, otherwise you get double to/from fields in some cases
  115.    // $headers .= "To: $to" . $newLine;
  116.    // $headers .= "From: $from" . $newLine;
  117.  
  118. echo "<br>HEADERS: $headers";
  119.  
  120.    /* Fix all messages to have proper <CR> returns for all SMTP messages. */
  121.    $message = str_replace("\n", "\r\n", $message);
  122.  
  123.    fputs($smtpConnect, "To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n$message\r\n.\r\n");
  124.    $smtpResponse = fgets($smtpConnect, 515);
  125.    $logArray['data2response'] = "$smtpResponse";
  126.  
  127. echo "<br>THE EMAIL:<br> To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n$message\r\n.\r\n";
  128. echo "<br>DATA2RESPONCE: ".$logArray['data2response']."........ <br>  If this reads '250 ok ...' then the message was sent.";
  129.  
  130.    /* Say Bye to SMTP */
  131.    fputs($smtpConnect,"QUIT" . $newLine);
  132.    $smtpResponse = fgets($smtpConnect, 515);
  133.    $logArray['quitresponse'] = "$smtpResponse";
  134.  
  135. echo "<br>QUIT: ".$logArray['quitresponse']."";
  136.  
  137. }
  138. ?>
  139.  
This really works, I have re-tested it to be certain because it was edited for posting here.

To use this yourself, in place of the more usual .php 'mail()' function you need to replace occurrences of mail() with auth_mail() AND be careful to ensure the order of the inputs, it is NOT identical, so take care OR it will NOT work ...

ie.
authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
versus
mail($to, $subject, $message, $headers, $parameters)

Share this Question
Share on Google+
2 Replies


P: 3
1st file, uses 2nd file [includes it] place both in same folder for simplicity, move one to /includes/ if you want to be tidy, then change path.

save the first file as 'test_auth_mail.php', or some memorable name
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <head>
  3.     <title>Simple SMTP mail test</title>
  4.   </head>
  5.   <body>
  6. Testing SMTP mail sending ...
  7. <br><hr><br>
  8.  
  9. <?php
  10. include_once('auth_mail.php');
  11. $user_email = "somebody@somewhere.com";
  12. $from = "legituser@yourdomain.com";  // Needs a valid email address able to use SMTP
  13. $subject = "Test Email";
  14. $message = "This is NOT a fluke !! IT REALLY WORKS !!";
  15. authSendEmail($from, 'yourdomain.com', $user_email, '-', $subject, $message);
  16. ?>
  17. <br><hr><br>
  18. Test completed, hopefuly IT WORKED for you too !!  :)
  19. <br>
  20. If not then double check that you setup the domain, username, password, etc correctly
  21. </body>
  22.  
save the second file as 'auth_mail.php', or some memorable name, it gets included, make sure the name matches of course
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // NOTE: this has been edited to remove private information, hopefully it still works
  4. //  if not send a private message via this site, I may try to assist you, if I am not too busy
  5.  
  6. /* * * * * * * * * * * * * * SEND EMAIL FUNCTIONS * * * * * * * * * * * * * */
  7.  
  8. // Authenticate Send - from 21st March 2005, updated 15th March 2012 .. 7 years on !!
  9. // This will send an email using auth smtp and output a log array
  10.  
  11. // OUTPUT of this script goes to your screen, comment the ECHO commands to stop that
  12.  
  13. function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
  14. {
  15.  
  16.    /* * * * CONFIGURATION START * * * */
  17.  
  18.    /* Change the following lines to reflect your smtp server settings */
  19.  
  20.    $smtpServer = "mail.yourdomain.com";
  21.    $port = "25";    // NOTE: this varies quite a lot, mine is 45, some are 25, they all vary
  22.    $timeout = "60";
  23. // NOTE - the username and password need to be sent in Base64 format, ie. emcrypted
  24. //     I found it easiest to use http://www5.rptea.com/base64/ and use the actual base64 version
  25. //   on my server the function 'base64_encode' failed to produce the correct output
  26.    $username = "validuser@yourdomain.com";
  27.    $password = "validpassword";
  28.  
  29.    $localhost = "yourdomain.com";
  30.    $newLine = "\r\n";
  31.  
  32.    /* * * * CONFIGURATION END * * * * */
  33.  
  34.    /* Connect to the host on the specified port */
  35.    $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
  36.    $smtpResponse = fgets($smtpConnect, 515);
  37.    if(empty($smtpConnect))
  38.    {
  39.        $output = "Failed to connect: $smtpResponse";
  40.        echo "$output .";
  41.        return $output;
  42.    }
  43.    else
  44.    {
  45.        $logArray['connection'] = "Connected: $smtpResponse";
  46.  
  47. echo "<br>CONNECTION: ".$logArray['connection']." ";
  48.  
  49.    }
  50.  
  51.    /* Request Auth Login */
  52.    fputs($smtpConnect,"EHLO" . $newLine);
  53.    $smtpResponse = fgets($smtpConnect, 515);
  54.    $logArray['authrequest'] = "$smtpResponse";
  55.  
  56. echo "<br>EHLO: ".$logArray['authrequest']."";
  57.  
  58.    /* Request Auth Login */
  59.    fputs($smtpConnect,"AUTH LOGIN" . $newLine);
  60.    $smtpResponse = fgets($smtpConnect, 515);
  61.    $logArray['authrequest'] = "$smtpResponse";
  62.  
  63. echo "<br>AUTH LOGIN: ".$logArray['authrequest']."";
  64.  
  65.    /* Send username */
  66. // NOTE - use base64 direct if you get errors from UID and PASSWORD - I had to
  67.    fputs($smtpConnect, base64_encode($username) . $newLine);
  68. // ie.  fputs($smtpConnect, "W5AdmFuY291dmVyY29uZG9yZXBvcnQu" . $newLine);
  69.    $smtpResponse = fgets($smtpConnect, 515);
  70.    $logArray['authusername'] = "$smtpResponse";
  71.  
  72. echo "<br>UID: ".$logArray['authusername']."";
  73.  
  74.    /* Send password */
  75. // NOTE - use base64 direct if you get errors from UID and PASSWORD - I had to
  76.    fputs($smtpConnect, base64_encode($password) . $newLine);
  77. // ie.   fputs($smtpConnect, "F0YWx1bm" . $newLine);
  78.    $smtpResponse = fgets($smtpConnect, 515);
  79.    $logArray['authpassword'] = "$smtpResponse";
  80.  
  81. echo "<br>PASSWORD: ".$logArray['authpassword']."";
  82.  
  83.    /* Say Hello to SMTP */
  84.    fputs($smtpConnect, "HELO $localhost" . $newLine);
  85.    $smtpResponse = fgets($smtpConnect, 515);
  86.    $logArray['heloresponse'] = "$smtpResponse";
  87.  
  88. echo "<br>HELO: ".$logArray['heloresponse']."";
  89.  
  90.    /* Email From */
  91.    fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
  92.    $smtpResponse = fgets($smtpConnect, 515);
  93.    $logArray['mailfromresponse'] = "$smtpResponse";
  94.  
  95. echo "<br>MAIL FROM: ".$logArray['mailfromresponse']."";
  96.  
  97.    /* Email To */
  98.    fputs($smtpConnect, "RCPT TO: $to" . $newLine);
  99.    $smtpResponse = fgets($smtpConnect, 515);
  100.    $logArray['mailtoresponse'] = "$smtpResponse";
  101.  
  102. echo "<br>RCPT TO: ".$logArray['mailtoresponse']."";
  103.  
  104.    /* The Email */
  105.    fputs($smtpConnect, "DATA" . $newLine);
  106.    $smtpResponse = fgets($smtpConnect, 515);
  107.    $logArray['data1response'] = "$smtpResponse";
  108.  
  109. echo "<br>DATA: ".$logArray['data1response']."";
  110.  
  111.    /* Construct Headers */
  112.    $headers  = "MIME-Version: 1.0" . $newLine;
  113.    $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
  114.    // The following 2 line were commented out, otherwise you get double to/from fields in some cases
  115.    // $headers .= "To: $to" . $newLine;
  116.    // $headers .= "From: $from" . $newLine;
  117.  
  118. echo "<br>HEADERS: $headers";
  119.  
  120.    /* Fix all messages to have proper <CR> returns for all SMTP messages. */
  121.    $message = str_replace("\n", "\r\n", $message);
  122.  
  123.    fputs($smtpConnect, "To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n$message\r\n.\r\n");
  124.    $smtpResponse = fgets($smtpConnect, 515);
  125.    $logArray['data2response'] = "$smtpResponse";
  126.  
  127. echo "<br>THE EMAIL:<br> To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n$message\r\n.\r\n";
  128. echo "<br>DATA2RESPONCE: ".$logArray['data2response']."........ <br>  If this reads '250 ok ...' then the message was sent.";
  129.  
  130.    /* Say Bye to SMTP */
  131.    fputs($smtpConnect,"QUIT" . $newLine);
  132.    $smtpResponse = fgets($smtpConnect, 515);
  133.    $logArray['quitresponse'] = "$smtpResponse";
  134.  
  135. echo "<br>QUIT: ".$logArray['quitresponse']."";
  136.  
  137. }
  138. ?>
  139.  
This really works, I have re-tested it to be certain because it was edited for posting here.

To use this yourself, in place of the more usual .php 'mail()' function you need to replace occurrences of mail() with auth_mail() AND be careful to ensure the order of the inputs, it is NOT identical, so take care OR it will NOT work ...

ie.
authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
versus
mail($to, $subject, $message, $headers, $parameters)
Mar 14 '12 #2

P: 3
NOTE: though this method does work, at least with my setup, there are some anomalies which I think are caused by the Server's rules concerning SMTP. For example only one eMail can be sent to an address at a time, there must be a delay of a few minutes before another can go, in cases where the mail is virtually identical. This makes testing awkward, but I guess it is part of anti-spam protection.

Further problems occur if there are any format mishaps, the mail must be ultra simple to pass, at least on this server. Any fancy formatting tends to break the process and the mail is lost.

This 'fsockopen' method has got my project going forward again, but it is far from simple or totally reliable to implement. There are lots of references to these problems, just Search. Send a PM if you get stuck, I will try to help if there is time.
Mar 16 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.