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 ="[email protected]";
$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 = "[email protected]";
$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 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>
//////////////////////////////////////////////// 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 ="[email protected]";
$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 = "[email protected]";
$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 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>