首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用重定向将表单添加到电子邮件?

如何使用重定向将表单添加到电子邮件?
EN

Stack Overflow用户
提问于 2019-04-10 07:27:37
回答 1查看 517关注 0票数 -1

我该如何解决这个问题?

尝试

代码语言:javascript
复制
<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>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-10 09:08:10

不需要使用像formspree这样的服务。相反,您应该使用现成的PHP-Mailer类,如PHPMailer

您可以(从理论上)使用mail()类,但这不是一个好主意。在他们的GitHub页面上解释了为什么不想使用mail()函数:

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

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

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

PHPMailer允许简单的集成和配置,并且可以轻松地安装和实施。

在互联网上有大量关于PHPMailer的优秀教程。他们的GitHub页面上已经提供了一个基本的示例:

代码语言:javascript
复制
<?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基础知识。例如,关于名称空间的知识是必不可少的。对于初学者来说,像formspree这样的mail()函数或服务看起来相当简单,但我建议您正确地使用它,或者根本不使用它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55602670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档