When I click on the button it is not working but when i refresh the page the mails are going.Please suggest me how to solve this problem.

     <button type="submit" title="<?php echo $this->__('Email Me') ?>"  class="button"><span><span><?php echo $this->__('Email Me')?></span></span></button>

<?php
$email=$_POST['email'];
$name=$_POST['name'];
$to=$email;
$subject= "New Application";
$message= "Name: ".$name;
$headers= "From: admin@labwise.in" . "\r\n" .
mail($to, $subject, $message, $headers);
?> 

Dani AI

Generated

A couple of things are likely at play here. First, as hinted, make sure the request is actually posting to a PHP handler and that your mail logic only runs on POST. Second, your $headers line appears to concatenate the mail() call into the header string, which unintentionally executes mail() with an empty/undefined $headers value. Finally, page refreshes will often resubmit the last POST, which explains why you see a send on refresh.

A minimal pattern that avoids all of this is:

  • Wrap inputs in a <form method="post" action="">.
  • On the server, guard with if ($_SERVER['REQUEST_METHOD'] === 'POST').
  • Build headers separately, then pass them to mail().
  • Use Post/Redirect/Get (PRG) to prevent duplicate sends on refresh.

Example:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name  = trim($_POST['name'] ?? '');
    $email = trim($_POST['email'] ?? '');

    if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $to       = $email;
        $subject  = 'New Application';
        $message  = "Name: {$name}";
        $headers  = "From: admin@labwise.in\r\n";
        $headers .= "Reply-To: admin@labwise.in\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/plain; charset=UTF-8\r\n";

        mail($to, $subject, $message, $headers);
        header('Location: ?sent=1'); // PRG
        exit;
    }
}
?>
<form method="post" action="">
  <input name="name" required>
  <input name="email" type="email" required>
  <button type="submit" class="button">Email Me</button>
</form>

Notes:

  • If you need non-ASCII in the subject, encode it per RFC rules for Internet message headers (see RFC 5322).
  • On many hosts mail() is unreliable or disabled. Prefer an SMTP library like PHPMailer and send through an authenticated mail server.
  • is right about __() in Magento; that is fine to keep for translation, it does not affect form submission.

Recommended Answers

All 6 Replies

Whats the action on your <form> element?

Also, the method name in $this->__('Email Me') does not seem to be valid or is my knowledge of PHP a bit shaky?

, thnx, I was not aware of that.

OK.Can we add variables in subject in mail function

Yes. You can use a variable to prepare a string for the subject. Please note that subject must satisfy RFC 2047. See the PHP mail documentation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.