0 Replies Latest reply on Dec 21, 2010 7:25 AM by ben37d

    SMTP Authentication for PHP Mail

    ben37d

      Can anyone help me in figuring out the correct way to incorporate the SMTP authentication into a form? I am having a lot of trouble in getting my forms to send with this format. My code for my php action page is below. I have my correct information where i included *******. Please let me know what i have wrong.

       

       

      CODE STARTS HERE

       

      <?php
      //new function

      $to = "*******";
      $nameto = "LTL Freight Shop";
      $from = "*******";
      $namefrom = "LTL Freight Shop";
      $subject = "Account Request";
      authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);
      ?>

       

      <?php

      $recipient  = "*******";
      //$subject = "Account Request";

       

      $companyname = check_input($_POST['CompanyName'], "Enter your company name");
      $firstname  = check_input($_POST['FirstName'], "Enter your first name");
      $lastname  = check_input($_POST['LastName'], "Enter your last name");
      $phone  = check_input($_POST['PhoneNumber'], "Enter your phone number");
      $fax  = check_input($_POST['FaxNumber']);
      $email  = check_input($_POST['Email'], "Enter your email");
      $address  = check_input($_POST['StreetAddress'], "Enter your address");
      $city  = check_input($_POST['City'], "Enter your city");
      $state  = check_input($_POST['State'], "Enter your state");
      $zipcode  = check_input($_POST['ZipCode'], "Enter your zip code");
      $country  = check_input($_POST['Country'], "Enter your country");
      $yearsinbusiness  = check_input($_POST['YearsinBusiness'], "Enter your years in business");
      $typeofindustry  = check_input($_POST['TypeofIndustry'], "Enter your type of industry");
      $multiplelocations    = check_input($_POST['MultipleLocations']);
      $numberoflocations  = check_input($_POST['LocationsCount']);
      $ltl  = check_input($_POST['ServicesLTL']);
      $ftl  = check_input($_POST['ServicesFTL']);
      $domesticparcel  = check_input($_POST['ServicesDomesticParcel']);
      $intlparcel  = check_input($_POST['ServicesInternationalParcel']);
      $airfreight  = check_input($_POST['ServicesAirFreight']);
      $oceanfreight  = check_input($_POST['ServicesOceanFreight']);
      $other  = check_input($_POST['ServicesOther']);
      $none  = check_input($_POST['ServicesNone']);
      $volume  = check_input($_POST['TypicalVolume'], "Enter your typical volume");
      $carrier  = check_input($_POST['CurrentCarrier'], "Enter your current carrier");
      $class  = check_input($_POST['AverageClass'], "Enter your average class");
      $weight  = check_input($_POST['AverageWeight'], "Enter your average weight");
      $process   = check_input($_POST['Process']);
      $hearabout = check_input($_POST['HearAbout']);
      $comments = check_input($_POST['Comments']);

       

      if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
      {
          show_error("E-mail address not valid");
      }

       

      $message = "You have received an account request from:

      Company Name: $companyname
      First Name: $firstname
      Last Name: $lastname
      Phone Number: $phone
      Fax Number: $fax
      E-mail: $email
      Street Address: $address
      City: $city
      State: $state
      Zip Code: $zipcode
      Country: $country
      Years in Business: $yearsinbusiness
      Type of Industry: $typeofindustry
      Multiple Locations: $multiplelocations
      Number of Locations: $numberoflocations
      Services they use: $ltl, $ftl, $domesticparcel, $intlparcel, $airfreight, $oceanfreight, $other, $none
      Typical Volume: $volume
      Current Carrier: $carrier
      Average Class: $class
      Average Weight: $weight
      How they currently process: $process
      How they heard about us: $hearabout
      Comments: $comments

      End of message
      ";

       

      //ini_set("SMTP","smtp.emailsrvr.com");
      //ini_set("SMTP_PORT", 25);
      //ini_set("sendmail_from","*******");
      //mail($recipient, $subject, $message);

       

      function check_input($data, $problem='')
      {
          $data = trim($data);
          $data = stripslashes($data);
          $data = htmlspecialchars($data);
          if ($problem && strlen($data) == 0)
          {
              show_error($problem);
          }
          return $data;
      }

       

      function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
      {
      $smtpServer = "smtp.emailsrvr.com";
      $port = "25";
      $timeout = "30";
      $username = "********";
      $password = "********";
      $localhost = "smtp.emailsrvr.com";
      $newLine = "\r\n";
      $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
      $smtpResponse = fgets($smtpConnect, 515);
      if(empty($smtpConnect))
      {
      $output = "Failed to connect: $smtpResponse";
      return $output;
      }
      else
      {
      $logArray['connection'] = "Connected: $smtpResponse";
      }

      fputs($smtpConnect,"AUTH LOGIN" . $newLine);
      $smtpResponse = fgets($smtpConnect, 515);
      $logArray['authrequest'] = "$smtpResponse";

       

      fputs($smtpConnect, base64_encode($username) . $newLine);
      $smtpResponse = fgets($smtpConnect, 515);
      $logArray['authusername'] = "$smtpResponse";

       

      fputs($smtpConnect, base64_encode($password) . $newLine);
      $smtpResponse = fgets($smtpConnect, 515);

      $logArray['authpassword'] = "$smtpResponse";

      fputs($smtpConnect, "HELO $localhost" . $newLine);
      $smtpResponse = fgets($smtpConnect, 515);
      $logArray['heloresponse'] = "$smtpResponse";

       

      fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
      $smtpResponse = fgets($smtpConnect, 515);
      $logArray['mailfromresponse'] = "$smtpResponse";

       

      fputs($smtpConnect, "RCPT TO: $to" . $newLine);
      $smtpResponse = fgets($smtpConnect, 515);
      $logArray['mailtoresponse'] = "$smtpResponse";

       

      fputs($smtpConnect, "DATA" . $newLine);
      $smtpResponse = fgets($smtpConnect, 515);
      $logArray['data1response'] = "$smtpResponse";

       

      $headers = "MIME-Version: 1.0" . $newLine;
      $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
      $headers .= "To: $nameto <$to>" . $newLine;
      $headers .= "From: $namefrom <$from>" . $newLine;

       

      fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
      $smtpResponse = fgets($smtpConnect, 515);
      $logArray['data2response'] = "$smtpResponse";

       

      fputs($smtpConnect,"QUIT" . $newLine);
      $smtpResponse = fgets($smtpConnect, 515);
      $logArray['quitresponse'] = "$smtpResponse";
      }

       

      function show_error($myError)
      {
      ?>
          <html>
          <body>

          <b>Please correct the following error:</b><br />
          <?php echo $myError; ?>

          </body>
          </html>

      <?php
      exit();
      }
      ?>