簡體   English   中英

如何從網頁發送電子郵件?

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

我一直在嘗試學習HTML的基礎知識,為此,我一直在設計自己的基本網頁。 我根本不打算將其公開,但是很好地幫助我學習HTML和CSS的所有不同方面。 為此,我在其中一個頁面上添加了“聯系人”部分,其中既包含電子郵件地址,又包含允許發送電子郵件的表格,我使用了W3Schools上的其中一篇教程來創建它。 為此,我刪除了自己的電子郵件地址,並用someone@example.com替換了它,但這是該代碼的此特定部分的輸出:

電子郵件

但是,每當我嘗試填寫表單進行測試時,都會收到此彈出消息。 如果單擊“取消”,則什么也不會發生,但是,如果單擊“確定”,則會打開計算機上的郵件應用程序。 但是,我在表格中鍵入的消息不存在,並且我在框中鍵入要發送的電子郵件地址只是更改為計算機上的默認地址。

彈出

那么,如何防止出現此彈出消息並僅向我發送電子郵件呢?

以下是HTML文檔中的相關代碼:

<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>

如果HTML不足以從網頁發送電子郵件,而我需要另一種語言來編寫可以做到這一點的程序,則我在Python方面相當稱職,並且在某種程度上我知道C#。 但是,我從未使用過JavaScript,PHP,Perl或其他任何東西(我不知道哪種語言合適)

如果您想提交表格以發送電子郵件,那真的很簡單。 您可以使用簡單的服務器端語言,例如PHP。 您將需要3個文件。 一個文件包含前端表單的內容,一個文件在用戶單擊“提交”按鈕后處理該表單,並在發送表單后向用戶發送感謝頁面,以告知用戶表單已提交。 這是下面的演示。

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文件)

/***********************************************************/
/*******   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(提交數據后)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM