为什么电子邮件发送功能不起作用?

时间:2018-10-12 08:15:27

标签: javascript php html email

当某人单击我网站的下载链接时,我想发送电子邮件。我正在使用PHP和JavaScript。以下两个片段位于同一文件中。

HTML

<form method="post">
  <div class="form-group">
      <input type="email" class="form-control" placeholder="Enter Email ID" id="email" onchange="check();" require pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$"/>
  </div>
  <div class="form-group">
    <label style="font-size:14px;"><input type="checkbox" id="toggle"> I agree to receive emails from MarketerBoard. We will not share your any personal information with anyone.</label>
  </div>
  <div class="form-group" style="min-height:40px;">
      <a style="display:none;" href="/images/docs/EMAIL-MARKETING-GUIDE.pdf" name="downloadBtn" download class="mb-button" id="dwn-btn" onchange="check();" onClick="sendmail();">Get your free guide</a>
  </div>
</form>

JavaScript

if(isset($_POST['downloadBtn']))
{
    $to      = 'examle@example.com';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: example@example.com' . "\r\n" .
        'Reply-To: example@example.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

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


   //echo 'Email Sent.';
 }

我通过使用以下代码来验证用户的电子邮件ID。我想通过单击链接来发送电子邮件,这似乎不起作用。

 <script type="text/javascript">

    function check() {     
        document.getElementById("toggle").checked = false;
        var email_x = document.getElementById("email").value;
        filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (filter.test(email.value)) {
            document.getElementById("email").style.border = "2px solid teal";
            var userEmail = document.getElementById('email').value
            $('#toggle').click(function () {
            //check if checkbox is checked
            if ($(this).is(':checked') & filter.test(email.value)) {
                 $("#dwn-btn").show(); } 
                else {
                $("#dwn-btn").hide();}});
                return true;
        } else {
            document.getElementById("email").style.border = "2px solid red";
            $("#dwn-btn").hide();
            return false;
        }
    }

</script>

1 个答案:

答案 0 :(得分:0)

首先检查/更改您的PHP邮件配置:

  1. 打开您的php.ini文件
  2. 搜索显示为[邮件功能]的行
  3. 添加/更改邮件服务器的详细信息。这可能是本地邮件服务器,也可能是您的ISP的邮件服务器。
  4. 保存/关闭php.ini文件
  5. 重新启动您的Web服务器

下面的示例代码:

<form action="" method="post">
    <input type="submit" value="Send email" />
    <input type="hidden" name="send_mail" value="1" />
</form>

<?php

if(isset($_POST['send_mail']))
{
    $to      = 'nobody@example.com';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: nobody@example.com' . "\r\n" .
        'Reply-To: nobody@example.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

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

    echo 'Email Sent.';
}

?>