cannot send mail using PHP beyond domain

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ajarn Chan
    New Member
    • Mar 2012
    • 3

    cannot send mail using PHP beyond domain

    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.
    Last edited by Ajarn Chan; Mar 14 '12, 09:11 PM. Reason: Resolved !! :)
  • Ajarn Chan
    New Member
    • Mar 2012
    • 3

    #2
    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
    Code:
    <html>
      <head>
        <title>Simple SMTP mail test</title>
      </head>
      <body>
    Testing SMTP mail sending ...
    <br><hr><br>
     
    <?php
    include_once('auth_mail.php');
    $user_email = "somebody@somewhere.com";
    $from = "legituser@yourdomain.com";  // Needs a valid email address able to use SMTP
    $subject = "Test Email";
    $message = "This is NOT a fluke !! IT REALLY WORKS !!";
    authSendEmail($from, 'yourdomain.com', $user_email, '-', $subject, $message);
    ?>
    <br><hr><br>
    Test completed, hopefuly IT WORKED for you too !!  :)
    <br>
    If not then double check that you setup the domain, username, password, etc correctly
    </body>
    save the second file as 'auth_mail.php' , or some memorable name, it gets included, make sure the name matches of course
    Code:
    <?php
     
    // NOTE: this has been edited to remove private information, hopefully it still works
    //  if not send a private message via this site, I may try to assist you, if I am not too busy
     
    /* * * * * * * * * * * * * * SEND EMAIL FUNCTIONS * * * * * * * * * * * * * */
     
    // Authenticate Send - from 21st March 2005, updated 15th March 2012 .. 7 years on !!
    // This will send an email using auth smtp and output a log array
     
    // OUTPUT of this script goes to your screen, comment the ECHO commands to stop that
     
    function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
    {
     
       /* * * * CONFIGURATION START * * * */
     
       /* Change the following lines to reflect your smtp server settings */
     
       $smtpServer = "mail.yourdomain.com";
       $port = "25";    // NOTE: this varies quite a lot, mine is 45, some are 25, they all vary
       $timeout = "60";
    // NOTE - the username and password need to be sent in Base64 format, ie. emcrypted
    //     I found it easiest to use http://www5.rptea.com/base64/ and use the actual base64 version
    //   on my server the function 'base64_encode' failed to produce the correct output
       $username = "validuser@yourdomain.com";
       $password = "validpassword";
     
       $localhost = "yourdomain.com";
       $newLine = "\r\n";
     
       /* * * * CONFIGURATION END * * * * */
     
       /* Connect to the host on the specified port */
       $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
       $smtpResponse = fgets($smtpConnect, 515);
       if(empty($smtpConnect))
       {
           $output = "Failed to connect: $smtpResponse";
           echo "$output .";
           return $output;
       }
       else
       {
           $logArray['connection'] = "Connected: $smtpResponse";
     
    echo "<br>CONNECTION: ".$logArray['connection']." ";
     
       }
     
       /* Request Auth Login */
       fputs($smtpConnect,"EHLO" . $newLine);
       $smtpResponse = fgets($smtpConnect, 515);
       $logArray['authrequest'] = "$smtpResponse";
     
    echo "<br>EHLO: ".$logArray['authrequest']."";
     
       /* Request Auth Login */
       fputs($smtpConnect,"AUTH LOGIN" . $newLine);
       $smtpResponse = fgets($smtpConnect, 515);
       $logArray['authrequest'] = "$smtpResponse";
     
    echo "<br>AUTH LOGIN: ".$logArray['authrequest']."";
     
       /* Send username */
    // NOTE - use base64 direct if you get errors from UID and PASSWORD - I had to
       fputs($smtpConnect, base64_encode($username) . $newLine);
    // ie.  fputs($smtpConnect, "W5AdmFuY291dmVyY29uZG9yZXBvcnQu" . $newLine);
       $smtpResponse = fgets($smtpConnect, 515);
       $logArray['authusername'] = "$smtpResponse";
     
    echo "<br>UID: ".$logArray['authusername']."";
     
       /* Send password */
    // NOTE - use base64 direct if you get errors from UID and PASSWORD - I had to
       fputs($smtpConnect, base64_encode($password) . $newLine);
    // ie.   fputs($smtpConnect, "F0YWx1bm" . $newLine);
       $smtpResponse = fgets($smtpConnect, 515);
       $logArray['authpassword'] = "$smtpResponse";
     
    echo "<br>PASSWORD: ".$logArray['authpassword']."";
     
       /* Say Hello to SMTP */
       fputs($smtpConnect, "HELO $localhost" . $newLine);
       $smtpResponse = fgets($smtpConnect, 515);
       $logArray['heloresponse'] = "$smtpResponse";
     
    echo "<br>HELO: ".$logArray['heloresponse']."";
     
       /* Email From */
       fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
       $smtpResponse = fgets($smtpConnect, 515);
       $logArray['mailfromresponse'] = "$smtpResponse";
     
    echo "<br>MAIL FROM: ".$logArray['mailfromresponse']."";
     
       /* Email To */
       fputs($smtpConnect, "RCPT TO: $to" . $newLine);
       $smtpResponse = fgets($smtpConnect, 515);
       $logArray['mailtoresponse'] = "$smtpResponse";
     
    echo "<br>RCPT TO: ".$logArray['mailtoresponse']."";
     
       /* The Email */
       fputs($smtpConnect, "DATA" . $newLine);
       $smtpResponse = fgets($smtpConnect, 515);
       $logArray['data1response'] = "$smtpResponse";
     
    echo "<br>DATA: ".$logArray['data1response']."";
     
       /* Construct Headers */
       $headers  = "MIME-Version: 1.0" . $newLine;
       $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
       // The following 2 line were commented out, otherwise you get double to/from fields in some cases
       // $headers .= "To: $to" . $newLine;
       // $headers .= "From: $from" . $newLine;
     
    echo "<br>HEADERS: $headers";
     
       /* Fix all messages to have proper <CR> returns for all SMTP messages. */
       $message = str_replace("\n", "\r\n", $message);
     
       fputs($smtpConnect, "To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n$message\r\n.\r\n");
       $smtpResponse = fgets($smtpConnect, 515);
       $logArray['data2response'] = "$smtpResponse";
     
    echo "<br>THE EMAIL:<br> To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n$message\r\n.\r\n";
    echo "<br>DATA2RESPONCE: ".$logArray['data2response']."........ <br>  If this reads '250 ok ...' then the message was sent.";
     
       /* Say Bye to SMTP */
       fputs($smtpConnect,"QUIT" . $newLine);
       $smtpResponse = fgets($smtpConnect, 515);
       $logArray['quitresponse'] = "$smtpResponse";
     
    echo "<br>QUIT: ".$logArray['quitresponse']."";
     
    }
    ?>
    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)
    Last edited by Ajarn Chan; Mar 14 '12, 09:53 PM. Reason: tidying up

    Comment

    • Ajarn Chan
      New Member
      • Mar 2012
      • 3

      #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.
      Last edited by Ajarn Chan; Mar 16 '12, 02:24 PM. Reason: tidying up

      Comment

      Working...