PHP Email

Discussion in 'Email' started by Debbie, Jul 1, 2015.

  1. Hi guys, I have been working on my php contact form to email. I got the authenticate part to work but am hoping a staff member will help me with a few more things to get it running. I don't want to post my code and personal information to the forum. If a staff member wouldn't mind helping me out, I would appreciate it. Please send me a message and I will contact you privately. Thanks.
     
  2. Hi guys, just to update on my form. I will post my code but I am going to mask my personal info like email, domain name and password. First, I created the form which I will post, then I created a php file called process.php. This file starts with the required authentication and then the actual code that goes with my form. When I upload this and a client sends me a message, I only get the test email. I was told to put it all in one php file. Any idea what is wrong? Thanks.

    Form
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Untitled Document</title>
    <!-- Bootstrap -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <link href="contactstyles.css" rel="stylesheet" type="text/css">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->

    </head>
    <body>
    <div class="container">
    <div class="row">
    <div class="col-xs offset-center col-md-offset-0 col-md-12"><form action="process.php" id="form" class="form" name="form" method="post" enctype="text/plain" accept-charset="UTF-8">
    <h1>Contact Form</h1>
    <div class="content">
    <div class="intro"></div>
    <div id="section0" >
    <div class="field"><label for="Name">Name:</label><input id="Name" name="Name" required type="text"></div>
    <div class="field"><label for="Email">Email:</label><input id="Email" name="Email" required type="email"></div>
    <div class="field"><div class="edit-options"><div class="edit"></div><div class="delete"></div></div><label for="Comments">Comments:</label><textarea id="Comments" name="Comments" wrap="hard"></textarea></div>
    <div class="field"><input id="Submit" name="Submit" type="submit"></div>
    </div>
    </div>
    </form></div>
    </div>
    </div>


    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery-1.11.2.min.js"></script>

    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.js"></script>
    </body>
    </html>

    process.php

    <html>
    <head><title>Test email via php</title></head>
    <body>
    <?php
    require_once "Mail.php";
    $from = "Sender <[email protected]>";
    $to = "Recipient <[email protected]>";
    $subject = "This is a test email sent via php";
    $body = "This is a test email";
    $host = "mail.domain.com";
    $username = "[email protected]";
    $password = "mypassword";
    $headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));
    $mail = $smtp->send($to, $headers, $body);
    echo "mail sent";
    ?>

    <?php

    $to = '[email protected]';

    $subject = 'Comments from website';

    $Name = trim(($_POST['Name']));

    $Email = trim(($_POST['Email']));

    $Comments = trim(stripslashes(htmlspecialchars($_POST['Comments'])));

    // build the mail message

    $message = "Name: $Name\n\n";

    $message .= "Email Address: $Email\n\n";

    $message .= "Comments: $Comments\n\n";

    $headers = "From: $Name <$Email>";

    mail($to, $subject, $message, $headers);

    header('Location: thank_you.html');

    ?>
    </body>
    </html>





    Hi guys, just to update on my form. I will post my code but I am going to mask my personal info like email, domain name and password. First, I created the form which I will post, then I created a php file called process.php. This file starts with the required authentication and then the actual code that goes with my form. When I upload this and a client sends me a message, I only get the test email. I was told to put it all in one php file. Any idea what is wrong? Thanks.

    Form
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Untitled Document</title>
    <!-- Bootstrap -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <link href="contactstyles.css" rel="stylesheet" type="text/css">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->

    </head>
    <body>
    <div class="container">
    <div class="row">
    <div class="col-xs offset-center col-md-offset-0 col-md-12"><form action="process.php" id="form" class="form" name="form" method="post" enctype="text/plain" accept-charset="UTF-8">
    <h1>Contact Form</h1>
    <div class="content">
    <div class="intro"></div>
    <div id="section0" >
    <div class="field"><label for="Name">Name:</label><input id="Name" name="Name" required type="text"></div>
    <div class="field"><label for="Email">Email:</label><input id="Email" name="Email" required type="email"></div>
    <div class="field"><div class="edit-options"><div class="edit"></div><div class="delete"></div></div><label for="Comments">Comments:</label><textarea id="Comments" name="Comments" wrap="hard"></textarea></div>
    <div class="field"><input id="Submit" name="Submit" type="submit"></div>
    </div>
    </div>
    </form></div>
    </div>
    </div>


    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery-1.11.2.min.js"></script>

    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.js"></script>
    </body>
    </html>

    process.php

    <html>
    <head><title>Test email via php</title></head>
    <body>
    <?php
    require_once "Mail.php";
    $from = "Sender <[email protected]>";
    $to = "Recipient <[email protected]>";
    $subject = "This is a test email sent via php";
    $body = "This is a test email";
    $host = "mail.domain.com";
    $username = "[email protected]";
    $password = "mypassword";
    $headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));
    $mail = $smtp->send($to, $headers, $body);
    echo "mail sent";
    ?>

    <?php

    $to = '[email protected]';

    $subject = 'Comments from website';

    $Name = trim(($_POST['Name']));

    $Email = trim(($_POST['Email']));

    $Comments = trim(stripslashes(htmlspecialchars($_POST['Comments'])));

    // build the mail message

    $message = "Name: $Name\n\n";

    $message .= "Email Address: $Email\n\n";

    $message .= "Comments: $Comments\n\n";

    $headers = "From: $Name <$Email>";

    mail($to, $subject, $message, $headers);

    header('Location: thank_you.html');

    ?>
    </body>
    </html>
     
  3. Sorry! No idea why it duplicated the message. Maybe an admin can erase one of them. Thanks.
     
  4. FredC

    FredC Winhost Staff

    what error are you getting?
     
  5. FredC, thanks so much for the response! I appreciate it. I was told by a staff member that the authentication and other php for the extraction must be in the same file so I put it all in process.php. But, when I upload the file and a person sends me a message, they are taken to the thank you page with no issue but I only receive the "Test email." I am really not sure what is wrong, maybe my php code for the forms is incorrect? Any help would be really appreciated. I really need to get this up and running and just don't know what I am doing wrong. Thanks so much.
     
  6. ComputerMan

    ComputerMan Winhost Staff

    Sounds like you're calling on the wrong PHP file with the settings to send the email message. If you're calling on our test script that we uploaded to your site from your PHP page. Then all it will do is keep sending you the test message.

    You need to update your PHP page to make sure it calls on your PHP file with the settings to send the email message.
     
    Michael likes this.
  7. Thanks for getting back to me. I put both scripts in one file called process.php. Should I put them in separate ones so I can put the test script in sendemailtest.php uploading it first? It will immediately send me the test. Then I would just put my form php in process.php.
     
  8. ComputerMan

    ComputerMan Winhost Staff

    I don't know what you mean by this statement.

    However, to try and avoid confusion. Don't use our test script that we uploaded to your site. We only uploaded it to prove to you that you can send from your site using PHP with SMTP authentication against our mail server.

    Don't mix our test script with your PHP scripts. Don't combined them in anyway.

    Our test script is only there as an example. It's not programmed to be used with your PHP Form.

    Within your PHP form page I'm assuming you might have a line of code that says: "action=" and it points to a PHP page containing the code to send the email message. Make sure you change it so it points to the one you created. The one that sends the email message using your form. I believe your form is pointing to our test script. I think that is why you're having an issue when someone clicks on send. You're getting our test message. You pointed it to our test script. This won't work.
     
    Michael likes this.
  9. ComputerMan

    ComputerMan Winhost Staff

    If you need more help send us a link to where you grabbed the PHP form script.
     
  10. Hi and thanks for getting back to me. I am so sorry about any confusion. I was told that I had to use this https://support.Winhost.com/kb/a826/how-to-send-email-from-a-php-application.aspx I filled out the information and put that in my process.php file which is the form action for my form. I then added in to the same file my php to extract information from the fields. When I upload everything, I just get the test email.

    Am I supposed to put the authentication is one separate php file and call it something like sendmail.php? If I fo that, I immediately get the test email. Then, I would just put my actual php code to extract the form fields in the process.php file. Does this make sense at all? I am sorry. I know I was told everything had to be in one file and that I had to run the authentication in order to be able to proceed with my form php.

    Is there anyway you could help me with my form and show me an example of what I need to do? If I could see what you mean, it would help a lot. Thanks so much.
     
    Last edited by a moderator: Oct 14, 2015
  11. ComputerMan

    ComputerMan Winhost Staff

    I took the time to look around the internet and found a really good example from this web page article here: https://solutions.hostmysite.com/in...ationauthentication-is-required-by-hostmysite

    I personally don't code at all but I get by.

    With that being said from the web page article I provided to you above. Lets get going:

    There will be a total of two files. One that you need to edit. The other you don't need to edit.

    First copy and paste the following code into a notepad. You want to name this file: contactform.htm


    HTML:
    <html>
    <form name="contactform" method="post" action="send_form_email.php">
    <table width="450px">
    <tr>
    <td valign="top">
    <label for="first_name">First Name *</label>
    </td>
    <td valign="top">
    <input type="text" name="first_name" maxlength="50" size="30">
    </td>
    </tr>
    <tr>
    <td valign="top"">
    <label for="last_name">Last Name *</label>
    </td>
    <td valign="top">
    <input type="text" name="last_name" maxlength="50" size="30">
    </td>
    </tr>
    <tr>
    <td valign="top"">
    <label for="home_address">Home Address</label>
    </td>
    <td valign="top">
    <textarea name="home_address" maxlength="100" cols="25 Rows="6"></textarea>
    </td>
    </tr>
    <tr>
    <td valign="top">
    <label for="email">Email Address *</label>
    </td>
    <td valign="top">
    <input type="text" name="email" maxlength="80" size="30">
    </td>
    </tr>
    <tr>
    <td valign="top">
    <label for="telephone">Telephone Number</label>
    </td>
    <td valign="top">
    <input type="text" name="telephone" maxlength="30" size="30">
    </td>
    </tr>
    <tr>
    <td valign="top">
    <label for="comments">Comments *</label>
    </td>
    <td valign="top">
    <textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea>
    </td>
    </tr>
    <tr>
    <td colspan="2" style="text-align:center"> * - Denotes a required field
    <input type="submit" value="Submit">
    </td>
    </tr>
    </table>
    </form>
    </html>
    Don't edit the above file. Upload this file into your site. You might want to place it in a different directory just to test it.

    ------ File Number 2 below (this one you need to do some editing)-------

    Next what you want to do is create a different file and call it: send_form_email.php

    Copy and paste the following code within that file:

    PHP:
    <?php
    if(isset($_POST['email'])) {
    require_once 
    "Mail.php"//PEAR mail is already installed in our current environment

    $email_to "[email protected]";  //Enter the email you want to send the form to
    $email_subject "This is a test";  // You can put whatever subject here

    $host "mail.yourdomain.xyz";  // The hostname of your mail server
    // If your email is with HostMySite, use the actual hostname of your mail server, for example:
    //   SmarterMail: mailXX.safesecureweb.com OR win-mailXX.hostmanagement.net
    //   OpenX-change: smtp.hostmanagement.net
    // Contact Support if you aren't sure what to enter here.

    $username "[email protected]";  // A valid email address you have setup
    $from_address "[email protected]"// Your email address again
    $password "your_password";  // Password for the above email address
    $reply_to "[email protected]";  //Enter the email you want customers to reply to
    $port "587"// This is the default port. Try port 25

    function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo 
    "These errors appear below.<br /><br />";
    echo 
    $error."<br /><br />";
    echo 
    "Please go back and fix these errors.<br /><br />";
    die();
    }

    // Validate expected data exists
    if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['home_address']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['comments'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');
    }

    $first_name $_POST['first_name']; // required
    $last_name $_POST['last_name']; // required
    $home_address $_POST['home_address']; // not required
    $email_from $_POST['email']; // required
    $telephone $_POST['telephone']; // not required
    $comments $_POST['comments']; // required
    $error_message "";
    $email_exp '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(!
    preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    }
    $string_exp "/^[A-Za-z .'-]+$/";
    if(!
    preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
    }
    if(!
    preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
    }
    if(
    strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
    }
    if(
    strlen($error_message) > 0) {
    died($error_message);
    }
    $email_message "Form details below.\n\n";
    function 
    clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return 
    str_replace($bad,"",$string);
    }
    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Home Address: ".clean_string($home_address)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";

    // This section creates the email headers
    $auth = array('host' => $host'auth' => true'username' => $username'password' => $password'port' => $port);
    $headers = array('From' => $from_address'To' => $email_to'Subject' => $email_subject'Reply-To' => $reply_to);

    // This section send the email
    $smtp Mail::factory('smtp'$auth);
    $mail $smtp->send($email_to$headers$email_message);

    if (
    PEAR::isError($mail)) {?>
    <!-- include your own failure message html here -->
      Unfortunately, the message could not be sent at this time. Please try again later.

    <!-- Uncomment the line below to see errors with sending the message -->
    <!-- <?php echo("<p>"$mail->getMessage()."</p>"); ?> -->

    <?php } else { ?>

    <!-- include your own success message html here -->

    Thank you for contacting us. We will be in touch with you very soon.

    <?php } } ?>
    Within this send_form_email.php you only need to edit the following lines with your information:

    (Remember our hosting environment needs SMTP authentication) So you need to authentication against our mail server in order to send the email message.

    So in closing:

    You need to upload the send_form_email.php within the same directory as the contactform.htm directory.

    Your own sending form you're using will not work with my test scripts. So please don't mix them.
     
    Michael likes this.
  12. I really appreciate you doing that for me. I created my form using a generator which is great because it puts in the required fields for me so I really wanted to stick with that. I only have a few fields. Name: Email: Comments:. I like to keep it simple for a user. With the code you sent me, I can sort of see what is going on. I tried some different things but I still am not receiving the information. I instead get, "Comments from website" for the subject title and body. It doesn't seem to be getting the field information.

    My form is the same. I'm sorry, I am just really baffled by this and don't know what I am doing wrong.

    My Form

    Form
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Untitled Document</title>
    <!-- Bootstrap -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <link href="contactstyles.css" rel="stylesheet" type="text/css">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->

    </head>
    <body>
    <div class="container">
    <div class="row">
    <div class="col-xs offset-center col-md-offset-0 col-md-12"><form action="process.php" id="form" class="form" name="form" method="post" enctype="text/plain" accept-charset="UTF-8">
    <h1>Contact Form</h1>
    <div class="content">
    <div class="intro"></div>
    <div id="section0" >
    <div class="field"><label for="Name">Name:</label><input id="Name" name="Name" required type="text"></div>
    <div class="field"><label for="Email">Email:</label><input id="Email" name="Email" required type="email"></div>
    <div class="field"><div class="edit-options"><div class="edit"></div><div class="delete"></div></div><label for="Comments">Comments:</label><textarea id="Comments" name="Comments" wrap="hard"></textarea></div>
    <div class="field"><input id="Submit" name="Submit" type="submit"></div>
    </div>
    </div>
    </form></div>
    </div>
    </div>


    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery-1.11.2.min.js"></script>

    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.js"></script>
    </body>
    </html>

    The second file is called process.php
    <html>
    <body>
    <?php
    require_once "Mail.php";
    $from = "Sender <[email protected]>";
    $to = "Recipient <[email protected]>";
    $subject = "Comments from website";
    $body = "Comments from website";
    $host = "mail.domain.com";
    $username = "[email protected]";
    $password = "mypassword";
    $headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));
    $Name = $_POST['Name'];
    $Email = $_POST['Email'];
    $Comments = $_POST['Comments'];
    $email = $smtp->send($to, $headers, $body);
    header('Location: thank_you.html');
    ?>
    </body>
    </html>
     
  13. Hi! I was able to get help on my code and now it works. :)
     
    Michael likes this.

Share This Page