如何创建向目标电子邮件发送邮件的电子邮件表单?

时间:2015-10-19 09:16:02

标签: php html forms email

所以我正在尝试为我的联系我们页面创建一个电子邮件表单。我的问题是我不知道这封电子邮件是如何为我的表格提供此代码的 如下图所示:

<form method = "post" action = "contactus.php" enctype="multipart/form-data">
<div class = "emailform">
    <input type = "text" placeholder = "Name" name = "name" style="width:450px; padding:5px;" required><br>
    <input type = "text" placeholder = "Email" name = "email" style="width:450px; padding:5px;" required><br>
    <textarea name = "message" placeholder = "Message" style = "width:450px; padding: 5px; height: 155px;" required></textarea><br>
    <button type = "submit" name = "send">Send</button>
</div>
</form> 
如果有人回答,那将会非常有帮助 顺便说一句,我只使用记事本++编辑我的代码。 而我也是一个新手:)

提前谢谢

2 个答案:

答案 0 :(得分:0)

在你的contactus.php页面试试这个。

<?php


$to = "recieveraddress@mail.com";
$subject = "Your subject";
$txt = $_POST['message'];
$headers = "From: ".$_POST['email'];

mail($to,$subject,$txt,$headers);
?> 

答案 1 :(得分:0)

在“contactus.php”文件中添加以下代码。将“your@email.com”替换为您的电子邮件地址。请将“contactus.php”放在放置表单文件的同一目录中。

<?php
function clean($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
if(isset($_POST['name'],$_POST['email'],$_POST['message']) && $_POST['name'] != '' && $_POST['email'] != '' && $_POST['message'] != '') {
    foreach($_POST as $key = > $value)
        $_POST[$key] = clean($value);

    $to = 'your@email.com';
    $from = $_POST['email'];
    $subject = 'Customer support needed';
    $message = 'Name='.$_POST['name'].'  Message='.$_POST['message']; 


    // Sending email
    if(mail($to, $subject, $message)){
        echo 'Your mail has been sent successfully.';
    } else{
        echo 'Unable to send email. Please try again.';
    }
}
?>