1

html markup for the form:

<form method="post" action="send_form_email.php">
                        <div class="form-group">
                            <label>Name: </label>
                            <input class="form-control" type="text" placeholder="Enter Name" name="name">
                        </div>
                        <div class="form-group">
                            <label>Email: </label>
                            <input class="form-control" type="text" placeholder="Enter Email" name="email">
                        </div>
                        <div class="form-group">
                            <label>Message: </label>
                            <textarea class="form-control" placeholder="Enter Message" name="message"></textarea>
                        </div>
                        <button type="submit" name="sended" class="btn btn-default">Submit</button>
                    </form>

php file is as follows including 2 arrays to make the swop between variables:

<?php
    if(!isset($_POST['sended'])) {
        died("Йуху");
    }
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $mastermail="[email protected]";
    $html = file_get_contents("./email-inlined.html");
// Массивы с заменой
    $search = array("#name#", "#email#", "#message#");
    $replace = array($name, $email, $message);
    $email_message = str_replace($search, $replace, $html); 
// create email headers
    $headers = 'From: '.$masteremail."\r\n".
    'Reply-To:'.$masteremail."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    $headers .= 'MIME-Version: 1.0'."\r\n";
    $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";   
    mail($mastermail, "письмо из Сакуры", $html, $headers);  
    ?>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <h1>Thanks!</h1>

and the mail itself to be proceeded to the recepient:

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Письмо с сайта</title>
</head>

<body>
    <div>
        <p>letter comes from <span>#name#</span></p>
        <p>email to reply <span>#email#</span></p>
        <p>message itself:<br><span>#message#</span></p>
    </div>
</body>
</html>

the problem is that snippets are not changed with the data that comes from the form. Means that html data is not transmitted to php file.

Thanks!

4
  • 1
    You are using $html in your mail() call instead of $email_message (where you did all the replacing). Commented Sep 14, 2017 at 22:33
  • @ccKep but I did $html = file_get_contents("./email-inlined.html"); means that now $html stores data from the file I wanna send, am I correct? Commented Sep 14, 2017 at 22:35
  • 1
    $html = file_get_contents("./email-inlined.html"); means "get the contents of that file and store it in $html". After that, you're doing your str_replace on that and store those results in $email_message, hence: $email_message are the contents of your email with the placeholders replaced, $html is the original one. When you send the email you want to send the one with the replaced values ofcourse, hence $email_message instead of $html. Change mail($mastermail, "письмо из Сакуры", $html, $headers); to mail($mastermail, "письмо из Сакуры", $email_message, $headers);. Commented Sep 14, 2017 at 22:36
  • @ccKep got it, thanks! Commented Sep 14, 2017 at 22:38

1 Answer 1

1

The problem lies with what you are passing to the mail function. After storing the file_contents in $html, the replace you did is stored in $email_message but you still pass $html to the mail.

Change it to

mail($mastermail, "письмо из Сакуры", $email_message, $headers); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.