0

im pretty new on it, my question is how to make it work

<?php

$errors = [];

if (isset($_POST['submitted']) && $_POST['submitted'] == 1)
{
    $email = trim($_POST["email"]);
    $message = trim(stripslashes($_POST['message']));
    $to = "adam9311@abv.bg";

    if (!filter_var($email, FILTER_VALIDATE_EMAIL))
    {
        $errors['email'] = "<p>Please type a valid email address.</p>";
        }

    if (empty($message))
    {
        $errors['message'] = "<p>Please type your message.</p>";
    }


    if (empty($errors)) // if  empty $errors array
    {

        require 'PHPMailerAutoload.php';

        $mail = new PHPMailer;

        mail($to,$message,$email);

        if(!$mail->send()) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
            echo 'Message has been sent';
        }
    }
}

?>

<?php if (!empty($errors)) : ?>
    <?php foreach ($errors as $key => $error) : ?>
        <p><?php echo $error; ?></p>
    <?php endforeach; ?>
<?php endif; ?>

    <form name="contact" action="index.php" method="post">
        <input type="hidden" name="submitted" value="1"/>

        <label for="email">Your Email:</label>
        <input type="text" name="email" class="required" id="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''; ?>">
        <br />
        <label for="message">Your Message:</label>
        <textarea  name="message" cols="10" rows="10" class="required" id="message"><?php echo isset($_POST['message']) ? $_POST['message'] : ''; ?></textarea>

    <fieldset>
        <input type="submit" name="submit" id="submit" value="Send" class="required"/>
    </fieldset>

</form>
Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
zarka
  • 1
  • 1
  • Are you using [PHPMailer](https://github.com/PHPMailer/PHPMailer) OR PHP [mail()](http://php.net/manual/en/function.mail.php)? Why do you create a phpmailer object and eventually using the regular `mail()`? – Alon Eitan Jul 18 '16 at 19:50
  • phpmailer != `mail()`. there is literally no point in using both, and `mail()` should be avoided at all costs. it's mickey-mouse garbage, and useful only as an emergency fallback. – Marc B Jul 18 '16 at 19:50

0 Answers0