Script to Log into an SMTP server

Is anyone familiar with setting up a php script to log into an SMTP server?

The problem I’m running into is this:
I’ve created a Contact form and it has worked for months up until a few months ago when a server was updated (not by me). I verified if the form was still operational by testing it on three different servers, and the form worked just fine.

The error that was occurring is this: Warning: mail() [function.mail]: SMTP server response: 501 5.1.7 Invalid address in C:\AppServ\www\info\contact.php on line 71

The server administrator recommended that I “set the PHP script up to log in to the SMTP server” to possibly fix the problem.

Any suggestions?

I’ve been trying to muscle my way to figure out how to fix my form to send email via a PHP form and have received the following errors:

Notice: Undefined variable: smtp in C:\…\contact.php on line 65

Fatal error: Call to a member function send() on a non-object in C:\…\contact.php on line 65

Line 65: $mail = $smtp->send($to, $subject, $message, $additionalHeaders);

I’m not sure how to fix it, but I’ve supplied my code below:

<?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\
\\'";,@%]+$/';
	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 = 'smtpout.secureserver.net';
	$username = 'username;
	$password = 'password';
	} else {
	$to = 'email@email.com';//
	$subject = 'Subject';
	$host = 'smtpout.secureserver.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
	
	$mail = $smtp->send($to, $subject, $message, $additionalHeaders);

	//build the message
	$smtp = Mail::factory('smtp',
																							array('host' => $host,
																													'auth' => true,
																													'username' => $username,
																													'password' => $password));
	$message = "To: $department\\r\
\\r\
";
	$message .= "From: $name $lname\\r\
";
	$message .= "$email\\r\
\\r\
";
	$message .= "Question/Comment: $messageBody";
	
	//build the additional headers
	$additionalHeaders = "From: Contact <email@email.com>\\r\
";
	$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.';
		}
	}
}
?>

Any help would be appreciated!

Thank you!

Personally, I just load a prebuilt class like PHPMailer and use that whenever i have to not use the default mail() command…

Agreed, but I’d be more inclined to use SwiftMailer; personal preference of course.

Thank you, folks!

I found that when I added this:

<?php
ini_set(“sendmail_from”, “email@domain.com”);
?>

at the top of my original code (and removed all of the other gobblety gook for PEAR), the form worked absolutely awesome.

What I wasn’t told in the beginning was that the system was on a Windows Server, so that changed my research for an answer. So now my processing code looks likes this:

<?php
ini_set("sendmail_from", "email@email.com");
// 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\
\\'";,@%]+$/';
	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';
	} else {
	$to = 'email@email.com';//
	$subject = 'Subject here';
	}
	
		$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
	$message = "To: $department\\r\
\\r\
";
	$message .= "From: $name $lname\\r\
";
	$message .= "$email\\r\
\\r\
";
	$message .= "Question/Comment: $messageBody";
	
	//build the additional headers
	$additionalHeaders = "From: Contact <email@domain.com>\\r\
";
	$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.';
		}
	}
}
?>

Again, thank you for the advice!