From: address must be from domain on the server (php mail)

Currently using a nice php mail form. Everything works as tested locally yet when I uploaded live the host wants the ‘From: address’ to be an address from a domain on the server, e.g 'noreply@domain.net

What I’d like to do is add this variable;

$from_email = “noreply@domain.net”;

to the mail send file I have that is working (below).


<?php
/*
Credits: Bit Repository
URL: http://www.bitrepository.com/
*/

include dirname(dirname(__FILE__)).'/includes/config.php';

error_reporting (E_ALL ^ E_NOTICE);

$post = (!empty($_POST)) ? true : false;

if($post)
{
include 'functions.php';

$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
//$subject = stripslashes($_POST['subject']);
$message = stripslashes($_POST['message']);

$error = '';

// Check name

if(!$name)
{
$error .= 'Please enter your name.<br />';
}

// Check email

if(!$email)
{
$error .= 'Please enter an e-mail address.<br />';
}

if($email && !ValidateEmail($email))
{
$error .= 'Please enter a valid e-mail address.<br />';
}

// Check message (length)

if(!$message || strlen($message) < 15)
{
$error .= "Please enter your message. It should have at least 15 characters.<br />";
}

if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $message,
     "From: ".$name." <".$email.">\\r\
"
    ."Reply-To: ".$email."\\r\
"
    ."Message: PHP/" . phpversion());


if($mail)
{
echo 'OK';
}

}
else
{
echo '<div class="notification_error">'.$error.'</div>';
}

}
?>

I’d also like to change the way the mail is being sent to me. As is above the message is showing up in the subject line. Ideally i’d like a static subject line such as “Query from website from $name” and have the $message show up in the mail. The current mail output is not reader friendly. The current output is;

From: stephen <stephen@test.com>
Reply-To: stephen@test.com
Message: PHP/5.0.3

I’d like to be something like;

Website query sent at: <time stamp is possible>
From: <$name> - <$email>
Message: <$message>

Apologies for asking so much - i have tried adjusting but im getting error after error. Any help would be v.appreciated!

Hi Stephen,

Change the code where it says:


$mail = mail(WEBMASTER_EMAIL, $message,
     "From: ".$name." <".$email.">\\r\
"
    ."Reply-To: ".$email."\\r\
"
    ."Message: PHP/" . phpversion());

to:


$from_email = "noreply@domain.net";
$subject = "Query from website from $name";
$msg_body = "Website query sent at:".date("Y-m-d H:i:s")."\\r\
"
          . "From: $name - $email\\r\
"
          . "Message: $message";

$mail = mail($from_email, $subject, $msg_body);

Cheers fretburner - i’ll try on the live server now. The only issue now is the WEBMASTER_EMAIL which needs to be my mail (its in the config.php - “define(“WEBMASTER_EMAIL”, ‘my mail address’);”) so that it ends up in my mailbox.

Perhaps something like; $mail = mail(WEBMASTER_EMAIL, $from_email, $subject, $msg_body);

Nope tried and it moves the order down - what I need is for the mail to be sent to my@domain.com and the from field show noreply@domain.com

Hi Stephen,

Sorry, ignore my previous post, what you actually need is something like this:


$headers =  "From: noreply@domain.net\\r\
"
    ."Reply-To: $email\\r\
"
    ."Message: PHP/" . phpversion());

$subject = "Query from website from $name";

$msg_body = "Website query sent at:".date("Y-m-d H:i:s")."\\r\
"
          . "From: $name - $email\\r\
"
          . "Message: $message";

$mail = mail(WEBMASTER_EMAIL, $subject, $msg_body, $headers);

If you define WEBMASTER_EMAIL to be the address you want to receive the emails at, then you should be fine.

Okay somtheing like this?


<?php
/**
Credits: Bit Repository
URL: http://www.bitrepository.com/
*/

require dirname(dirname(__FILE__)).'/includes/config.php';

//error_reporting (E_ALL ^ E_NOTICE);

$post = (!empty($_POST)) ? true : false;

if ($post) {
    include 'functions.php';
    $name = stripslashes($_POST['name']);
    $email = trim($_POST['email']);
    //$subject = stripslashes($_POST['subject']);
    $message = stripslashes($_POST['message']);
    
    $error = '';

    // Check name
    
    if (!$name) {
        $error .= 'Please enter your name.<br />';
    }

    // Check email
    
    if (!$email) {
        $error .= 'Please enter an e-mail address.<br />';
    }
    
    if ($email && !ValidateEmail($email)) {
        $error .= 'Please enter a valid e-mail address.<br />';
    }

    // Check message (length 15 char or more to valid)

    if (!$message || strlen($message) < 15) {
        $error .= "Please enter your message. It should have at least 15 characters.<br />";
    }
    
    if (!$error) {
$headers =  "From: noreply@hscni.net\\r\
" 
    ."Reply-To: $email\\r\
" 
    ."Message: PHP/" . phpversion());  
    
$subject = "Query from website from $name"; 

$msg_body = "Website query sent at:".date("Y-m-d H:i:s")."\\r\
" 
          . "From: $name - $email\\r\
" 
          . "Message: $message"; 
         
$mail = mail(WEBMASTER_EMAIL, $subject, $msg_body, $headers);  

        );

        if ($mail) {
            echo 'OK';
        }
    }

    else{
        echo '<div class="notification_error">'.$error.'</div>';
    }
}
?>

Yeah, that looks about right.

Okay couple of Parse errors: syntax errors, unexpected ‘)’ removed - i’ll test on live server and report back - thanks so far so good : )

Still getting Parse error: syntax errors… but it did work at some point got a mail in my junk folder today???

I’ve just had another look at the code you posted above… there are a couple of things I noticed:

$headers =  "From: noreply@hscni.net\\r\
"
    ."Reply-To: $email\\r\
"
    ."Message: PHP/" . phpversion()); // the extra right parenthesis shouldn't be there
$mail = mail(WEBMASTER_EMAIL, $subject, $msg_body, $headers);

        ); // remove this right parenthesis and semicolon

Thanks fretburner i kept replacing the 2nd parenthesis and semicolon with a closing bracket. Cant see any errors now. Will test live over the weekend. Thanks again your help is much appreciated!