简体   繁体   English

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

[英]Need to Send Email from HTML pages

I m creating a Website as Static HTML pages. 我创建一个网站作为静态HTML页面。 In that only in one contacts page alone i need to get the users name and emailId . 仅在一个联系人页面中,我需要获取用户名和emailId。 This information should be send to a particular mail Id with the information of username and emailId. 应将此信息发送到具有username和emailId信息的特定邮件ID。

I m using only HTML and Javascript , can anyone say me how to make it possible. 我只使用HTML和Javascript,任何人都可以说我如何使它成为可能。

Without any backend stuff your only option is using a mailto in an href. 没有任何后端的东西你唯一的选择是在href中使用mailto。 This relies on the user sending the email themselves. 这取决于用户自己发送电子邮件。 You might be able to do something with javascript to populated the email .eg 您可以使用javascript来填充电子邮件.eg

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

You can send yourself the form data by using the following html: 您可以使用以下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> 

Clicking on submit will create a new email message with the default mail client and will populate the subject Results and the body with the form data which will look like this 单击“提交”将使用默认邮件客户端创建新的电子邮件,并使用表单数据填充主题“ Results和正文,如下所示

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

Note how the data has been url encoded . 请注意数据是如何进行网址编码的 eg %40 for the @ sign. 例如@符号为%40

The user will have to press Send to send the message. 用户必须按发送才能发送消息。

I used to do something similar to this around 15 years ago before I discovered a cgi-bin sendMail script on my old webhost. 大约15年前我曾经做过类似的事情,之后我在旧的虚拟主机上发现了一个cgi-bin sendMail脚本。

Any reason you can't use server-side code? 你有什么理由不能使用服务器端代码吗? This kind of thing is very easily doable with PHP. 这种事情很容易用PHP来完成。 Otherwise, the only choice is the <a href="mailto .. as pointed out already. 否则,唯一的选择是已经指出的<a href="mailto ..

If you really can't use PHP, you can use the formmail.cgi if your host provides one. 如果你真的不能使用PHP,你可以使用formmail.cgi,如果你的主机提供一个。 Most hosts support this, and instructions for using FormMail are simple. 大多数主机支持此功能, 使用FormMail的说明很简单。

This is trivial if you are using PHP though. 如果你使用PHP,这是微不足道的。 You can try to rename the page with a .php extension (instead of .htm or .html) and put the following code into your page: 您可以尝试使用.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>

Refer - https://medium.com/design-startups/b53319616782 请参阅 - https://medium.com/design-startups/b53319616782

You can Send an email using only JavaScript. 您只能使用JavaScript发送电子邮件。 No server-side languages involved. 不涉及服务器端语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM