首页 > 解决方案 > 使用表单数据并将其发送到 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 帐户,或者甚至是其中的一部分。

感谢您阅读我的帖子,如果对此主题有任何疑问,请告诉我。

标签: phphtmlforms

解决方案


推荐阅读