0

I have a question about some PHP form handling. What I want is to take the data in the form below, and sent the data to my Gmail account. I do not own a mail server. I already discovered that you can not do it with the PHP mail function. I'm trying the PHPmailer out at this moment but it does not seem to have the right results, because it seems that PHPmailer only can be used to fill in a form and sent it to a person with your Gmail address.I want something different i want to let a user mail me to my Gmail address using the form inputs. So what shall I do? setting up a mail server with hmailserver and SquirrelMail or is there a better method.

<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>

The PHP code:

<?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');
    }

}

adding some PHPmailer code all i have to do is link the form action to the file name.

<?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();
    }

?>

What i basically want is when a user pressed on submit I want an email to my Gmail account with all the input data, or better even a part of it.

Thank you for reading my post if there are any questions regarding this topic let me know.

  • 1
    we need to see the php. not the html –  Mar 11 '19 at 21:28
  • Possible duplicate of [send email using Gmail SMTP server through PHP Mailer](https://stackoverflow.com/questions/16048347/send-email-using-gmail-smtp-server-through-php-mailer) –  Mar 11 '19 at 21:28
  • your still most posting any of the code that is actually related to sending email –  Mar 11 '19 at 21:51
  • The problem is that there is not much where I can come up with. The mail() function will not work. And PHPmailer is also not giving the result that I want. so I'm looking for a method that can work. If there is not a good method then the only solution is setting up a mail server, or rent one and use the mail() function. – Andre Meijer Mar 11 '19 at 21:59
  • you keep saying you're using phpmailer, but refuse to show us the code you are using. –  Mar 11 '19 at 22:01
  • Oke Tim, I added the other file where I was using PHPMailer, the file will work when I link it to my form. – Andre Meijer Mar 11 '19 at 22:18
  • postmarkapp has a free tier you can use to test with. I use that all the time. They have a free php library for integration as well. Otherwise, why not request your host enable mail() or switch to a host that has mail() enabled? – Kai Qing Mar 11 '19 at 22:22

0 Answers0