Php 如何创建HTML表单,在用户输入电子邮件并单击“提交”后,从我的gmail帐户向他们发送电子邮件?

Php 如何创建HTML表单,在用户输入电子邮件并单击“提交”后,从我的gmail帐户向他们发送电子邮件?,php,html,forms,email,gmail,Php,Html,Forms,Email,Gmail,我有一个HTML表单,比如 <form name="contact-form" method="POST" action="sendemail.php"> <label>Name</label> <input type="text" name="name" required="required">

我有一个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。我该如何为它编写sendmail.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();
?>

您可以冷静地使用此方法编写一个方法

如果您使用的是经典的PHPmailer:

在debianoid上,您可以尝试以下方法:

apt install libphp-phpmailer
它将从()安装PHPmailer类

然后,您可以按照

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

我制作了一个超级简单的版本

//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对象
$mail=新的PHPMailer;
$mail->isSMTP();//使用SMTP发送
$mail->Host='smtp.gmail.com';//将SMTP服务器设置为通过发送
$mail->SMTPAuth=true;//启用SMTP身份验证
$mail->Username=josh@gmail.com';               // 您的SMTP用户名
$mail->Password='您的gmail密码';//您的SMTP密码
$mail->SMTPSecure=PHPMailer::ENCRYPTION\u STARTTLS;//启用TLS加密`PHPMailer::鼓励加密\u SMTPS`
$mail->Port=587;//要连接到的TCP端口,使用465
//来自电子邮件地址和名称
$mail->From=”josh@gmail.com";
$mail->FromName=“Josh Bealer”;
//解决
$mail->addAddress(“recepient@example.com");
//抄送和密件抄送
$mail->addCC(“cc@example.com");
$mail->addBCC(“bcc@example.com");
//发送HTML或纯文本电子邮件
$mail->isHTML(true);
$mail->Subject=“主题文本”;
$mail->Body=“HTML中的邮件正文”;
$mail->AltBody=“这是电子邮件内容的纯文本版本”;
试一试{
$mail->send();
}
捕获(例外$e){
echo$e->getMessage();
}
捕获量(无效argumentexception$e){
echo$e->getMessage();
}

我想我会使用PHPmailer。谢谢@Max Muster。有没有教程或任何东西解释了如何喜欢代码我想做什么,当我网站的用户输入他们的姓名和电子邮件地址并单击“提交”时,会从我的Gmail通过该电子邮件地址向他们发送pdf?是的,这里有一个简单的例子:谢谢。它是如何检查我的gmail是否有效以及我是否是gmail的所有者的呢?看看我提交的例子。它会问你gmail用户和密码谢谢。如果StuckYou shure您设置您的gmail帐户的方式可以从“未知”位置访问它,您会得到回复吗?@MaxMuster我的代码是,它保存为sendmail.php,当我访问localhost/sendmail.php时,它只显示php代码,但不“执行”它?您安装了php吗?它是什么版本?在ubuntu php上——PHP7.2.24-0ubuntu0.18.04.6(cli)(构建时间:2020年5月26日13:09:11)(NTS)版权所有(c)1997-2018 php集团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@MaxMusterI刚刚意识到我应该使用