Web Hosting
» PHP script to send mail to gmail
» PHP script to send mail with attachment

PHP script to send mail to gmail ID from web form

The following php script can be used to send email from web form to Gmail Address.

$email_to = "your-id@gmail.com";
$email_subject = "Mail from Web form";
$thankyou = "thanks.html"

$full_name = $_POST['Full_Name']; // from web form
$email_from = $_POST['Email_Address']; // from web form
$telephone = $_POST['Telephone']; //from web form
$comments = $_POST['Message']; // from form

$email_message = "Message details below.\r\n";
function clean_string($string)
{
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Full Name: ".clean_string($full_name)."\r\n";
$email_message .= "Email: ".clean_string($email_from)."\r\n";
$email_message .= "Telephone: ".clean_string($telephone)."\r\n";
$email_message .= "Message: ".clean_string($comments)."\r\n";

$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
header("Location: $thankyou");

To download the full code, please click here.

Note: In some web servers the above code does not work since the default smtp server is stopped due to security issues. In that case you need to create an email under your domain and use that email id to send mails to other Ids.

$thankyou = "Thnks.html";
$full_name = $_POST['Full_Name'];
$company_name = $_POST['Company_Name'];
$email_from = $_POST['Email_Address'];
$telephone = $_POST['Telephone_Number'];
$comments = $_POST['Description'];
function clean_string($string)
{
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message = "Read Message details below:\r\n";
$email_message .= "-----------------------------------------\r\n";
$email_message .= "Customer Name: ".clean_string($full_name)."\r\n";
$email_message .= "Company Name: ".clean_string($company_name)."\r\n";
$email_message .= "Email: ".clean_string($email_from)."\r\n";
$email_message .= "Telephone: ".clean_string($telephone)."\r\n";
$email_message .= "Requirements: ".clean_string($comments)."\r\n";
require_once "Mail.php";
$from = "yourid@yourdomain.com"; \\the mail-id under your domain
$to = "yourgmail@gmail.com"; \\the TO gmail ID
$subject = "PHP Enquiry form";
$body = $email_message;
$host = "mail.yourdomain.com"; \\ your domain mail server
$username = "yourid@yourdomain.com"; \\the mail-id under your domain
$password = "yourpassword"; \\your password for yourid@mydomain.com
$headers = array ('From' => $from, 'To' => $to, 'Reply-To'=> $email_from, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $body);

To download the full code, please click here.

blog comments powered by Disqus
About Us | Contact us at : info@top-answers.net | Web Designing Company in Bangalore | SEO Company in Bangalore | Electrical Switch Dealers Bangalore
© top-answers.net | All Rights Reserved | Powered by Mindroit Technologies