使用用户在表单中提供的数据发送电子邮件?

时间:2013-11-28 20:15:41

标签: php email

我有一个联系表格,我希望对其进行修改,以便在用户点击提交时向用户发送确认电子邮件。有人可以告诉我最好的方法吗?

我的php非常简单:

 // validation complete 

if(isset($_POST['submit'])){

if($msg_name=="" && $msg2_name=="" && $msg_email=="" && $msg2_email=="" && 
$msg2_Message=="")

$msg_success = "Thankyou for your enquiry";

//send mail   

    $EmailFrom = "someone@somewhere.co.uk";

    $EmailTo = "someone@somewhere.co.uk";

    $Subject = "Online contact form";

    $full_name = Trim(stripslashes($_POST['full_name'])); 

    $Phone_Num = Trim(stripslashes($_POST['Phone_Num'])); 

    $email_addr = Trim(stripslashes($_POST['email_addr'])); 

} 

    // prepare email body text

    $Body = "";

    $Body .= "full_name: ";

    $Body .= $full_name;

    $Body .= "\n";

    $Body .= "Phone_Num: ";

    $Body .= $Phone_Num;

    $Body .= "\n";

    $Body .= "email_addr: ";

    $Body .= $email_addr;

    $Body .= "\n";


    // send email 

    $success = mail($EmailTo, $Subject, $Body, "From: $EmailFrom");

    ?>

2 个答案:

答案 0 :(得分:0)

首先,我们将mailto函数与localhost和电子邮件客户端一起使用:
 在stackoverflow上检查此链接:
网址链接:send email to owner by collecting information provided by user through form?

然后我推荐使用Swiftmailer。 http://swiftmailer.org/docs/sending.html他们获得了最好的手册。

答案 1 :(得分:0)

正如其他人已经建议“if”声明没用,因为它什么都没有。我认为您的想法是验证您的联系表单中的字段是否已填写。如果字段不可用,您应该抛出一些您没有做的错误。

此外,如果您要向点击提交按钮的用户发送确认邮件,那么$ EmailTo变量应该接收来自$ msg_email或$ msg2_email的电子邮件,根据此处未执行此操作。

选中这个可能对您有帮助的简单代码:

if ($_POST['submit']) {
  if ($_POST['name1] == '')
    echo "Please provide the first name";
  if ($_POST['name2] == '')
    echo "Please provide the last name";
  if ($_POST['email1] == '')
    echo "Please provide the email";        
  if ($_POST['email2] == '')
    echo "Please provide the alternate email";
  if ($_POST['message] == '')
    echo "Please provide the Message";

//Send email to your inquiry mail with above details

// Confirmation mail

    $message =<<<EOM
    Dear $_POST['name1'],
    Thank you for your inquiry. 
    Our executives will get back to you ASAP.

    Thanks, 
        Sales
    EOM;    

    $to = $_POST['email1'];
    $subject = "Acknowledgement of Inquiry";
    $headers = "Content-Type: text-plain";
    $headers .= "From: sales@yourcompany.co.uk";

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

}