March 14, 2012

PHP SMTP Email, Mail.php, Rackspace Cloud Hosting, Pear Mail, Authenticated Email Function

PHP SMTP Email, Mail.php, Rackspace Cloud Hosting, Pear Mail, Authenticated Email Function

//I AM USING HTTP POT VARS BECAUSE WE ARE GETTING VARIABLES FROM HTML FILE, IF WE USE PHP FILE THEN WE CAN USE $_POST & $_GET

$random_hash = md5(date(‘r’, time()));

$what_to_replace = array(‘contact_’,’_’);
$replace_with = ‘ ‘;//WE WANA REPLACE UNDERSCORES WITH SPACES

$http_vars = $_POST; //WE ARE GETTING AN ARRAY
//print_r($http_vars);

$bad_array = array(‘x’,’y’,’redirection’);
$for_db = array();
//FOR THIS WE HAVE TO USE FOREACH LOOP, BECAUSE WE NEED INDEXES AS NAMES AND VALUES AS VALUES
$data = ‘<table>’;//USING SINGLE QUOTE BECAUSE ITS JUST STRING
foreach($http_vars as $name=>$values)
{

if(substr(strtolower($name),0,8)==’contact_’)//IF FIELD NAME IS NOT SUBMIT THEN APPEND THIS VARIABLE ELSE NOT, WE WANA SKIP SUBMIT
{
//    WE WANA FORMATE FIELD NAMES PROPERLY WITHOUT ANY UNDERSCORE AND ALSO FIRST LETTER CAPITAL, so
$name = str_replace($what_to_replace,’ ‘,$name);

$name = ucwords($name);//WE WANA EVERY WORD WITH UPPER CASE STARTING

if(is_array($values) && count($values)>0)
{
$values = implode(‘, ‘,$values);
}
//USING DOT BECAUSE I WANA APPEND DATA VARIABLE NOT OVERWRITE THAT
$data.= ‘<tr><td valign=”top”><b>’.$name.’:</b> </td><td>’.$values.'</td></tr>’;
//FIRT TD MEANS LEFT COLUMN, WILL DISPLAY ALL THE CAPTIONS
//SECOND TD MEANS RIGHT COLUMN, WILL DISPLAY VALUES
//TR WILL BREAK THE ROW AND WE WILL GO TO NEXT ROW
$for_db[$name]=$values;
}

}

$data.='</table>’;

$domain = str_replace(‘www.’, ”, $_SERVER[‘HTTP_HOST’]);

$subject = ‘Contact Enquiry from ‘.$domain.’  ‘.date(‘d M, Y’);

$host = “mail.”.$domain;

$username = “enquiry@”.$domain;

$password = “*********”;

$from = “vws enquiry <$username>”;
$to = “admin email address”;//

$headers = array (‘From’ => $from,
‘To’ => $to,
‘Subject’ => $subject,
‘Content-Type’ => ‘text/HTML; charset=ISO-8859-1\n’);
$smtp = Mail::factory(‘smtp’,
array (‘host’ => $host,
‘auth’ => true,
‘username’ => $username,
‘password’ => $password));

$mail = $smtp->send($to, $headers, $data);

if (PEAR::isError($mail)) {
echo(“<p>” . $mail->getMessage() . “</p>”);
} else {
echo(“<p>Message successfully sent!</p>”);
}

Last updated: March 19, 2014