Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php从网站发送邮件_Php_Email - Fatal编程技术网

使用php从网站发送邮件

使用php从网站发送邮件,php,email,Php,Email,我是php的初学者。我有一个任务是从网站发送邮件,请帮助我。我正在附加我尝试过的html和php代码。这完全行不通。 我面临的问题是,它永远无法显示邮件来自何处。而不是显示主机名。 要求: 附属的: 电子邮件id: 信息 HTML代码 <form class="form-horizontal" method="post" name="myemailform" action="form-to-email.php" > <div class="row">

我是php的初学者。我有一个任务是从网站发送邮件,请帮助我。我正在附加我尝试过的html和php代码。这完全行不通。 我面临的问题是,它永远无法显示邮件来自何处。而不是显示主机名。 要求: 附属的: 电子邮件id:

信息

HTML代码

<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;
    }
}

在邮件功能中使用自定义
标题。检查以下邮件功能代码:

<?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);
您必须将“发件人”地址设置为邮件中的附加标题。看


您是否使用复制粘贴的代码?或者你写过这个吗?在mail()调用中,你从未添加过
$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);