首页 > 解决方案 > 如何通过重定向将表单添加到电子邮件?

问题描述

我有一个网站,我想用 PHP 添加一个表单到电子邮件,但我不太擅长。我该如何解决这个问题?

试图

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Checkout</title>
    <link rel="stylesheet" href="style2.css">

	
	
	
	
	</head>
<body>
    <header>
            <div class="container">
                    <h1 class="logo">LOLLYSHOP</h1>
      
                      <nav>
                         <ul>
                             <img src="file:///C:/Users/zam/Downloads/lines-menu.svg" alt="" width="40" height="40">
                          </ul>
                      </nav>  
                  </div>
    </header>
    <div>
        <img id="badla" src="https://sc01.alicdn.com/kf/UTB8lgV0pWrFXKJk43Ovq6ybnpXay/latest-shirt-design-for-men.jpg" height="250" width="250">
        <p id="suit">Blue Suit</p>
        <p id="suit">Price<strong>:</strong>$50</p>
        
		
		<form action="https://formspree.io/aliabououf@gmail.com" method="POST" accept-charset="utf-8">
 
  
        <input type="hidden" name="itemid" value="1"> 
        <h2>Name:          <input type="username" id="name" placeholder="Your Name" name="username" required></h2>
        <h2>Address:</h2>
        <textarea placeholder="Type Your Address" rows="20" id="comment_text" cols="40" class="ui-autocomplete-input" autocomplete="off" role="textbox" aria-autocomplete="list" aria-haspopup="true" name="address" required></textarea>
      <br> <br>
      <p id="note"><b>Note:Payement is When Recieving</b></p>      
        <a href="done.html"><button type="submit" value="Buy" id="buy1">Buy</button></a>
    </form> 
	
	<?php

	if(!isset($_POST['submit'])) 
	{
// the message
$Item = $_POST['itemid'];
$name = $_POST['name'];
$address = $_POST['comment_text'];


mail("aliabououf@gmail.com","E-Market","Item : " $Item "Name :" $name "address:" $address );
header('Location: done.html')
}
?>
</div>
</body>
</html>

标签: phphtmlforms

解决方案


无需使用formspree. 您应该做的是使用现成的 PHP-Mailer 类,例如PHPMailer
您可以(理论上)使用mail()该类,但这不是一个好主意。为什么你不想使用该mail()功能在他们的 GitHub 页面上进行了解释:

唯一支持此功能的 PHP 函数是 mail() 函数。但是,它不提供任何帮助来使用流行的功能,例如基于 HTML 的电子邮件和附件。

正确格式化电子邮件非常困难。有无数重叠的 RFC,需要严格遵守极其复杂的格式和编码规则——你在网上找到的绝大多数直接使用 mail() 函数的代码都是完全错误的!请不要试图自己动手——如果你不使用 PHPMailer,那么在你自己动手之前,你应该看看许多其他优秀的库——试试 SwiftMailer、Zend/Mail、eZcomponents 等。

PHP mail() 函数通常通过本地邮件服务器发送,通常在 Linux、BSD 和 OS X 平台上以 sendmail 二进制文件为前端,但是,Windows 通常不包含本地邮件服务器;PHPMailer 的集成 SMTP 实现允许在没有本地邮件服务器的 Windows 平台上发送电子邮件。

PHPMailer 允许简单的集成和配置,并且可以轻松安装和实现。
互联网上有大量关于 PHPMailer 的好教程。他们的 GitHub 页面上已经提供了一个基本示例:

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Load Composer's autoloader
require 'vendor/autoload.php';

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = 2;                                       // Enable verbose debug output
    $mail->isSMTP();                                            // Set mailer to use SMTP
    $mail->Host       = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'user@example.com';                     // SMTP username
    $mail->Password   = 'secret';                               // SMTP password
    $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
    $mail->Port       = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    // Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

如果这超出了您当前的技能水平,则必须查看 PHP 基础知识。例如,关于名称空间的知识是必不可少的。对于初学者来说,类似的mail()功能或服务formspree看起来相当简单,但我建议你做对或根本不做。


推荐阅读