Jump to content

Recommended Posts

Hi All,

I am in dire need of help!!  I am trying to use this contact form http://www.jbbarry.ie/jbb/contact.html (which works perfectly on a testing server) but will not work on the company domain. It uses a PHP mailer, which apparently my web host has disabled due to spam? So I need to make my PHP mailer work with SMTP.

 

Below is the form code:

 

<div id="brochure">  
Request a Company Brochure.
<form action="mailer.php" method="post">
<fieldset>
<input type="text" name="name" value="Name…"  onfocus="this.value=(this.value=='Name…')? '' : this.value ;" />
<input type="text" name="email" value="Email…"  onfocus="this.value=(this.value=='Email…')? '' : this.value ;" />
<input type="submit" name="submit" class="button" id="submit_btn" value="Submit →"/>
</fieldset>
</form>

 

And below is the PHP script mailer.php, can anyone help me out and explain how to change it into smtp authentication?

 

<?php 
if(isset($_POST['submit'])) { 
$to = "peteomoore@gmail.com"; 
$subject = "Brochure Request"; 
$name_field = $_POST['name']; 
$email_field = $_POST['email']; 
  
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message"; 
  
mail($to, $subject, $body); 


   header('Location: brochure-request.html');
   } else{


echo "test!"; 
} 
?>

 

Thanks in advance for any help you can give me....

 

Link to comment
https://forums.phpfreaks.com/topic/268115-make-a-php-contact-form-use-smtp/
Share on other sites

I spent hours trying to figure that one out a while back, so hopefully this will help:

 

Your form uses the php mail() function, you need PHP mailer...

 

download phpmailer: http://phpmailer.worxware.com/index.php?pg=phpmailer

 

include it and define an email to send the form to:

 require_once('phpmailer/class.phpmailer.php');
include("phpmailer/class.smtp.php");

$emailaddress = 'your@youremail.com';

 

create your message from your form:

 

$message=
'Name:	'.$_POST['name'].'<br />
Email:	'.$_POST['email'].'<br />
Subject:	'.$_POST['subject'].'<br />
IP:	'.$_SERVER['REMOTE_ADDR'].'<br /><br />
Message:<br /><br />
'.nl2br($_POST['msg']).'
';

 

and sent it with SMTP (you will need to create an email address in your hosting control panel something like: smtp@yourdomain.com)

 

$mail             = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
//$mail->SMTPDebug  = 2;                     // 1 = errors and messages,2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port       = 25;                    // set the SMTP port for the GMAIL server
$mail->Username   = "smtp@yourdomain.com"; // SMTP account username (the email account your created)
$mail->Password   = "123456";        // SMTP account password (the password for the above email account)

$mail->CharSet  = 'UTF-8';  // so it interprets foreign characters
$mail->SetFrom($_POST['email']);
$mail->AddReplyTo($_POST['email']);
$mail->Subject    = "Contact form from ".$_POST['name']." ";
$mail->MsgHTML($message);

$mail->AddAddress($emailaddress);
$mail->Send();

 

Hi,

 

Many thanks for getting back to me, but unfortunately i Had no joy with this! I downloaded PHP-Mailer and included phpmailer.inc.php and smtp.inc.php into a phpmailer folder in my main website folder.

 

Below is the form code im using:

 

<div id="brochure">
Request a Company Brochure.
<form action="mailer2.php" method="post">
<fieldset>
<input type="text" name="name" value="Name…"  onfocus="this.value=(this.value=='Name…')? '' : this.value ;" />
<input type="text" name="email" value="Email…"  onfocus="this.value=(this.value=='Email…')? '' : this.value ;" />
<input type="submit" name="submit" class="button" id="submit_btn" value="Submit →"/>
</fieldset>
</form>

 

and below is the new mailer php script i created using your advice:

 

<?php 
require_once('phpmailer/phpmailer.inc.php');
include("phpmailer/smtp.inc.php");

$emailaddress = 'pomoore@jbbarry.ie';

$message=
'Name:	 '.$_POST['name'].'<br />
Email:	 '.$_POST['email'].'<br />
Subject:	 '.$_POST['subject'].'<br />
IP:	 '.$_SERVER['REMOTE_ADDR'].'<br /><br />
Message:<br /><br />
'.nl2br($_POST['msg']).'
';

$mail             = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "smtp.gmail.com"; // SMTP server
//$mail->SMTPDebug  = 2;                     // 1 = errors and messages,2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "smtp@gmail.com"; // sets the SMTP server
$mail->Port       = 25;                    // set the SMTP port for the GMAIL server
$mail->Username   = "test@gmail.com"; // SMTP account username (the email account your created)
$mail->Password   = "password";        // SMTP account password (the password for the above email account)

$mail->CharSet  = 'UTF-8';  // so it interprets foreign characters
$mail->SetFrom($_POST['email']);
$mail->AddReplyTo($_POST['email']);
$mail->Subject    = "Contact form from ".$_POST['name']." ";
$mail->MsgHTML($message);

$mail->AddAddress($emailaddress);
$mail->Send();
?> 

 

Apologies if this seems stupid, as im sure ive missed something easy! but im a graphic designer and learnt an easy php script that has never needed this level of thought before!!

 

Thanks again...

make sure the relative path to the phpmailer script is correct. you may need to add '../' if you placed it in the root folder as you may be calling it form the jbb folder..?

 

require_once('../phpmailer/phpmailer.inc.php');

 

never tried it with gmail, try creating a new email test@jbbarry.ie in your hosting account and see if that works

 

$mail             = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.jbbarry.ie"; // SMTP server
//$mail->SMTPDebug  = 2;                     // 1 = errors and messages,2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.jbbarry.ie"; // sets the SMTP server
$mail->Port       = 25;                    // set the SMTP port for the GMAIL server
$mail->Username   = "test@jbbarry.ie"; // SMTP account username (the email account your created)
$mail->Password   = "whateverpassowordyouchose";        // SMTP account password (the password for the above email account)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.