6

How can I go about setting up an SMTP server to send email as *@mydomain.com?

How can I then use this SMTP server with a PHP script that grabs data posted from my website's contact form and send it to me as an email?

I'm using Ubuntu 13.10 and webmin, but I can do the changes locally on the server.

here is my PHP script..

#!/var/www/php
<?php

 if(isset($_POST['email'])) {





$email_to = "havok@havoksecurity.com";

$email_subject = "havok security contact form";





function died($error) {

    // your error code can go here

    echo "We are very sorry, but there was an error 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['name']) ||

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

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

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

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

}



$first_name = $_POST['name']; // required

$email_from = $_POST['email']; // required

$telephone = $_POST['phone']; // not required

$comments = $_POST['message']; // 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,$name)) {

$error_message .= 'The Name you entered does not appear to be valid.<br />';

}


if(strlen($message) < 2) {

$error_message .= 'The Message 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 .= "Name: ".clean_string($name)."\n";

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

$email_message .= "Phone: ".clean_string($phone)."\n";

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





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

  ?>



    <!-- include your own success html here -->
    <link rel="stylesheet" type="text/css" media="screen" href="css/reset.css">
    <link rel="stylesheet" type="text/css" media="screen" href="css/style.css">
    <script src="js/jquery-1.7.min.js"></script>
    <script src="js/jquery.easing.1.3.js"></script>
    <script src="js/FF-cash.js"></script>
    <p>&nbsp;</p>
    <p><img src="images/security_logo.png" width="596" height="190" alt="security_logo" longdesc="http://havoksecurity.com" /><img src="images/twitter_icon-1.png" width="24" height="24" alt="twitter_icon" /><img src="images/facebook_icon-2.png" width="24" height="24" alt="facebook_icon" /><img src="images/google_icon-3.png" width="24" height="24" alt="google_icon" /></p>
    <p>&nbsp;</p>
    <p>Thank you for contacting us. We will respond within 24 hours. 
    <?php

    }

    ?>
    </p>

i have installed iRedMail, on a fresh install of Ubuntu 12.4 but it wont send or recieve emails. So it seems I'll have to figure that out before i can make it use this php script. all help would be very appreciated.

I just tried top send a message so i can see the error here is the error log

Feb  5 00:53:27 havoksecurity postfix/smtpd[8193]: connect from unknown[192.168.1.126]
Feb  5 00:53:53 havoksecurity postfix/smtpd[8193]: lost connection after UNKNOWN from unknown[192.168.1.126]
Feb  5 00:53:53 havoksecurity postfix/smtpd[8193]: disconnect from unknown[192.168.1.126]
Feb  5 00:57:13 havoksecurity postfix/anvil[8195]: statistics: max connection rate 1/60s for (smtp:192.168.1.126) at Feb  5 00:53:27
Feb  5 00:57:13 havoksecurity postfix/anvil[8195]: statistics: max connection count 1 for (smtp:192.168.1.126) at Feb  5 00:53:27
Feb  5 00:57:13 havoksecurity postfix/anvil[8195]: statistics: max cache size 1 at  Feb  5 00:53:27
Feb  5 01:10:45 havoksecurity postfix/pickup[7985]: DDEACD40FD2: uid=0 from=<havok@havoksecurity.com>
Feb  5 01:10:45 havoksecurity postfix/cleanup[9202]: DDEACD40FD2: message-id=<1391580645.9181@havoksecurity.com>
Feb  5 01:10:45 havoksecurity postfix/qmgr[1687]: DDEACD40FD2: from=<havok@havoksecurity.com>, size=641, nrcpt=1 (queue active)
Feb  5 01:10:46 havoksecurity amavis[1412]: (01412-06) NOTICE: reconnecting in response to: err=2006, HY000, DBD::mysql::st execute failed: MySQL server has gone away at (eval 100) line 166, <GEN37> line 1834.
Feb  5 01:10:47 havoksecurity postfix/smtpd[9211]: connect from localhost[127.0.0.1]
Feb  5 01:10:47 havoksecurity postfix/trivial-rewrite[9204]: warning: do not list domain havoksecurity.com in BOTH mydestination and virtual_mailbox_domains
Feb  5 01:10:47 havoksecurity postfix/smtpd[9211]: 527E6D40F94: client=localhost[127.0.0.1]
Feb  5 01:10:47 havoksecurity postfix/cleanup[9202]: 527E6D40F94: message-id=<1391580645.9181@havoksecurity.com>
Feb  5 01:10:47 havoksecurity postfix/qmgr[1687]: 527E6D40F94: from=<havok@havoksecurity.com>, size=1098, nrcpt=1 (queue active)
Feb  5 01:10:47 havoksecurity postfix/smtpd[9211]: disconnect from localhost[127.0.0.1]
Feb  5 01:10:47 havoksecurity amavis[1412]: (01412-06) Passed CLEAN, MYUSERS <havok@havoksecurity.com> -> <user@gmail.com>, Message-ID: <1391580645.9181@havoksecurity.com>, mail_id: tx8Bn4bMXniG, Hits: -10, size: 641, queued_as: 527E6D40F94, 1404 ms
Feb  5 01:10:47 havoksecurity postfix/smtp[9207]: DDEACD40FD2: to=<user@gmail.com>, relay=127.0.0.1[127.0.0.1]:10024, delay=1.6, delays=0.09/0.01/0/1.5, dsn=2.0.0, status=sent (250 2.0.0 from MTA([127.0.0.1]:10025): 250 2.0.0 Ok: queued as 527E6D40F94)
Feb  5 01:10:47 havoksecurity postfix/qmgr[1687]: DDEACD40FD2: removed
Feb  5 01:11:17 havoksecurity postfix/smtp[9219]: connect to gmail-smtp-in.l.google.com[74.125.196.27]:25: Connection timed out
Feb  5 01:11:47 havoksecurity postfix/smtp[9219]: connect to alt1.gmail-smtp-in.l.google.com[173.194.75.27]:25: Connection timed out
Feb  5 01:12:17 havoksecurity postfix/smtp[9219]: connect to alt2.gmail-smtp-in.l.google.com[173.194.78.27]:25: Connection timed out
Feb  5 01:12:47 havoksecurity postfix/smtp[9219]: connect to alt3.gmail-smtp-in.l.google.com[74.125.136.27]:25: Connection timed out
Feb  5 01:13:17 havoksecurity postfix/smtp[9219]: connect to alt4.gmail-smtp-in.l.google.com[173.194.70.27]:25: Connection timed out
Feb  5 01:13:17 havoksecurity postfix/smtp[9219]: 527E6D40F94: to=<user@gmail.com>, relay=none, delay=150, delays=0.04/0.02/150/0, dsn=4.4.1, status=deferred (connect to alt4.gmail-smtp-in.l.google.com[173.194.70.27]:25: Connection timed out)
HaVoK
  • 61

1 Answers1

4

I'd recommend you look into something like Postfix as your email server. This will allow you to send email.

Just be sure that, if you want to run a full email server (with inbound traffic), you setup your DNS zone file to handle inbound mail traffic.

For the PHP script you're referring to... do you mean that you want to send email from something like a form, with form data as part of the email and perhaps some other default info?

Flyk
  • 1,480
  • 3
  • 18
  • 24