Jump to content

PHP Mailer Contact form


whitto

Recommended Posts

I am attempting to build a SMTP contact form however when my form is submitted i get an error message from google that says my account has been accessed.  Also will this work fine with JS form validation?  Thank you in advance.

 

 

 
 
<?php
if(isset($_POST['submit'])) 
{
 
$message=
'Full Name: '.$_POST['fullname'].'<br />
Email: '.$_POST['emailid'].'<br />
Comments: '.$_POST['comments'].'
';
    require "PHPMailer-master/class.phpmailer.php"; //include phpmailer class
      
    // Instantiate Class  
    $mail = new PHPMailer();  
      
    // Set up SMTP  
    $mail->IsSMTP();                // Sets up a SMTP connection  
    $mail->SMTPAuth = true;         // Connection with the SMTP does require authorization    
    $mail->SMTPSecure = "ssl";      // Connect using a TLS connection  
    $mail->Host = "smtp.gmail.com";  //Gmail SMTP server address
    $mail->Port = 465;  //Gmail SMTP port
    $mail->Encoding = '7bit';
    
    // Authentication  
    $mail->Username   = "me@gmail.com"; //Gmail address
    $mail->Password   = "pass"; //  Gmail password
      
    // Compose
    $mail->SetFrom($_POST['emailid'], $_POST['fullname']);
    $mail->AddReplyTo($_POST['emailid'], $_POST['fullname']);
    $mail->Subject = "New Contact Form Enquiry";      // Subject   
    $mail->MsgHTML($message);
 
    // Send To  
    $mail->AddAddress("me@gmail.com", "Mr. Example"); // Where to send it - Recipient
    $result = $mail->Send(); // Send!  
 
 
unset($mail);
 
}
?>
 
 
 
Link to comment
Share on other sites

The From: header should always be from from the actual account on the server sending the email, in this case "me@gmail.com" since that's really where the email is being sent FROM. If they don't match, most, if not all, of the more reputable ISPs will flag it as spam or worse, just not deliver it. It could also be why google is sending you that message.

 

There is nothing in your PHP to be able to know if it will "work fine with js validation". That's all done clientside, in the HTML, which you haven't shown us.

Link to comment
Share on other sites

The From: header should always be from from the actual account on the server sending the email, in this case "me@gmail.com" since that's really where the email is being sent FROM. If they don't match, most, if not all, of the more reputable ISPs will flag it as spam or worse, just not deliver it. It could also be why google is sending you that message.

 

There is nothing in your PHP to be able to know if it will "work fine with js validation". That's all done clientside, in the HTML, which you haven't shown us.

 

Do you mean this line?

    $mail->SetFrom($_POST['emailid'], $_POST['fullname']);
 

 

Sorry about that here is my JS

/*
  Jquery Validation using jqBootstrapValidation
   example is taken from jqBootstrapValidation docs 
  */
$(function() {
 
 $("input,textarea").jqBootstrapValidation(
    {
     preventSubmit: true,
     submitError: function($form, event, errors) {
    
     },
     submitSuccess: function($form, event) {
      event.preventDefault(); // prevent default submit behaviour
       // get values from FORM
       var name = $("input#name").val();  
       var email = $("input#email").val(); 
       var message = $("textarea#message").val();
        var firstName = name; // For Success/Failure Message
           // Check for white space in name for Success/Fail message
        if (firstName.indexOf(' ') >= 0) {
  firstName = name.split(' ').slice(0, -1).join(' ');
         }        
$.ajax({
                url: "./bin/contact_me.php",
             type: "POST",
             data: {name: name, email: email, message: message},
             cache: false,
             success: function() {  
             // Success message
                $('#success').html("<div class='alert alert-success'>");
                $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
             .append( "</button>");
               $('#success > .alert-success')
             .append("<strong>Your message has been sent. </strong>");
    $('#success > .alert-success')
  .append('</div>');
      
    //clear all fields
    $('#contactForm').trigger("reset");
        },
     error: function() { 
  // Fail message
   $('#success').html("<div class='alert alert-danger'>");
             $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
              .append( "</button>");
             $('#success > .alert-danger').append("<strong>Sorry "+firstName+" it seems that my mail server is not responding...</strong> Could you please email me directly to <a href='mailto:me@example.com?Subject=Message_Me from myprogrammingblog.com'>me@example.com</a> ? Sorry for the inconvenience!");
          $('#success > .alert-danger').append('</div>');
  //clear all fields
  $('#contactForm').trigger("reset");
      },
           })
         },
         filter: function() {
                   return $(this).is(":visible");
         },
       });
 
      $("a[data-toggle=\"tab\"]").click(function(e) {
                    e.preventDefault();
                    $(this).tab("show");
        });
  });
 
 
/*When clicking on Full hide fail/success boxes */ 
$('#name').focus(function() {
     $('#success').html('');
  });
 
Edited by whitto
Link to comment
Share on other sites

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.