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

Posted October 16, 2014 6.4k views


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:


    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 = ' 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 />";


        // 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) {



        $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>';



Thanks in advance.

  • Chempo

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.

Submit an Answer
1 answer

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 NetSMTP
sudo pear install Auth
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

  • Yeah forgot to mentioned it :) . If I go and send mail through contact form it will say on the site it was sent, but in the error log it says that it cannot find send mail.

    I will try your suggestions and let you know if it works.

  • Nope still nothing. I’ve did everything you said.

  • You need to give the error you get from the logs.
    Sendmail usually comes pre-installed on most distros.

  • mail.log

    Oct 16 18:09:15 Chempo postfix/pickup[15278]: A0436233AC: uid=33 from=<www-data>
    Oct 16 18:09:15 Chempo postfix/cleanup[15585]: A0436233AC: message-id=<>
    Oct 16 18:09:15 Chempo postfix/qmgr[14371]: A0436233AC: from=<>, size=535, nrcpt=1 (queue active)
    Oct 16 18:09:15 Chempo postfix/smtp[15587]: connect to[2a00:1450:4010:c04::1b]:25: Network is unreachable
    Oct 16 18:09:17 Chempo postfix/smtp[15587]: A0436233AC: to=<>,[]:25, delay=1.5, delays=0.02/0/0.74/0.7, dsn=2.0.0, status=sent (250 2.0.0 OK 1413497357 g8si36725147lae.48 - gsmtp)
    Oct 16 18:09:17 Chempo postfix/qmgr[14371]: A0436233AC: removed
    Oct 16 18:24:04 Chempo postfix/pickup[15278]: B9ABD233AC: uid=33 from=<www-data>
    Oct 16 18:24:04 Chempo postfix/cleanup[15741]: B9ABD233AC: message-id=<>
    Oct 16 18:24:04 Chempo postfix/qmgr[14371]: B9ABD233AC: from=<>, size=534, nrcpt=1 (queue active)
    Oct 16 18:24:05 Chempo postfix/smtp[15743]: B9ABD233AC: to=<>,[]:25, delay=1.2, delays=0.02/0.01/0.67/0.54, dsn=2.0.0, status=sent (250 2.0.0 OK 1413498245 q13si36644467lal.108 - gsmtp)
    Oct 16 18:24:06 Chempo postfix/qmgr[14371]: B9ABD233AC: removed
    Oct 16 18:24:25 Chempo postfix/pickup[15278]: CA489233AC: uid=33 from=<www-data>
    Oct 16 18:24:25 Chempo postfix/cleanup[15741]: CA489233AC: message-id=<>
    Oct 16 18:24:25 Chempo postfix/qmgr[14371]: CA489233AC: from=<>, size=534, nrcpt=1 (queue active)
    Oct 16 18:24:25 Chempo postfix/smtp[15743]: connect to[2a00:1450:4010:c04::1a]:25: Network is unreachable
    Oct 16 18:24:27 Chempo postfix/smtp[15743]: CA489233AC: to=<>,[]:25, delay=1.2, delays=0.01/0/0.37/0.82, dsn=2.0.0, status=sent (250 2.0.0 OK 1413498267 s10si36784546lal.38 - gsmtp)
    Oct 16 18:24:27 Chempo postfix/qmgr[14371]: CA489233AC: removed
  • Pff wow it works actually, it was in the spam folder.

  • But why does it end in spam?

  • That’s probably because you don’t have SPF/PTR records properly configured.
    Read up on that.
    Check this answer out

  • Thanks man, you have been very helpful.