我有一个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 PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;
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();
?>
您可以很酷地使用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(
然后您可以按照https://github.com/PHPMailer/PHPMailer#a-的简单示例
我认为我从那里为你复制代码是没有意义的。。。
我做了一个超级简单的版本
//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();
}