使用php从网站发送的邮件

时间:2017-06-26 12:27:32

标签: php email

我是php的begniner。我有一个从网站发送邮件的任务,请帮帮我。我附加了我试过的html和php代码。这不完全正常。 我面临的问题是它永远不会显示邮件的来源。而不是它显示主机名。 需求: 子: 电子邮件ID:

消息

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代码

<?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 = "vandanak2012@gmail.com";//<== 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;
    }
}

&GT?;

2 个答案:

答案 0 :(得分:1)

您必须将“发件人”地址设置为邮件中的其他标题。见http://php.net/manual/en/function.mail.php

$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

答案 1 :(得分:0)

在邮件功能中使用自定义header。检查邮件功能代码:

<?php
$to = "somebody@example.com, somebodyelse@example.com";
$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: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

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

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