View Full Version : Change email form from PHP to SMTP ?

03-17-2010, 06:33 PM

I have the following code on my site, when a user fills out a form, I post to a page with the following code. Pretty basic I know, however my host has disabled php mail on all it's servers for goodness knows how long.

My question is, How do I re-jig this code to work with SMTP instead ? (sorry, not very handy with PHP myself)


$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$visitor = $_POST['visitor'];
$visitormail = $_POST['visitormail'];
$notes = $_POST['notes'];
$attn = $_POST['attn'];

if (eregi('http:', $notes)) {
die ("Do NOT try that! ! ");
if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))
echo "<h2>Please click 'BACK' and Enter a valid e-mail</h2>\n";
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
die ("Go back! ! ");

if(empty($visitor) || empty($visitormail) || empty($notes )) {
echo "<h2>Please fill in all the required (<FONT COLOR='RED'>*</FONT>) fields</h2>\n";
die ("Please click 'BACK' on your webbrowser and try again.<BR><CENTER><B>Your message has NOT been sent!</CENTER></B>");

$todayis = date("l, F j, Y, g:i a") ;

$attn = $attn ;
$subject = $attn;

$notes = stripcslashes($notes);

$message = " $todayis [EST] \n
Attention: $attn \n
Message: $notes \n
From: $visitor ($visitormail)\n
Additional Info : IP = $ip \n
Browser Info: $httpagent \n
Referral : $httpref \n

$from = "From: $visitormail\r\n";

mail("my_email@here.com", $subject, $message, $from);


Thanks for any and all help.


03-17-2010, 07:05 PM
You got a sendmail path from you're host? If so, use the provided sendmail program by setting it as the fifth parameter to the mail command.
If not, you'll need to establish a socket connection to you're smtp and fire it commands. To do so, you would start like so:

if (!($smtp = fsockopen("smtp.yoursmtpserver.com", 25, $errno, $error)))
die('Cannot connect to smtp server: ' . $error);

list($visitorUser, $visitorDomain) = explode("@", $visitormail);

// For now, we'll just ignore the server's replies. For that, look into what the return codes are
// and use fgets with $smtp to retrieve them.
fwrite($smtp, "HELO anyone\r\n");
fwrite($smtp, "MAIL FROM: <$visitormail>\r\n");
fwrite($smtp, "RCPT TO: <my_email@here.com>\r\n");
fwrite($smtp, "DATA\r\n");
fwrite($smtp, "Received: from $visitorDomain by here.com\r\n");
fwrite($smtp, "Date: " . date('r') . "\r\n");
fwrite($smtp, "From: $visitormail\r\n");
fwrite($smtp, "Subject: $subject\r\n");
fwrite($smtp, "To: my_email@here.com\r\n");
fwrite($smtp, $message);
fwrite($smtp, "\r\nQUIT\r\n");


Something like that will replace the mail() call. No idea if it works though, I can't test anything ATM.
You're $message will require proper endings though, \r\n not just \n. Its somewhat ugly to do this manually; use the sendmail path if the host has an alternate location for sending email.

03-17-2010, 11:25 PM
In the past, I've just used PHPMailer class for when I had to send mail via SMTP rather use the limited mail function in PHP.

You could download PHPMailer and look at the example code they give and rewire your PHP form using PHPMailer.

Just another option for you.