【发布时间】: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