php - 使用表单数据并将其发送到 gmail 的最佳方法是什么
问题描述
我对一些 PHP 表单处理有疑问。我想要的是获取下面表格中的数据,并将数据发送到我的 Gmail 帐户。我没有自己的邮件服务器。我已经发现你不能用 PHP 邮件功能来做到这一点。我现在正在尝试使用 PHPmailer,但它似乎没有正确的结果,因为 PHPmailer 似乎只能用于填写表格并将其发送给具有您的 Gmail 地址的人。我想要一些不同的东西我想让用户使用表单输入将我邮寄到我的 Gmail 地址。那我该怎么办?使用 hmailserver 和 SquirrelMail 设置邮件服务器,或者有更好的方法。
<form id="form" action="PHPmail2.php" method="POST">
<input type="text" name="firstname"class="first" placeholder="first name"><font color="red">*</font>
<input type="text" name="lastname" class="first" placeholder="lastname"><font color="red">*</font>
<input type="email" name="email" class="first" placeholder="E-mail"><font color="red">*</font>
<br>
<input type="text" name="subject" class=" subject first" placeholder="subject">
<textarea type="text" class="textarea mail first" name="textarea" placeholder="fill somting in"></textarea>
<button type ="submit" id="submitbutton"name="submit">Send</button>
</form>
PHP代码:
<?php
if (isset($_POST['submit'])) {
if (!empty($firstname)) {
header('Location:index.php');
}else{
header('Location: filleErrorHandeling/nietAlleVeldenIngevuld.php');
}
if (!empty($lastname)) {
header('Location:index.php');
}else{
header('Location: filleErrorHandeling/nietAlleVeldenIngevuld.php');
}
if (!empty($from)) {
header('Location:index.php');
}else{
header('Location: filleErrorHandeling/nietAlleVeldenIngevuld.php');
}
}
添加一些 PHPmailer 代码我所要做的就是将表单操作链接到文件名。
<?php
$to="*";
$from=$_POST['email'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$subject=$_POST['subject'];
$msg=$_POST['textarea'];
$subject= "form submission";
$subject2="Copy of your form submission";
$message= $firstname. " ".$lastname. " Wrote the following: "."\n\n".$msg;
$message2= "Here is a copy of your message ". $firstname ."\n\n". $msg;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require ("composer/vendor/autoload.php");
$mail= new PHPMailer(true);
try{
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Port =25;
$mail->Username =$to;
$mail->Password = '*';
$mail->setFrom($email);
$mail->addAddress($email);
$mail->Subject='*';
$mail->Body=$msg;
$mail->send();
}
catch(Exception $e){
echo $e->errorMessage();
}
catch(\Exception $e)
{
echo $e->getMessage();
}
?>
我基本上想要的是当用户按下提交时,我想要一封包含所有输入数据的电子邮件到我的 Gmail 帐户,或者甚至是其中的一部分。
感谢您阅读我的帖子,如果对此主题有任何疑问,请告诉我。
解决方案
推荐阅读
- javascript - 将一些代码从 jQuery 重写为 JS (ReactJS)
- jquery - 通过唯一或单个类 JQuery 查找元素
- c - 将结果存储在 c 中的数组中
- php - 我可以在一台主机上使用我的 mysql 数据库,而在另一台主机上使用我的服务器文件吗?
- java - 从主要活动访问方法和内容的片段(一切都为空)
- apache-camel - camel spring ws - 从类型转换期间出错:java.io.ByteArrayInputStream
- typescript - NestJS + NuxtJS(在 TypeScript 中)
- python - itertools.permutations 是否总是返回一个排序的对象?
- javascript - 来自 Google 地方信息的图片需要刷新页面
- java - 找不到“org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder”类型的 bean