SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2012
    Location
    N.Ireland
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 Code:
    <?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\n"
        
    ."Reply-To: ".$email."\r\n"
        
    ."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!

  2. #2
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,257
    Mentioned
    32 Post(s)
    Tagged
    5 Thread(s)
    Hi Stephen,

    Change the code where it says:
    PHP Code:
    $mail mail(WEBMASTER_EMAIL$message,
         
    "From: ".$name." <".$email.">\r\n"
        
    ."Reply-To: ".$email."\r\n"
        
    ."Message: PHP/" phpversion()); 
    to:
    PHP Code:
    $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\n"
              
    "From: $name - $email\r\n"
              
    "Message: $message";
            
    $mail mail($from_email$subject$msg_body); 

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2012
    Location
    N.Ireland
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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...

  4. #4
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,257
    Mentioned
    32 Post(s)
    Tagged
    5 Thread(s)
    Hi Stephen,

    Sorry, ignore my previous post, what you actually need is something like this:
    PHP Code:
    $headers =  "From: noreply@domain.net\r\n" 
        
    ."Reply-To: $email\r\n" 
        
    ."Message: PHP/" phpversion());  
        
    $subject "Query from website from $name"

    $msg_body "Website query sent at:".date("Y-m-d H:i:s")."\r\n" 
              
    "From: $name - $email\r\n" 
              
    "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.

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2012
    Location
    N.Ireland
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay somtheing like this?

    Code:
    <?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\n" 
        ."Reply-To: $email\r\n" 
        ."Message: PHP/" . phpversion());  
        
    $subject = "Query from website from $name"; 
    
    $msg_body = "Website query sent at:".date("Y-m-d H:i:s")."\r\n" 
              . "From: $name - $email\r\n" 
              . "Message: $message"; 
             
    $mail = mail(WEBMASTER_EMAIL, $subject, $msg_body, $headers);  
    
            );
    
            if ($mail) {
                echo 'OK';
            }
        }
    
        else{
            echo '<div class="notification_error">'.$error.'</div>';
        }
    }
    ?>

  6. #6
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,257
    Mentioned
    32 Post(s)
    Tagged
    5 Thread(s)
    Yeah, that looks about right.

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2012
    Location
    N.Ireland
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay couple of Parse errors: syntax errors, unexpected ')' removed - i'll test on live server and report back - thanks so far so good : )

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2012
    Location
    N.Ireland
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still getting Parse error: syntax errors.... but it did work at some point got a mail in my junk folder today????

  9. #9
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,257
    Mentioned
    32 Post(s)
    Tagged
    5 Thread(s)
    I've just had another look at the code you posted above.. there are a couple of things I noticed:
    PHP Code:
    $headers =  "From: noreply@hscni.net\r\n" 
        
    ."Reply-To: $email\r\n" 
        
    ."Message: PHP/" phpversion()); // the extra right parenthesis shouldn't be there 
    PHP Code:
    $mail mail(WEBMASTER_EMAIL$subject$msg_body$headers);  

            ); 
    // remove this right parenthesis and semicolon 

  10. #10
    SitePoint Enthusiast
    Join Date
    May 2012
    Location
    N.Ireland
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •