1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

@@@.................Help with php mail()........................@@@

Discussion in 'General Programming Chat' started by rachidspot, Apr 4, 2016.

  1. rachidspot

    rachidspot Newbie

    Joined:
    Nov 22, 2015
    Messages:
    6
    Likes Received:
    0
    Hi Everybody

    Please I want to know how can I specify header parameters using mail() function

    I want to set and usemy own email header, plz tell me how can I do it? like specify "From Name", "Mail From", "Reply-To", "Returnpath", etc..

    now I can set just: to, message, subject

    Thank a lot
     
  2. b1acky

    b1acky Newbie

    Joined:
    Jun 17, 2015
    Messages:
    47
    Likes Received:
    12
    Directly from php documentation:

    Code:
    bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
    
    Code:
    <?php
    $to      = 'emailHere';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: emailHere' . "\r\n" .
        'Reply-To: emailHere' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    
    
    mail($to, $subject, $message, $headers);
    ?>
    
    Edit: Don't forget to replace 'emailHere' with correct informations as I can't post emails nor urls yet.
     
    • Thanks Thanks x 1
  3. rachidspot

    rachidspot Newbie

    Joined:
    Nov 22, 2015
    Messages:
    6
    Likes Received:
    0
    Thank you b1acky

    plz other question, how can I set the From input as i want ? I use this code but not works:

    $from=$_POST['from'];


    $headers .= 'From: $from' . "\r\n";
     
  4. AdvancedDevelopment

    AdvancedDevelopment BANNED BANNED

    Joined:
    Mar 23, 2016
    Messages:
    91
    Likes Received:
    28
    Can you post the code you have for you form? Your maybe processing the form as GET rather than POST. I cant really tell without seeing your code but try this and see if this works instead

    Code:
    $from=$_GET['from'];
    
     
  5. ChanzGrande

    ChanzGrande Elite Member

    Joined:
    Feb 16, 2008
    Messages:
    2,484
    Likes Received:
    1,172
    Occupation:
    Accountant
    Location:
    Northern Woods Counting Money
    I would stick with the $_POST function assuming that's how you are setting the data via form input. In any event, I think you need to have the $_POST use a correct input element, so try:

    Code:
    $from=$_POST['From']
    
    As it appears in the header you set it with data of "From:"

    I don't think the $_POST function is currently finding anything matching 'from', but it should match 'From'. Admittedly it's a guess.
     
  6. rachidspot

    rachidspot Newbie

    Joined:
    Nov 22, 2015
    Messages:
    6
    Likes Received:
    0
    Thank you for your reply

    This is the code:

    <?php



    $to=$_POST['to'];
    $subject=$_POST['subject'];
    $message=$_POST['message'];

    $From=$_POST['From'];


    $headers .= 'From: $From' . "\r\n";

    $headers .= "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=ISO-8859-1" . "\r\n";
    $headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
    $headers .= 'Reply-To: mailhere' . "\r\n";



    if($_POST['submit']) {

    mail($to,$subject,$message,$headers);

    echo "message sent";
    }



    ?>


    <form action="<?php echo $PHP_SELF; ?>" method="post">

    <input type="text" name="from" alt="from" value="from"/>
    <input type="text" name="subject" alt="subject" value="subject"/> <br /> <br />
    <input type="text" name="to" alt="to" value="to"/>
    <p>Body:</p> <textarea name="message" cols="40" rows="20"></textarea> <br />
    <input type="submit" name="submit" value="send" />

    </form>

    -----------------------------------------------------------------------------------------------
    I want to specify the From Name as I put at the input, like Subject and to

    Thank you
     
  7. b1acky

    b1acky Newbie

    Joined:
    Jun 17, 2015
    Messages:
    47
    Likes Received:
    12
    First of all, you should always filter form inputs!!! ;) I don't really have time now to correct everything but this should be pretty close:

    Code:
    <?php
        // you should check if request method is POST
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            
            $to = filter_input(INPUT_POST, 'to');
            $subject= filter_input(INPUT_POST, 'subject');
            $message= filter_input(INPUT_POST, 'message');
            $from= filter_input(INPUT_POST, 'from');
            
            echo "To: $to, Subject: $subject, Message: $message, From: $from<br>";
    
    
            $headers = "From: $from" . "\r\n";
            $headers .= "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=ISO-8859-1" . "\r\n";
            $headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
            $headers .= "Reply-To: $from" . "\r\n";
    
    
            mail($to,$subject,$message,$headers);
        }
    ?>
    
    
    
    
    <!DOCTYPE HTML>
    <html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form action="" method="post">
            <input type="text" name="from" alt="from" value="from"/>
            <input type="text" name="subject" alt="subject" value="subject"/> <br /> <br />
            <input type="text" name="to" alt="to" value="to"/>
            <p>Body:</p> <textarea name="message" cols="40" rows="20"></textarea> <br />
            <input type="submit" name="submit" value="send" />
        </form>
    </body>
    </html>
    
    Edit: Short explanation:
    1. We check what method was used to submit the form, if method = POST then we process form data.
    2. We filter data from our inputs (you should actually pass correct filter for specific datatypes but we can omit it for now.
    3. We build $headers variable to pass to mail function.

    Your errors:
    - wrong method check - you should not check if $_POST['submit'] exists, submit type input is used just for submitting the form
    - you try to append new content to $headers variable while it was not declared
    - variable $PHP_SELF does not exist
    - you pass variables to string while using ' and it won't work unless you use "
    - you do not stick to one naming strategy - which does not matter now but it is just a good practice to choose one and use it (camelCase naming or underscores or whatever you like)
     
    • Thanks Thanks x 1
    Last edited: Apr 13, 2016
  8. rachidspot

    rachidspot Newbie

    Joined:
    Nov 22, 2015
    Messages:
    6
    Likes Received:
    0
    Yes it's work now

    Plz I want also add Return-path, and a text area about Header format, please give me the right code to add a text area in wich I can modify and parameter all heders input that I had put