【问题标题】:HTML Contact Form Send Mail Through PHPHTML 联系表单通过 PHP 发送邮件
【发布时间】:2013-10-20 16:28:35
【问题描述】:

我有这个 html 中的联系表单代码:

 <form method=post action=sendmail.php>
    <div class=one_third>
      <label>Name</label>
      <input type=text name=author value id=name />
    </div>
    <div class=one_third>
      <label>Email</label>
      <input type=text name=email value id=email />
    </div>
    <div class="one_third last">
      <label>Subject</label>
      <input type=text name=subject value id=subject />
    </div>
    <div class=full_width>
      <label>Your Message</label>
      <textarea name=msg id=msg></textarea>
    </div>
    <input type=submit name=submit value=Submit />
  </form>

我想知道你是否可以提供一个 php 代码,进入“sendmail.php”,它会实际发送电子邮件。

【问题讨论】:

标签: html forms email


【解决方案1】:

首先,您应该在发布问题之前尝试寻找答案,这几乎就是谷歌搜索“php send mail”的第一个结果将显示给您的内容:

<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
    //send email
    $from = $_REQUEST['author'] ;
    $to = $_REQUEST['email'] ;
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['msg'] ;
    mail($to, $subject, $message, "From:" . $from);

    // the mail was sent
    echo "Thank you for using our mail form";
}
else {
    //if "email" is not filled out, display the form
    //just close php and copy the code for your form
?>
- paste your html form here -
<?php
}
?>

第二件事是,我不知道你想用 author 字段做什么。我怀疑您从未发送过电子邮件,您必须在任何输入字段中输入您的身份。客户端会为您做到这一点。因此,考虑到这一点,我只是在消息底部留下了 author

这一切都为您在 php.ini 配置设置中设置了一个有效的电子邮件系统。

【讨论】:

  • 我的意思是单击提交按钮时要执行的外部代码,它将检查不同 html 文件中的所有字段,然后发送电子邮件。
  • 抱歉,还是没听懂。哪些外部代码,您打算如何使用提交按钮调用它以及您需要哪些其他文件?如果您想检查字段是否为空,您可以级联每个字段的 >>if (isset
【解决方案2】:

如果您有任何问题,请告诉我,这将与您在上面发布的表单一起使用。

<?php

/* Subject and email variables */

$emailsSubject = 'This is where you type what you subject will show up as';
$webMaster  = 'youremail@gmail.com';


/* Gathering Data Variables - Whats in the form */

$name = $_POST ['name'];
$email = $_POST ['email'];
$subject = $_POST ['subject'];
$msg = $_POST['msg'];


/*Security*/




/* What You Want To See In The Email Place Inbetween $body = <<<EOD  and EOD; */    
$body = <<<EOD

<strong>Client:</strong> $name
<br />
<br />
<strong>Email:</strong> $email
<br />
<br />
<strong>Subject:</strong> $subject
<br />
<br />
______________________________________________
<br />
<br />
$msg

EOD;

/* Headers is a tag containing the users email and how you want it to display in your email */

$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";

/* This is what sends the email */
$success = mail($webMaster, $emailsSubject, $body, $headers);

/* Results Rendered as Html */
echo file_get_contents("http://yourdomain.com/after-message-sent/");

?>

对于“echo file_get_contents”,您可以创建一个您希望客户看到的页面,然后告诉他们消息已发送。如果你想要简单的简,那么只需回显你的消息已发送。希望这可以帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多