简体   繁体   中英

How do I send an E-mail from a webpage?

I've been trying to learn the basics of HTML, and in doing this I've been designing my own rudimentary webpage. I don't plan on making it public at all, but it's good to help me to learn all of the different aspects of both HTML and CSS. In doing this, I've added a Contact section on one of the pages, that includes both an email address and a form that allows for an E-mail to be sent, I used one of the tutorials on W3Schools to create it. For the purposes of this I have removed my own email address, and replaced it with someone@example.com , but here is the output of this specific part of the code:

电子邮件

However, whenever I try to fill the form in to test it, I get this pop-up message. If I click cancel, then nothing happens, but if I click OK then then the mail app on my computer is opened. But the message that I typed into the form isn't there, and the E-mail address that I type into the box to send from is just changed to the default address on my computer.

弹出

So what can I do to prevent this pop-up message, and to just send the e-mail to me?

Here is the relevant code from the HTML document:

<h2 style = 'font-weight:normal'><a name = 'Contact' id = 'Contact'></a>Contact me:</h2>

<p>
  You can reach me at: someone@example.com <br />
  Or by just using the form below
</p>

<form action = 'mailto: someone@example.com' method = 'post' enctype = 'text/plain'>
  <input type = 'text' name = 'name' placeholder = 'Name'> <br />
  <br />
  <input type = 'text' name = 'mail' placeholder = 'E-mail'> <br />
  <br />
  <input type = 'text' name = 'comment' size = '50' placeholder = 'Comment'> <br />
  <br />
  <input type = 'submit' value = 'Send'>
  <input type = 'reset' value = 'Reset'>
</form>

If HTML isn't going to be enough to send an e-mail from a webpage, and I need another language to write a program that can do it, I am quite competent in Python, and I know C# to some extent. However, I've never used JavaScript, PHP, Perl, or anything else (I don't know what sort of languages would be appropriate)

If you want to submit the form to go to an email it's simple really. You could use a simple server-side language like PHP. You'll need 3 files. One file that houses the front-end form contents, one file that processes the form once the user hits the submit button and a thank you page after the form gets sent to let the user know that the form has been submitted. Here is a demo below.

HTML:

 <form action="processor.php" method="post" id="myForm">
        <label for="firstname"></label>
        <input type="text" name="firstname" placeholder="First Name" id="firstname" required>
        <label for="lastname"></label>
        <input type="text" name="lastname" placeholder="Last Name" id="lastname" required> 
        <label for="email"></label>
        <input type="email" name="email" placeholder="Email" id="email" required>
        <label for="comments"></label>
        <textarea rows="4" cols="32" name="comments" id="comments" placeholder="Questions & Comments"></textarea>
        <input type="submit" value="Submit">
    </form>

PHP (processor.php file)

/***********************************************************/
/*******   Set the receipient email address below   ********/
/*******   And set the subject line of the email    ********/
/*$recipient_email = "testemail@yahoo.com";*/
$recipient_email = "testemail@yahoo.com";
$email_subject_line = "Mail from Website";

/***********************************************************/
/***********************************************************/

if(isset($_POST['firstname']))
{
$firstName = $_POST['firstname'];
    $lastName = $_POST['lastname'];
    $email = $_POST['email'];
    $comments = $_POST['comments'];

   if(!empty($firstName) && 
    !empty($lastName) &&
    !empty($email) &&
    !empty($comments))
   {

$message = "Name: $firstName, Lastname: $lastName, Phone: $phoneNumber, 
Email: $email, Comments: $comments";

send_mail($email, $message, $recipient_email, $email_subject_line);

   }
}

function send_mail($email, $message, $recipient_email, $email_subject_line)
{
$to = $recipient_email;
$from = $email;
$subject = $email_subject_line;
$headers = "From: {$email}" . "\r\n" . 'Reply-To:' . $email . "\r\n" . 'X-
Mailer: PHP/' . phpversion();

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

header("Location:thankyoupage.php");

thankyoupage.php (After data has been submitted)

<div class="thankyoucontainer">
    <h1>Thank you, your message has been submitted.</h1>
    <a href="index.php">Go back to home page</a>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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