Question

Setting up mail servers so contact page can send e-mails.

  • Posted on October 16, 2014
  • ChempoAsked by Chempo

Hi,

I have a php contact form so user can input name, e-mail, website, subject and message. I installed on my droplet php and Postfix. What else do I need or do I need to configure something? My Droplet is Ubuntu 14.04.

I really don’t know how to setup this thing so it works.

Here is the php code:

<?php
    
    if(isset($_POST['email'])) {
        
        $time = time();     // get unix timestamp

        # SETTINGS
        // email to send to
        $email_to = "ENTER EMAIL HERE";                 

        // subject prefix with unix timestamp, each message will have unique id [#]: "CHEMPO Contact Form [#1234567890]- Bla bla"
        $subject_prefix = 'Chempo.com Contact Form [#'.$time.']- ';
    
        function died($error) {

            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();

        }
        // validation expected data exists

        if(!isset($_POST['sender_name']) ||

            !isset($_POST['email']) ||

            !isset($_POST['website']) ||

            !isset($_POST['email_subject']) ||

            !isset($_POST['comments'])) {

            died('We are sorry, but there appears to be a problem with the form you submitted.');      
        }

        $sender_name    =   $_POST['sender_name'];     #$sender_name = mysql_real_escape_string($sender_name); 
        $email_from     =   $_POST['email'];    
        $website        =   $_POST['website']; // not required    
        $email_subject  =   $_POST['email_subject']; 
        $comments       =   $_POST['comments'];

        $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 .= '<span style="color:#e50000;">The Email Address you entered does not appear to be valid.</span><br />';

        }

          $string_exp = "/^[A-Za-z .'-]+$/";

        if(!preg_match($string_exp,$sender_name)) {

          $error_message .= '<span style="color:#e50000;">The Name you entered does not appear to be valid.</span><br />';

        }

        if(strlen($email_subject) < 2) {

          $error_message .= '<span style="color:#e50000;">The Subject you entered do not appear to be valid.</span><br />';

        }

        if(strlen($comments) < 2) {

          $error_message .= '<span style="color:#e50000;">The Message you entered do not appear to be valid.</span><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 .= "Name: ".clean_string($sender_name)."\n";

        $email_message .= "Email: ".clean_string($email_from)."\n";

        $email_message .= "Website: ".clean_string($website)."\n";

        $email_message .= "Subject: ".clean_string($email_subject)."\n";

        $email_message .= "Message: ".clean_string($comments)."\n";



        $email_subject = clean_string($email_subject);
        $email_subject = $subject_prefix.$email_subject;




        // create email headers

        $headers = 'From: '.$email_from."\r\n".

        'Reply-To: '.$email_from."\r\n" .

        'X-Mailer: PHP/' . phpversion();

        @mail($email_to, $email_subject, $email_message, $headers); 

        echo '<span style="color:#00d614;">Thank you for contacting me. I will be in touch with You very soon. Please use navigation bar above to continue browsing my site.</span>';
 
    }

    else{   
    ?>

Thanks in advance.

  • Chempo

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Okay, your question is a little incomplete. First, are you getting any errors as a result of your current setup ? If so what is it ?

Depending on your setup you may want to make sure some of these are installed.

sudo apt-get install php-pear sudo pear install mail sudo pear install Net_SMTP sudo pear install Auth_SASL sudo pear install mail_mime

sudo apt-get install postfix

^ guessing you already ran this

Edit php.ini file from /etc/php5/apache2/php.ini sendmail_path = “/usr/sbin/sendmail -t -i”

#Restart apache that is if you have Apache running sudo /etc/init.d/apache2 restart