Skip to main content
Known Participant
July 30, 2011
Question

Convert PHP Email Code to Using SMTP Authentication

  • July 30, 2011
  • 1 reply
  • 1429 views

One of my contact forms no longer is able to send (it used to until some server updates were made), for it keeps posting the following error:

Warning:  mail() [function.mail]: SMTP server response: 501 5.1.7 Invalid address in C:\contact.php on line 71

This form does work on three other servers, so obviously the form is in working order but not on this particular server.

I was recommended to "set the PHP script up to log in to       the SMTP server" using this example:


I've only gotten this far and cannot figure out how to actually create it to SEND:
<?php
// set flag to indicate whether mail has been sent
$mailSent = false;
if (array_key_exists('eList', $_POST)) {
    // mail processing script
    // remove escape characters from POST array
    if (get_magic_quotes_gpc()) {
    function stripslashes_deep($value) {
    $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
    return $value;
    }
      $_POST = array_map('stripslashes_deep', $_POST);
      }

$email = $_POST['email'];

        // check for valid email address
    $pattern = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
    if (!preg_match($pattern, trim($email))) {
    $error['email'] = 'Please enter a valid email address';
    }
   
// validate the input, beginning with name
    $name = trim($_POST['name']);
    if (empty($name)) {
        $error['name'] = 'Please enter your First Name';
        }
    $lname = trim($_POST['lname']);
    if (empty($lname)) {
        $error['lname'] = 'Please enter your Last Name';
        }
    $department = $_POST['department'];
    if ($_POST['department'] == '') {
    $error['department'] = 'Please select a Department';
    }
   
//     check the content of the text area
        $messageBody = trim($_POST['message']);
        if (empty($messageBody)) {
        $error['message'] = 'Please enter your message';
        }
       

    // initialize variables
    if (!empty($_POST['url'])) {
    $to = 'email@email.com';//
    $subject = 'Suspected as SPAM';
    $host = 'smptout.serverserver.net';
    $username = 'username';
    $password = 'password';
    } else {
    $to = 'email@email.com';//
    $subject = 'Here we are';
    $host = 'smptout.serverserver.net';
    $username = 'username';
    $password = 'password';
    }
   
        $SpamErrorMessage = "No URLs permitted";
    if (preg_match("/http/i", "$name")) {echo "$SpamErrorMessage"; exit();}
    if (preg_match("/http/i", "$lname")) {echo "$SpamErrorMessage"; exit();}
    if (preg_match("/http/i", "$email")) {echo "$SpamErrorMessage"; exit();}
    if (preg_match("/http/i", "$messageBody")) {echo "$SpamErrorMessage"; exit();} // check for spam

    //build the message
    $smtp = Mail::factory('smtp',
                                                                                            array('host' => $host,
                                                                                                                    'auth' => true,
                                                                                                                    'username' => $username,
                                                                                                                    'password' => $password));
    $message = "To: $department\r\n\r\n";
    $message .= "From: $name $lname\r\n";
    $message .= "$email\r\n\r\n";
    $message .= "Question/Comment: $messageBody";
   
    //build the additional headers
    $additionalHeaders = "From: Contact <here@here.com>\r\n";
    $additionalHeaders .= "Reply-To: $email";
   
//send the email if there are not errors
    if (!isset($error)) {
    $mailSent = mail($to, $subject, $message, $additionalHeaders);
    // check that the mail was sent successfully
    if (!$mailSent) {
        $error['notSent'] = 'Sorry, there was a problem sending your mail. Please try later.';
        }
    }
}
?>

Thank you for your help!

This topic has been closed for replies.

1 reply

toad78Author
Known Participant
August 2, 2011

Anyone?