pomoore Posted September 7, 2012 Share Posted September 7, 2012 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.... Quote Link to comment https://forums.phpfreaks.com/topic/268115-make-a-php-contact-form-use-smtp/ Share on other sites More sharing options...
happypete Posted September 7, 2012 Share Posted September 7, 2012 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(); Quote Link to comment https://forums.phpfreaks.com/topic/268115-make-a-php-contact-form-use-smtp/#findComment-1376161 Share on other sites More sharing options...
Christian F. Posted September 7, 2012 Share Posted September 7, 2012 Other than that there is no reason to send this as a HTML mail, it's a good solution. Just remove the HTML code, nl2br () call, and replace the $mail->MsgHTML () with the proper pure-text method. Then you're set. Quote Link to comment https://forums.phpfreaks.com/topic/268115-make-a-php-contact-form-use-smtp/#findComment-1376205 Share on other sites More sharing options...
pomoore Posted September 10, 2012 Author Share Posted September 10, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/268115-make-a-php-contact-form-use-smtp/#findComment-1376639 Share on other sites More sharing options...
Christian F. Posted September 11, 2012 Share Posted September 11, 2012 What error do you get, or whetever else is telling you this doesn't work? "Not working" isn't enough information to help you, I'm afraid. You need to be more detailed, so that we actually have something to work with. Quote Link to comment https://forums.phpfreaks.com/topic/268115-make-a-php-contact-form-use-smtp/#findComment-1376923 Share on other sites More sharing options...
pomoore Posted September 11, 2012 Author Share Posted September 11, 2012 Hi, I dont get any error, the page goes to an "error page not found" page and the form does not send to the specified email. Quote Link to comment https://forums.phpfreaks.com/topic/268115-make-a-php-contact-form-use-smtp/#findComment-1376967 Share on other sites More sharing options...
happypete Posted September 12, 2012 Share Posted September 12, 2012 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) Quote Link to comment https://forums.phpfreaks.com/topic/268115-make-a-php-contact-form-use-smtp/#findComment-1377256 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.