I think I have been taken for a ride. A PHP programmer for my fathers website supposedly spent '2 hours' going over this code and could not get it to work on our webhost. Please check it and tell me what you think. Below is a test php script that works fine.
<?php
$to = "test@test.com";
$subject = "Php test mail";
$body = "Test mail for php mail()";
if (mail($to, $subject, $body)) {
echo("<p>Yes you have done it</p>");
} else {
echo("<p>Oops try once more..</p>");
}
?>
The code that does not work required is below and required php pear to be installed. When using this form it just hangs and then says could not connect to the host via SMTP. All details have been checked and the form works on another host.
Does some hosts have problem with a forms smtp authentication procedure?
How do I change the below code to remove the smtp login e.t.c and use the method of emailing as in the test script above? The test script does not use any external server login, it just does it from the server directly.
<?php
ini_set("include_path", ".:/usr/lib/php:/usr/local/lib/php:/home/harore/php");
require_once "Mail.php";
// <<< begin form handling
if ($_POST["PA_Name"] != "")
{
$from = "Website Enquiry <form@example.com>";
$to = "Haro Removals <form@example.com>";
$subject = "Website Quotation";
$host = "mail.example.com";
$port = "26";
$username = "form@example.com";
$password = "pass";
$body = "Hi,\r\n\r\nHere is my request:\r\n\r\nPickup From:\r\n\r\n";
foreach ($_POST as $k => $v)
if (substr($k, 0, 3) == "PA_")
$body = $body.substr($k, 3).": ".$v."\r\n";
$body = $body."\r\n\r\nDelivery To:\r\n\r\n";
foreach ($_POST as $k => $v)
if (substr($k, 0, 3) == "DA_")
$body = $body.substr($k, 3).": ".$v."\r\n";
$body = $body."\r\n\r\nAnything else we should know:\r\n\r\n";
foreach ($_POST as $k => $v)
if (substr($k, 0, 3) == "AE_")
$body = $body.substr($k, 3).": ".$v."\r\n";
$body = $body."\r\n\r\nBedrooms:\r\n\r\n";
foreach ($_POST as $k => $v)
if (substr($k, 0, 3) == "BE_")
$body = $body.substr($k, 3).": ".$v."\r\n";
$body = $body."\r\n\r\nLaundry:\r\n\r\n";
foreach ($_POST as $k => $v)
if (substr($k, 0, 3) == "LA_")
$body = $body.substr($k, 3).": ".$v."\r\n";
$body = $body."\r\n\r\nLounge:\r\n\r\n";
foreach ($_POST as $k => $v)
if (substr($k, 0, 3) == "LO_")
$body = $body.substr($k, 3).": ".$v."\r\n";
$body = $body."\r\n\r\nHall:\r\n\r\n";
foreach ($_POST as $k => $v)
if (substr($k, 0, 3) == "HA_")
$body = $body.substr($k, 3).": ".$v."\r\n";
$body = $body."\r\n\r\nKitchen:\r\n\r\n";
foreach ($_POST as $k => $v)
if (substr($k, 0, 3) == "KI_")
$body = $body.substr($k, 3).": ".$v."\r\n";
$body = $body."\r\n\r\nDining:\r\n\r\n";
foreach ($_POST as $k => $v)
if (substr($k, 0, 3) == "DI_")
$body = $body.substr($k, 3).": ".$v."\r\n";
$body = $body."\r\n\r\nSundries:\r\n\r\n";
foreach ($_POST as $k => $v)
if (substr($k, 0, 3) == "SU_")
$body = $body.substr($k, 3).": ".$v."\r\n";
$body = $body."\r\n\r\nFamily / Study:\r\n\r\n";
foreach ($_POST as $k => $v)
if (substr($k, 0, 3) == "FA_")
$body = $body.substr($k, 3).": ".$v."\r\n";
$body = $body."\r\n\r\nOutside:\r\n\r\n";
foreach ($_POST as $k => $v)
if (substr($k, 0, 3) == "OU_")
$body = $body.substr($k, 3).": ".$v."\r\n";
$body = $body."\r\n\r\nCartoons:\r\n\r\n";
foreach ($_POST as $k => $v)
if (substr($k, 0, 3) == "CA_")
$body = $body.substr($k, 3).": ".$v."\r\n";
$body = $body."\r\n\r\nAdditional Items:\r\n\r\n";
foreach ($_POST as $k => $v)
if (substr($k, 0, 3) == "AD_")
$body = $body.substr($k, 3).": ".$v."\r\n";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
}
// end form handling >>>
?>