-1

We created this contact form a long time ago, but we have recently had issues with our sendmail, now i need to change this to use SMTP and that i haven't done before. Is it much work or just a matter of changing few lines? Any tips are welcome.

You can see our whole script here, it's very simple...

<?php
    if(!$_POST) exit;

    function tommus_email_validate($email) {
        return filter_var($email, FILTER_VALIDATE_EMAIL) && preg_match('/@.+\./', $email);
    }

    $name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['phone']; $comments = $_POST['comments'];

    if(trim($name) == '') {
        exit('<div class="error_message">You must enter your name.</div>');
    } else if(trim($name) == 'Name') {
        exit('<div class="error_message">You must enter your name.</div>');
    } else if(trim($email) == '') {
        exit('<div class="error_message">Please enter a valid email address.</div>');
    } else if(!tommus_email_validate($email)) {
        exit('<div class="error_message">You have entered an invalid e-mail address.</div>');
    } else if(trim($comments) == 'Tell us what you think!') {
        exit('<div class="error_message">Please enter your message.</div>');
    } else if(trim($comments) == '') {
        exit('<div class="error_message">Please enter your message.</div>');
    } else if( strpos($comments, 'href') !== false ) {
        exit('<div class="error_message">Please leave links as plain text.</div>');
    } else if( strpos($comments, '[url') !== false ) {
        exit('<div class="error_message">Please leave links as plain text.</div>');
    } if(get_magic_quotes_gpc()) { $comments = stripslashes($comments); }    

    $address = 'hello@basicagency.com';

    $e_subject = 'You\'ve been contacted by ' . $name . '.';

    $e_body = "You have been contacted by $name from your contact form, their additional message is as follows." . "\r\n" . "\r\n";
    $e_content = "\"$comments\"" . "\r\n" . "\r\n";
    $e_reply = "You can contact $name via email, $email (or by phone if supplied: $phone)";    

    $msg = wordwrap( $e_body . $e_content . $e_reply, 70 );

    $headers = "From: $email" . "\r\n";
    $headers .= "Reply-To: $email" . "\r\n";
    $headers .= "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type: text/plain; charset=utf-8" . "\r\n";
    $headers .= "Content-Transfer-Encoding: quoted-printable" . "\r\n";

    if(mail($address, $e_subject, $msg, $headers)) {
        echo "<fieldset><div id='success_page'><p>Thank you $name, your message has been submitted to us.</p></div></fieldset>";
    }
Blundering Philosopher
  • 5,116
  • 2
  • 35
  • 50
Jack Johnson
  • 549
  • 4
  • 10
  • 23
  • Are you trying to use your own SMTP server to send these mails that is hosted on another machine? I use postfix to push my e-mail and just use the [relayhost](http://www.postfix.org/postconf.5.html#relayhost) to direct the mail to there. – Matt D. Mar 16 '16 at 15:38
  • I do have my own SMTP server, the issue is changing the current script above to use SMTP and not sendmail as it is now. – Jack Johnson Mar 25 '16 at 22:58

2 Answers2

2

You can use a package for handling e-mails, such as PHPMailer, just use the included methods to build your message, instead of the $headers variable. Once you have downloaded PHPMailer and have it somewhere accessable by your script, replace this:

$headers = "From: $email" . "\r\n";
$headers .= "Reply-To: $email" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=utf-8" . "\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable" . "\r\n";

if(mail($address, $e_subject, $msg, $headers)) {
    echo "<fieldset><div id='success_page'><p>Thank you $name, your message has been submitted to us.</p></div></fieldset>";
}

with something like this:

require '/path/to/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();

// SMTP server details 
$mail->Host = "mail.example.com";
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "yourname@example.com";
$mail->Password = "yourpassword";

// message details 
$mail->setFrom($email, $email);
$mail->addReplyTo($email, $email);
$mail->addAddress($address, $address);
$mail->Subject = $e_subject;
$mail->Body = $msg;

// send 
if($mail->send()) {
    echo "<fieldset><div id='success_page'><p>Thank you $name, your message has been submitted to us.</p></div></fieldset>";
}
else{
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}
Rainner
  • 589
  • 3
  • 7
0

You could do various thinks:

Use SMTP client like PHPMAIL (https://github.com/PHPMailer/PHPMailer) or PEAR (updated):

<?php
require 'PHPMailerAutoload.php';    
$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

Or if you could use gmail (https://support.google.com/a/answer/176600?hl=en):

// Pear Mail Library
require_once "Mail.php";

$from = '<fromaddress@gmail.com>';
$to = '<toaddress@yahoo.com>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'johndoe@gmail.com',
        'password' => 'passwordxxx'
    ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
}

Or try this answer: https://stackoverflow.com/a/20338651/6058255 "without modify anything".

Community
  • 1
  • 1
JP. Aulet
  • 4,117
  • 3
  • 21
  • 34
  • 1
    I doubt your second example will work despite it being upvoted on the other question – Ivan Yarych Mar 27 '16 at 08:59
  • I guess you are right (there's no user and password) but I want to show different forms to do that (for that the link). I updated my answer to be correct. Thanks! – JP. Aulet Mar 27 '16 at 11:24