Vandana k2012
Vandana k2012

Reputation: 29

Mail sending from a website using php

Iam a begniner in php. I got a task that is sending mail from website, Please help me. iam attaching html and php code that i tried. That is not working exactly. The problem iam facing is it never showing from where the mail is coming. instead of it it showing the host name. Requirement: Sub : email id:

message

HTML CODE

<form class="form-horizontal" method="post" name="myemailform" action="form-to-email.php" >
    <div class="row">
        <div class="col-lg-6">
            <div class="form-group" style="padding-right:10px;">
                <label>Name</label>
                <input type="text" class="form-control" id="name" name="name" placeholder="NAME..." value="">                                    
            </div>
        </div>
        <div class="col-lg-6">
            <div class="form-group">
                <label for="exampleInputEmail1">Email</label>
                    <input type="email" class="form-control" id="email" name="email" placeholder="EMAIL..." value="">      
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-6">
            <div class="form-group" style="padding-right:10px;">
                <label>Telephone</label>
                    <input type="email" class="form-control" id="telephone">
            </div>
        </div>
        <div class="col-lg-6">
            <div class="form-group">
                <label>Mobile</label>
                <input type="email" class="form-control" id="mobile">
            </div>
        </div>
    </div>
    <div class="row">
       <div class="col-lg-12">
            <div class="form-group">
                <label>Message</label>
                <textarea class="form-control" rows="4" id="message" name="message" placeholder="MESSAGE..."></textarea>
            </div>
       </div>
   </div>
   <button type="submit" class="btn btn-default sub">Send</button>
</form>

PHP CODE

<?php
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$telephone = $_POST['telephone'];
$mobile =  $_POST['mobile'];
$subject =  $_POST['subject'];

//Validate first
if(empty($name)||empty($visitor_email)||empty($message)) 
    {
        echo "Name,email  and message are mandatory!";
        exit;
    }

if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}

$email_from = $visitor_email;//<== update the email address
$email_body = "Name:\t $name.\n".
              "Email:\t $vi\nsitor_email.\n".
              "Telephone:\t $telephone.\n".
              "Mobile:\t $mobile.\n".
              "Message:\t $message.\n".

$to = "[email protected]";//<== update the email address
//Send the email!
mail($to,$subject,$email_body);
//done. redirect to thank-you page.
header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
    $injections = array('(\n+)',
          '(\r+)',
          '(\t+)',
          '(%0A+)',
          '(%0D+)',
          '(%08+)',
          '(%09+)'
          );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str))
    {
        return true;
    }
    else
    {
        return false;
    }
}

?>

Upvotes: 0

Views: 90

Answers (2)

user1915746
user1915746

Reputation: 587

You have to set the "from" address as additional headers in mail. See http://php.net/manual/en/function.mail.php

$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

Upvotes: 1

Bhunesh  Satpada
Bhunesh Satpada

Reputation: 810

Use custom header in mail function.Check below mail function code:

<?php
$to = "[email protected], [email protected]";
$subject = "HTML email";

$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

$headers .= 'From: <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";

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

https://www.w3schools.com/php/func_mail_mail.asp

Upvotes: 0

Related Questions