Fairy_G
Fairy_G

Reputation: 417

Need to Send Email from HTML pages

I m creating a Website as Static HTML pages. In that only in one contacts page alone i need to get the users name and emailId . This information should be send to a particular mail Id with the information of username and emailId.

I m using only HTML and Javascript , can anyone say me how to make it possible.

Upvotes: 3

Views: 17861

Answers (5)

pjp
pjp

Reputation: 17629

You can send yourself the form data by using the following html:

<form method="post" action="mailto:[email protected]?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

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

Note how the data has been url encoded. e.g. %40 for the @ sign.

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.

Upvotes: 2

Howard Wolowitz
Howard Wolowitz

Reputation: 1

Refer - https://medium.com/design-startups/b53319616782

You can Send an email using only JavaScript. No server-side languages involved.

Upvotes: 0

matpol
matpol

Reputation: 3074

Without any backend stuff your only option is using a mailto in an href. This relies on the user sending the email themselves. You might be able to do something with javascript to populated the email .e.g

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

Upvotes: 7

Pranab
Pranab

Reputation: 2787

If you really can't use PHP, you can use the formmail.cgi if your host provides one. Most hosts support this, and instructions for using FormMail are simple.

This is trivial if you are using PHP though. You can try to rename the page with a .php extension (instead of .htm or .html) and put the following code into your page:

        <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("[email protected]", "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>

Upvotes: 0

Jean Azzopardi
Jean Azzopardi

Reputation: 2289

Any reason you can't use server-side code? This kind of thing is very easily doable with PHP. Otherwise, the only choice is the <a href="mailto .. as pointed out already.

Upvotes: 1

Related Questions