My hosting service recently stopped allowing sendmail and wants us to change to smtp mail. I have yet to get this to work with the existing code in contact.php despite spending countless hours trying. I can however get it to send from a clean test contact.php setup for smtp mail. I don't work with PHP very often and wrote the existing one years ago. I've asked around but no one seems to be able to help.

I've attached a sample of contact.php existing and test codes.

[ATTACH]5327[/ATTACH]

phpbuilder.txt

    In the past I've always used PHPMailer to handle SMTP, as it provides a fairly simple interface and then does all the difficult work itself. I'm sure SwiftMailer has something comparable, I've just never used it.

    Here's a sample from their user guide on sending using SMTP: http://phpmailer.worxware.com/index.php?pg=examplebsmtp

      Looks to me like he's using PEAR::Mail which, in my experience, is better than PHPMailer. He just needs to change something in this existing code to get email sent via PEAR mail:

      function SendEmails(){
          global $_values, $zag, $un;
          $un        = strtoupper(uniqid(time()));
          $to[0]      .= "myemail@CleaningBiz.com";
          $subject[0]      .= "contact was submitted on ".date("F j, Y")." ".date("H:i")."";
          $head[0]      = "From: ".str_replace("%,%", ",", $_values['email'])."\n";
          $head[0]      .= "Subject: contact was submitted on ".date("F j, Y")." ".date("H:i")."\n";
          $head[0]     .= "X-Mailer: Forms Expert at www.forms-expert.com\n";
          $head[0]      .= "Reply-To: ".str_replace("%,%", ",", $_values['email'])."\n";
          $head[0]     .= "Content-Type:multipart/mixed;";
          $head[0]     .= "boundary=\"".$un."\"\n\n";
          $EmailBody = "<html>
      <body>
      Form was filled with the following data:
      <br>
      <br><b>Name:</b> ".htmlspecialchars($_values["name"])."
      <br><b>Address:</b> ".htmlspecialchars($_values["address"])."
      <br><b>City/State:</b> ".htmlspecialchars($_values["city_state_zip"])."
      <br><b>Phone:</b> ".htmlspecialchars($_values["phone"])."
      <br><b>Email:</b> ".htmlspecialchars($_values["email"])."
      <br><b>Comments:</b> ".htmlspecialchars($_values["comments"])."
      </body></html>
      ";
          BuildBody($EmailBody, True, 0);
          for ($i=0;$i<=0;$i++){
            mail($to[$i], $subject[$i], $zag[$i], $head[$i]);
          }
        }
      

      Marion628, I hope you realize you posted your password in that text file. You might want to change it or you can probably expect some abuse.

        Marion628 says this code works. Seems to me it shouldn't be too hard to turn this into a function called my_mail and replace that mail() call in your code above.

        require_once "Mail.php"; // assumes PEAR::Mail is installed an PHP include_path set correctly
        
        // NOTE: to, subject, body are the first three parameters you pass to the built-in php mail() function
        $from = "Web Master <webmaster@CleaningBiz.com>";
        $to = "Secretary <myemail@CleaningBiz.com>";
        $subject = "Test email using PHP SMTP\r\n\r\n";
        $body = "This is a test email message";
        
        // configuration values (I have removed his original credentials)
        $host = "smtp.example.com";
        $username = "user@mydomain.com";
        $password = "*********";
        
        // setting headers
        $headers = array (
          'From' => $from,
          'To' => $to,
          'Subject' => $subject
        );
        
        $smtp = Mail::factory('smtp', array (
          'host' => $host,
          'auth' => true,
          'username' => $username,
          'password' => $password
        ));
        
        $mail = $smtp->send($to, $headers, $body);
        
        if (PEAR::isError($mail)) {
        echo("<p>" . $mail->getMessage() . "</p>"); // might want to return FALSE or throw an exception in your my_mail function
        } else {
        echo("<p>Message successfully sent!</p>"); // success! you should probably return TRUE in your my_mail function
        }
        

          Thanks all!! I know I need to change something in the existing code, I just can't figure out what to replace. I've been working on it for a little over 12 hours now changing this and that code, replacing and testing. I'd like to stay with PEAR mail and try to make it work. I was hoping that someone could point out to me what they see that I need to change because it should have worked with all the code changes I've made and tested each. Sneakyimp I didn't use my real password or any of the information that would give away my personal information like it stated in the email that came when I joined. 🙂

          I appreciate and and all input! 🙂

            This line in your SendEmails function is PHP's built-in [man]mail[/man] function. It typically uses sendmail to get your mail delivered. If sendmail is disabled, it's not going to work.

            mail($to[$i], $subject[$i], $zag[$i], $head[$i]);
            

            There's some fishy stuff going on with your SendMails function, though. It's not defined to accept any parameters when you call it and instead declares 3 global vars, each of which are suspect for different reasons:
            $_values - you refer repeatedly to this variable but haven't defined it anywhere. It appears to be an associative array that specifies the address of the recipient.
            $zag - you refer to this var but haven't defined it either. Apparently it's a numerically-indexed array
            * $un - you declare this as a global variable but then set it within your function.

            There's also $head, which is apparently an array but you only ever bother to set its 0th element.

            I'm not sure where to start.

              How do I send someone a private message in this forum? I know I can send one in my other group but don't see how to within this group.

                Marion628;11055309 wrote:

                How do I send someone a private message in this forum? I know I can send one in my other group but don't see how to within this group.

                Not sure about this forum, but in at least one of it's sibling forums that attracts a lot of spammers, you have to have a certain number of posts before the PM ability is activated (as well as the ability to create an automated signature.

                  2 months later
                  Write a Reply...