PHP contact form gmail smtp help

email contact form website

Page 1 of 1

3 Replies - 13592 Views - Last Post: 05 September 2010 - 03:52 PM Rate Topic: -----

#1 weemanpow3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 27-September 09

PHP contact form gmail smtp help

Posted 05 September 2010 - 07:24 AM

Im trying to set my php script to work correctly by using gmail for the smtp server and sending me emails from my website contact form. For some reason my script wont work and I can't figure out why. Below is the php script that I have and the html section form. Any help is appreciated, Thanks.


contact.php
<?php

if(!$_POST) exit;

require("phpmailer/class.phpmailer.php")

function smtpMail($to, $from_name, $from_email, $subject, $body){

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username ="fake@gmail.com"; 
$mail->Password = "123456"; 
$mail->SetFrom($from_email, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);

return $mail->Send();
}

$email = $_POST['email'];


//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+ $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+ ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\ )){
$error .="Invalid email address entered";
$errors = 1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','message','subject');
$required = array('name','email','message','subject');

$your_email = "james@example.com";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";

foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'company') {
if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}

if(@smtpMail($your_email, $receipient_email, $receipient_name, $email_subject, $email_content) {
echo 'Message sent!';
} else {
echo 'ERROR! Message was NOT sent.';
}
}

?>


HTML Contact Form
<form action="contact.php" method="post" id="contactform">
	  				<ol>
            			<li>
              				<label for="name">First Name <span>*</span></label>
              				<input id="name" name="name" class="text" />
            			</li>
            			<li>
              				<label for="email">Your email <span>*</span></label>
              				<input id="email" name="email" class="text" />
            			</li>
            			<li>
              				<label for="company">Company</label>
              				<input id="company" name="company" class="text" />
           				</li>
            			<li>
              				<label for="subject">Subject</label>
              				<input id="subject" name="subject" class="text" />
            			</li>
            			<li>
              				<label for="message">Message <span>*</span></label>
              				<textarea id="message" name="message" rows="5" cols="50" style="height:200px;"></textarea>
            			</li>
            			<li>
              				<input class="button" style="color:#fff;" type="submit"  value="Send" />
            			</li>
       			    </ol>
        		</form>


Is This A Good Question/Topic? 0
  • +

Replies To: PHP contact form gmail smtp help

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: PHP contact form gmail smtp help

Posted 05 September 2010 - 07:44 AM

Please tell us what you mean by "my script wont work". We're not psychic. Please provide any errors you may be experiencing and/or explain how the behavior differs from what you expect.
Was This Post Helpful? 0
  • +
  • -

#3 weemanpow3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 27-September 09

Re: PHP contact form gmail smtp help

Posted 05 September 2010 - 07:56 AM

View PostJackOfAllTrades, on 05 September 2010 - 06:44 AM, said:

Please tell us what you mean by "my script wont work". We're not psychic. Please provide any errors you may be experiencing and/or explain how the behavior differs from what you expect.


Well, when I send a test message from the html contact form, it will tell me Message sent!, but I never get the email. Im not sure if its something with the php form thats not setup correctly. I have tried with other PHPMailler test examples and they all go through instantly with gmail.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_mab4*


Reputation:

Re: PHP contact form gmail smtp help

Posted 05 September 2010 - 03:52 PM

make sure your host support mailing
Was This Post Helpful? 0

Page 1 of 1