Crani0
Crani0

Reputation: 43

HTML / PHP contact form doesn't send data to my gmail

i've been trying to fix this one with solutions i've found here and in other foruns but nothing seems to work. I'm really noob with PHP. I don't know if this info is relevant but i'm hosting my project at byethost.com which they say it supports PHP.

I have made a contact form in my HTML contact page, made a PHP file to process the data and send it to my gmail adress. It says message successfully sent, but the data never arrives at my inbox on gmail. I've tried changing the PHP code (with copy paste solutions) in alternative ways but with no luck. I've also tried to sent it to my hotmail account but it doesn't work either. Can anyone help me?

Here is the HTML contact form:

     <div id="form_wrap">
        <form method="post" action="form_process.php" id="contact_form" class="contact">
                <label>
                    <span>NOME*</span>
                    <input name="nome" type="text" tabindex="1" required>
                </label>

                <label>
                    <span>E-MAIL*</span>
                    <input name="email" type="email" tabindex="2" required>
                </label>

                <label>
                    <span>TELEFONE*</span>
                    <input name="phone" type="tel" tabindex="3" required>
                </label>

                <label>
                    <span>WEBSITE</span>
                    <input name="website" placeholder="http://" type="url" tabindex="4">
                </label>

                <label>
                    <span>MOTIVO DE CONTACTO*</span>
                    <select class="escolha" name="motivo" size="1" tabindex="5" required>
                    <option>Contratá-lo</option>
                    <option>Fazer uma pergunta</option>
                    <option>Dizer olá ou agradecer</option>
                    </select>
                </label>

                <div>
                <label>
                    <span>MENSAGEM*</span>
                    <textarea name="message" tabindex="6" required></textarea>
                </label>
                </div>

                <div>
                <input name="submit" type="submit" value="Enviar" id="submit">
                </div>

        </form>
            <a class="subtop" href="#subtop">&#8211; Voltar ao topo</a> 
        </div>

Here is my form_process.php:

    <?php
if(isset($_POST['submit'])) {

$nome = $_POST['nome'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$website = $_POST['website'];
$motivo = $_POST['motivo'];
$message = $_POST['message'];

$to = "[email protected]";
$subject = "Site contact form";

$header = "From: ".$fromText."\r\n";
$header .= "Cc: ".$ccText."\n";
$header .= "Reply-To : ".$fromText."\r\n";
$header .= "Return-Path : ".$fromText."\r\n";
$header .= "X-Mailer: PHP\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";

mail($to, $subject, $header, $message, "From: " . $nome);
}
if(@mail($emailRecipient, $subject, $message, $headers))
{
  echo "Mail Sent Successfully";
}else{
  echo "Mail Not Sent";
}
?>

Upvotes: 0

Views: 4619

Answers (4)

Leonid Makhnovskiy
Leonid Makhnovskiy

Reputation: 1

If you are usinf free hosting, they probably restrict your ability to send email. Something like that is happening:

https://www.freehosting.com/client/knowledgebase.php?action=displayarticle&id=25

PHP mailing functionality is limited on free account to prevent abuse. To make it working, your mailing script should be configured to use SMTP server 'cpanel.freehosting.com' and to authenticate against it using credentials of email account set up in cpanel. Paid accounts are provided with unrestricted access to PHP mailing functionality.

You can find more info on setting up email authentication in PHP scripts at this link: http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm

Optionally, PHP mail() can be enabled by purchasing corresponding addon HERE.

Upvotes: 0

Richard Washington
Richard Washington

Reputation: 483

There are many possible reasons that the email never arrives.

It could be caught in spam, blocked by your host or your mail function could be incorrectly setup in your php.ini file.

However, from your code it looks like you are using the mail() function incorrectly.

You are trying to send the email twice by calling the function twice. Just call it once like:

  if(mail(...)) {
    echo 'good times';
    } else {
    echo 'boo, no email was sent';
    }

Secondly you are using the function incorrectly. According to the documentation here the mail function takes five arguments like so:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

$additional_headers and $additional_parameters are optional as denoted by the [square brackets]. $to, $subject and $message are required in that order.

Thirdly I heavily suggest NOT using the built in mail() function.

I suggest using SwiftMailer. It's a fully comprehensive PHP library which will look after you.

Upvotes: 1

hywak
hywak

Reputation: 900

U are trying to send mail twice you are using wrong variable names.

This code works for me.

<?php
    if(isset($_POST['submit'])) {

    $nome = $_POST['nome'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $website = $_POST['website'];
    $motivo = $_POST['motivo'];
    $message = $_POST['message'];

    $to = "[email protected]";
    $subject = "Site contact form";

    $header = "From: ".$email."\r\n";
    $header .= "Cc: ".$email."\n";
    $header .= "Reply-To : ".$email."\r\n";
    $header .= "Return-Path : ".$email."\r\n";
    $header .= "X-Mailer: PHP\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";

    if(mail($to, $subject, $message, $header))
    {
      echo "Mail Sent Successfully";
    }else{
      echo "Mail Not Sent";
    }

    }
    ?>

Upvotes: 0

pavel
pavel

Reputation: 27072

Why twice mail function?

  1. mail has 4 parameters, you give 5 params (from is a paert of fourth, it should be in $header)
  2. variable $emailRecipient used in your mail function doesn't exists.
  3. in 'from' header should be an e-mail address (and name), not only a non-mail string

Upvotes: 1

Related Questions