Home php Sending mail via the mail () function does not work when submitting...

Sending mail via the mail () function does not work when submitting a form

Author

Date

Category

The script for sending letters does not work. Help me figure out what the problem might be. Nothing happens when the button is clicked

& lt;! - Form - & gt;
        & lt; form method = "post" action = "contact.php" name = "contactform" id = "contactform" & gt;
          & lt; fieldset & gt;
            & lt; div & gt;
              & lt; label for = "name" accesskey = "N" & gt; How to contact you: & lt; / label & gt;
              & lt; input name = "name" type = "text" id = "name" / & gt;
            & lt; / div & gt;
            & lt; div & gt;
              & lt; label for = "email" accesskey = "E" & gt; Email: & lt; span & gt; * & lt; / span & gt; & lt; / label & gt;
              & lt; input name = "email" type = "email" id = "email" pattern = "^ [A-Za-z0-9] (([_ \. \ -]? [a-zA-Z0-9] +) *) @ ([A-Za-z0-9] +) (([\. \ -]? [A-zA-Z0-9] +) *) \. ([A-Za-z] { 2,}) $ "/ & gt;
            & lt; / div & gt;
            & lt; div & gt;
              & lt; label for = "comments" accesskey = "M" & gt; Message: & lt; span & gt; * & lt; / span & gt; & lt; / label & gt;
              & lt; textarea name = "comments" cols = "40" rows = "3" id = "comments" spellcheck = "true" & gt; & lt; / textarea & gt;
            & lt; / div & gt;
          & lt; / fieldset & gt;
          & lt; input type = "submit" class = "submit" id = "submit" value = "Submit message" / & gt;
        & lt; / form & gt;

Email sending script

& lt;? php
// Check for fullness
if (empty ($ _ POST ['name']) ||
  empty ($ _ POST ['email']) ||
  empty ($ _ POST ['comments']) ||
  ! filter_var ($ _ POST ['email'], FILTER_VALIDATE_EMAIL))
  {
  echo "Fill in the marked fields";
  return false;
  }
$ name = $ _POST ['name'];
$ email_address = $ _POST ['email'];
$ message = $ _POST ['comments'];
// Create mail and send message
$ to = '[email protected]'; // Address of the recipient
$ email_subject = "Submitting from a contact form"; // Letter subject
$ email_body = "You received a new email from the website, sent via the contact form. \ n \ n". "Message details: \ n \ n Name: $ name \ n \ n Email: $ email_address \ n \ n Message: \ n $ message ";
$ headers = "Reply to the message at the address provided in the contact form: $ email_address";
$ send = mail ($ to, $ email_subject, $ email_body, $ headers);
return true;
if ($ send == 'true')
{
echo '& lt; div class = "notification error closeable" style = "display: block; float: right;" & gt;
      & lt; p & gt; & lt; span & gt; Error! & lt; / span & gt; Your message has not been sent & lt; / p & gt;
    & lt; / div & gt;
    & lt; div class = "clearfix" & gt; & lt; / div & gt; ';
}
else
{
echo '& lt; div class = "notification success closeable" style = "display: block; float: right;" & gt;
      & lt; p & gt; & lt; span & gt; Thank you! & lt; / span & gt; Your message was successfully sent & lt; / p & gt;
    & lt; / div & gt;
    & lt; div class = "clearfix" & gt; & lt; / div & gt; ';
}
? & gt;

Answer 1, authority 100%

First you haven’t read the manual for the function mail ( ) . You have $ headers this is a line with text, and you need to pass the line additional_headers to the function. See at least an example in the manual. (see example at the end)

Second using return in the global scope stops script execution. Documentation . So you never see your notification.

Thirdly sending an email is a big and complex issue. If you use the function mail () , then you need to take into account that php does not send letters, but transfers the sending to your mail server. Most likely this is POSTFIX . You need to look at the settings and logs.

and in the fourth letter could be sent, but not delivered. Again, look in logs and spam in the mailbox. It could get into black lists or spam sheets. Do not forget the same about dkim and SPF

It is better to send mail via SMTP server. For example Yandex or Google. For PHP there is a library SwiftMailer

PS. Here is an example of a working code:

$ charset = 'utf-8'; // letter encoding
$ to = ""; // Recipient
$ subject = ""; // Letter subject
$ text = ""; // Content Letters
$ torm = ""; // Sender
$ Fromame = ""; // Sender's name
// That's what is headlines
$ Headers = "Mime-Version: 1.0 \ n";
$ Headers. = "from: =? $ charset? b?". Base64_ENCode ($ Fromame). "? = & lt; $ freom & gt; \ n";
$ Headers. = "Content-Type: text / html; charset = $ charset \ n";
$ Headers. = "CONTENT-TRANSFER-ENCODING: BASE64 \ N";
return mail ("=? $ charset? b?" Base64_encode ($ to). "? = & lt; $ to & gt;", "=? $ charset? B?". Base64_ENCode ($ SUBJECT). "? =", chunk_split (Base64_encode ($ Text)), $ Headers, "-f $ FROM");

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions