【问题标题】:How to create HTML form in which after user inputs email and clicks submit an email is sent to them from my gmail account?如何创建 HTML 表单,在用户输入电子邮件并单击提交后,电子邮件会从我的 gmail 帐户发送给他们?
【发布时间】:2020-08-05 00:52:57
【问题描述】:

我有一个 HTML 表单,例如

<form name="contact-form" method="POST" action="sendemail.php">
        <label>Name</label>
       <input type="text" name="name" required="required">
        <label>Email</label>
        <input type="email" name="email" required="required">
        <button type="submit" name="submit" required="required">Submit</button>
</form>
         

现在我希望这样,当我网站的用户输入他们的姓名和电子邮件地址并单击提交时,我的 Gmail 会通过该电子邮件地址通过电子邮件将 pdf 发送给他们。我将如何为它编写 sendemail.php?

试过max的回答:

<?

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;


require 'vendor/autoload.php';


$mail = new PHPMailer;

$mail->isSMTP();                                            // Send using SMTP
$mail->Host       = 'smtp.gmail.com';                    // Set the SMTP server to send through
$mail->SMTPAuth   = true;                                   // Enable SMTP authentication
$mail->Username   = 'sample1@gmail.com';                     // SMTP username
$mail->Password   = 'mypassword';                               // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port       = 587;                                    // TCP port to connect to, use 465 for


//From email address and name
$mail->From = "sample1@gmail.com";
$mail->FromName = "Josh Bealer";

//To address
$mail->addAddress("receiveremail@gmail.com");

//CC and BCC
$mail->addCC("cc@example.com");
$mail->addBCC("bcc@example.com");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Blah</i>";
$mail->AltBody = "This is the plain text version of the email content";

$mail->send();
?>

【问题讨论】:

  • 你确定你的 gmail 帐户设置为可以从“未知”位置访问吗?
  • @MaxMuster 我的代码是pastebin.com/fLetQqKg,它保存为 sendemail.php,当我访问 localhost/sendemail.php 时它只显示 php 代码但不“执行”它?
  • 你安装php了吗?这是什么版本?
  • 在 ubuntu php --version PHP 7.2.24-0ubuntu0.18.04.6 (cli) (build: May 26 2020 13:09:11) (NTS) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0,版权所有 (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.24-0ubuntu0.18.04.6,版权所有 (c) 1999-2018,Zend Technologies @MaxMuster
  • 我刚刚意识到我应该使用

标签: php html forms email gmail


【解决方案1】:

你可以很酷,用这个https://www.php.net/manual/en/ref.imap.php写一个方法

你们中的一些人使用经典的 PHPmailer:https://github.com/PHPMailer/PHPMailer

在 debianoid 上你可以试试这个:

apt install libphp-phpmailer

它将从 (https://github.com/PHPMailer/PHPMailer) 安装 PHPmailer 类

那么你可以按照https://github.com/PHPMailer/PHPMailer#a-simple-example的例子进行

我认为我从那里为你复制代码是没有意义的......

我做了一个超级简单的版本

//PHPMailer Object
$mail = new PHPMailer;

$mail->isSMTP();                                    // Send using SMTP
$mail->Host       = 'smtp.gmail.com';               // Set the SMTP server to send through
$mail->SMTPAuth   = true;                           // Enable SMTP authentication
$mail->Username   = 'josh@gmail.com';               // your SMTP username
$mail->Password   = 'your_gmail_password';          // your SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port       = 587;                            // TCP port to connect to, use 465 for


//From email address and name
$mail->From = "josh@gmail.com";
$mail->FromName = "Josh Bealer";

//To address
$mail->addAddress("recepient@example.com");

//CC and BCC
$mail->addCC("cc@example.com");
$mail->addBCC("bcc@example.com");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
}
catch (Exception $e) {
    echo $e->getMessage();
}
catch (InvalidArgumentException $e) {
    echo $e->getMessage();
}

【讨论】:

  • 我想我会使用 PHPmailer。谢谢@Max Muster。当我的网站用户输入他们的姓名和电子邮件地址并点击提交 pdf 时,任何教程或任何解释如何点赞代码我想要做什么的教程或任何内容都会通过我的 Gmail 的电子邮件地址通过电子邮件发送给他们?
  • 是的,有一个简单的例子:github.com/PHPMailer/PHPMailer#a-simple-example
  • 谢谢。它如何检查我的 gmail 是否有效并且我是 gmail 的所有者?
  • 看看我提交的例子。它会询问您的 gmail 用户名和密码
  • 谢谢。如果卡住会回复你
猜你喜欢
  • 2013-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-30
  • 1970-01-01
  • 2015-03-28
  • 2016-07-26
相关资源
最近更新 更多