需要从HTML页面发送电子邮件

时间:2009-11-30 10:17:54

标签: javascript html

我创建一个网站作为静态HTML页面。仅在一个联系人页面中,我需要获取用户名和emailId。应使用username和emailId的信息将此信息发送到特定的邮件ID。

我只使用HTML和Javascript,任何人都可以说我如何使它成为可能。

5 个答案:

答案 0 :(得分:7)

没有任何后端内容,您唯一的选择就是在href中使用mailto。这取决于用户自己发送电子邮件。您可以使用javascript来填充电子邮件.e.g

"mailto:"+emailTo+"&subject="+subjectText+"&body="+bodyText

答案 1 :(得分:2)

您可以使用以下html发送自己的表单数据:

<form method="post" action="mailto:me@my.com?subject=Results">

    <label for="Name">Name:</label><input type="text" name="Name"><br />
    <label for="Email">Email:</label><input type="text" name="Email"><br />
    <input type="submit">
    </form>

点击“提交”将使用默认邮件客户端创建新的电子邮件,并使用表单数据填充主题Results和正文

Name=PJP&Email=me%40my.com 

注意数据是如何url encoded的。例如%40符号为@

用户必须按发送才能发送信息。

在我的旧虚拟主机上发现cgi-bin sendMail脚本之前,我曾经在大约15年前做过类似的事情。

答案 2 :(得分:1)

你有什么理由不能使用服务器端代码吗?这种事情很容易用PHP来完成。否则,唯一的选择是已经指出的<a href="mailto ..

答案 3 :(得分:0)

如果你真的不能使用PHP,你可以使用formmail.cgi,如果你的主机提供一个。大多数主机支持此功能,instructions for using FormMail很简单。

如果你使用PHP,这是微不足道的。您可以尝试使用.php扩展名(而不是.htm或.html)重命名该页面,并将以下代码放入您的页面:

        <div class="post">
        <h2 class="title">Write to us</h2>
        <?php
        function validEmail($email)
        {
            $isValid = true;
            $atIndex = strrpos($email, "@");
            if (is_bool($atIndex) && !$atIndex)
            {
                $isValid = false;
            }
            else
            {
                $domain = substr($email, $atIndex+1);
                $local = substr($email, 0, $atIndex);
                $localLen = strlen($local);
                $domainLen = strlen($domain);
                if ($localLen < 1 || $localLen > 64)
                {
                    // local part length exceeded
                    $isValid = false;
                }
                else if ($domainLen < 1 || $domainLen > 255)
                {
                    // domain part length exceeded
                    $isValid = false;
                }
                else if ($local[0] == '.' || $local[$localLen-1] == '.')
                {
                    // local part starts or ends with '.'
                    $isValid = false;
                }
                else if (preg_match('/\\.\\./', $local))
                {
                    // local part has two consecutive dots
                    $isValid = false;
                }
                else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
                {
                    // character not valid in domain part
                    $isValid = false;
                }
                else if (preg_match('/\\.\\./', $domain))
                {
                    // domain part has two consecutive dots
                    $isValid = false;
                }
                else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                            str_replace("\\\\","",$local)))
                {
                    // character not valid in local part unless 
                    // local part is quoted
                    if (!preg_match('/^"(\\\\"|[^"])+"$/',
                        str_replace("\\\\","",$local)))
                    {
                    $isValid = false;
                    }
                }
                if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
                {
                    // domain not found in DNS
                    $isValid = false;
                }
            }
            return $isValid;
        }


        if (isset($_REQUEST['email']))
          {//if "email" is filled out, proceed

          //check if the email address is invalid
          $mailcheck = validEmail($_REQUEST['email']);
          if ($mailcheck==FALSE)
            {
            echo "<p>Invalid e-mail address.</p>";
            }
          else {
            //send email
            $name = $_REQUEST['name'] ;
            $email = $_REQUEST['email'] ;
            $message = $_REQUEST['message'] ;
            mail("recepient@example.com", "Subject: Message from contact form",
            $message, 'From: "' . $name . '" <' . $email . '>' );
            echo "<p>Thank you for writing to our website. Please allow up to 24 hours for a reply, if you have requested one.</p>";
            }
          }
        else {
          //if "email" is not filled out, display the form
          echo "<form method='post' action='contact.php'>
          Name: <input id='name' name='name' type='text' /><br />
          E-mail: <input id='email' name='email' type='text' /> (required)<br />
          Message for us:<br />
          <textarea id='message' name='message' rows='15' cols='40'>
          </textarea><br />
          <input id='submit' type='submit' value='Send Message' />
          </form>";
          }
        ?>
    </div>

答案 4 :(得分:0)

参考 - https://medium.com/design-startups/b53319616782

您只能使用JavaScript发送电子邮件。没有涉及服务器端语言。

相关问题