Home php Simple Form Sending Script on E Mail

Simple Form Sending Script on E Mail

Author

Date

Category

Hello! Here is the easiest way to send a message to email. In the developer panel shows “Status 200”, but the post per mail does not come. Is there a solution to the problem?

& lt;? php
// If the Send button was pressed
if ($ _ require ['buttonsend']) {
 // If the POST method was used
 if ($ _ Server ['Request_Method'] == 'Post') {
 if ((ISSET ($ _ POST ['Text']) & amp; & amp; $ _post ['text']! = "")) {
  // Mail Recipient
  $ to = '[email protected]';
  // Talking messages
  $ subject = 'message';
  // message in the form of text
  $ message = $ _post ['text'];
  // message encoding for text
  $ Headers = "Content-Type: text / plain; charset = utf-8 \ r \ n";
  //sending letter
  Mail ($ To, $ Subject, $ Message, $ Headers);
 }
 }
}
? & gt;
& lt;! DOCTYPE HTML & GT;
& lt; html & gt;
& lt; Head & gt;
 & lt; Meta Charset = "UTF-8" / & gt;
 & lt; Title & gt; new page & lt; / title & gt;
& lt; / Head & gt;
& lt; Body & gt;
 & lt; Form id = "Form" action = "" Method = "POST" & gt;
 & lt; Input Type = "Text" name = "text" required = "Required" Placeholder = "Text" / & GT;
 & lt; br / & gt;
 & lt; br / & gt;
 & lt; Input Type = "Submit" name = "Buttonsend" value = "Send" / & gt;
 & lt; / form & gt;
 & lt; script src = "https://cdnjs.cloudflare.com/ajax/libs/JQuery/1.12.2/jquery.min.js" & gt; & lt; / script & gt;
 & lt; script src = "js / jQuery-1.12.2.min.js" & gt; & lt; / script & gt;
 & lt; script & gt;
 $ (Document) .ready (Function () {
 });
 & lt; / script & gt;
& lt; / body & gt;
& lt; / html & gt;

=== This is a continuation of my question ===

Maybe I missed something or did not understand. Here is here The settings are shown. I chose mail.ru This is a real mail (specially created) and real settings.
Mail: [email protected]
Password: ABC12345ZZZ756

Settings

SMTP server: smtp.mail.ru (smtp.list.ru / smtp.bk.ru / smtp.inbox.ru):
2525 or 465 Authentication: ESMTP – RFC 2554 Encryption: SSL login /
Password: Mailbox Name Fully ([email protected]), your password to
Mailbox Email Sender = Login Connection to server
(streams) – 1 limit: 1 message / min. or 200 email posts / day
POP3 server: pop.mail.ru (pop.list.ru / pop.bk.ru / pop.inbox.ru): 110

And all messages that users will enter from the form should, for example, to enter [email protected]

Here is the new index.php file, for convenience I postpone everything in it. You can copy yourself to the server and try to execute. Specify only your email instead of [email protected] to see, a message came or not.

& lt;? php
// File phpmailAtoladoad.php.
/ **
* PHPMailer SPL AutoLoader.
* PHP Version 5
* @Package phpmailer
* @link https://github.com/phpmailer/phpmailer/ The PHPMailer GitHub Project
* @Author Marcus Bointon (Synchro / Coolbru) & lt; [email protected]>
* @Author Jim Jagielski (Jimjag) & lt; [email protected]>
* @Author Andy Prevost (CodeWorXTech) & lt; [email protected]>
* @Author Brent R. Matzelle (Original Founder)
* @Copyright 2012 - 2014 Marcus Bointon
* @Copyright 2010 - 2012 Jim Jagielski
* @Copyright 2004 - 2009 ANDY PREVOST
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
* @Note This Program Is Distributed in the Hope That It Will Be Useful - Without
* ANY WARRANTY; Without Even The Implied Warranty of Merchantability OR
* Fitness for a Particular Purpose.
* /
/ **
* PHPMailer SPL AutoLoader.
* @Param String $ Classname The Name of the Class to Load
* /
Function phpMailerautoload ($ ClassName) {
 // CAN'T USE __DIR__ AS IT'S Only in PHP 5.3 +
 $ filename = dirname (__ file __). directory_separator.'class. '. Strtolower ($ ClassName).'. PHP ';
 If (IS_Readable ($ FileName)) {
 REQUIRE $ FILENAME;
 }
}
if (Version_Compare (PHP_VERSion, '5.1.2', '& gt; =')) {
 // SPL Autoloading WAS Introduced in PHP 5.1.2
 If (Version_Compare (PHP_VERSion, '5.3.0', '& gt; =')) { 
SPL_AUTOLOAD_REGISTER ('PHPMILERAUTOLOAD', TRUE, TRUE);
 }
 ELSE {
 SPL_AUTOLOAD_REGISTER ('PHPMILERAUTOLOAD');
 }
}
ELSE {
 / **
 * Fall Back to Traditional AutoLoad for Old PHP Versions
 * @Param String $ Classname The Name of the Class to Load
 * /
 FUNCTION __AUTOLOAD ($ ClassName) {
 Phpmailerautoload ($ classname);
 }
}
? & gt;
& lt;? php
// If the Send button was pressed
if ($ _ require ['buttonsend']) {
 // If the POST method was used
 if ($ _ Server ['Request_Method'] == 'Post') {
 if ((ISSET ($ _ POST ['Text']) & amp; & amp; $ _post ['text']! = "")) {
  // Mail Recipient
  $ to = '[email protected]';
  // Talking messages
  $ subject = 'Order a call';
  // message in the form of text
  $ message = $ _post ['text'];
  // message encoding for text
  $ Headers = "Content-Type: text / plain; charset = utf-8 \ r \ n";
  // Require 'phpmailerautolaoad.php';
  $ Mail = New phpMailer;
  // $ Mail- & gt; SMTPDEBUG = 3; // Start for debugging
  $ Mail- & GT; ISSMTP (); // Set Mailer To Use SMTP
  $ Mail- & gt; host = 'smtp.mail.ru'; // Specify Main and Backup SMTP Servers
  $ Mail- & gt; smtpauth = true; // Enable SMTP Authentication
  $ Mail- & GT; UserName = '[email protected]'; // SMTP Username.
  $ Mail- & GT; Password = 'ABC12345ZZZ756'; // SMTP Password.
  $ Mail- & gt; smtsecure = 'ssl'; // Enable TLS Encryption, `SSL` Also Accepted
  $ Mail- & gt; port = 465; // TCP PORT TO CONNECT TO
  $ Mail- & gt; setFrom ('[email protected] ',' mailer '); // from which address send a letter
  $ Mail- & gt; AddAddress ($ to); // Recipient Message
  $ Mail- & gt; subject = $ subject;
  $ Mail- & gt; Body = $ Message;
  If (! $ Mail- & gt; send ()) {
  DIE ('Mailer Error:'. $ Mail- & gt; errorinfo);
  }
 }
 }
}
? & gt;
& lt;! DOCTYPE HTML & GT;
& lt; html & gt;
& lt; Head & gt;
 & lt; Meta Charset = "UTF-8" / & gt;
 & lt; Title & gt; new page & lt; / title & gt;
& lt; / Head & gt;
& lt; Body & gt;
 & lt; Form id = "Form" action = "" Method = "POST" & gt;
 & lt; Input Type = "Text" name = "text" required = "Required" Placeholder = "Text" / & GT;
 & lt; br / & gt;
 & lt; br / & gt;
 & lt; Input Type = "Submit" name = "Buttonsend" value = "Send" / & gt;
 & lt; / form & gt;
 & lt; script src = "https://cdnjs.cloudflare.com/ajax/libs/JQuery/1.12.2/jquery.min.js" & gt; & lt; / script & gt;
 & lt; script src = "js / jQuery-1.12.2.min.js" & gt; & lt; / script & gt;
 & lt; script & gt;
 $ (Document) .ready (Function () {
 });
 & lt; / script & gt;
& lt; / body & gt;
& lt; / html & gt;

Answer 1, Authority 100%

If you have free hosting or your hosting does not support Email mailing, then to send a message to e-mail, you need to use the third-party SMTP server.

sendmailsmtpclass.php

& lt;? php
/ **
* SendMailsmtpclass
*
* Class to send letters via SMTP with authorization
* Can work via ssl protocol
* Tested on email servers Yandex.ru, Mail.Ru and Gmail.com
*
* @author iPatov Evgeniy & lt; [email protected]>
* @version 1.0.
* /
Class SendMailSmtpClass {
  / **
  *
  * @Var String $ SMTP_USERNAME - Login
  * @var String $ SMTP_PASSWORD - Password
  * @var String $ SMTP_HOST - Host
  * @var string $ smtp_from - from whom
  * @Var Integer $ SMTP_PORT - port
  * @var String $ SMTP_CHARSET - encoding
  *
  * /
  Public $ SMTP_USERNAME;
  Public $ SMTP_PASSWORD;
  Public $ SMTP_HOST;
  Public $ SMTP_FROM;
  Public $ SMTP_PORT;
  Public $ SMTP_CHARSET;
  Public Function __Construct ($ SMTP_USERNAME, $ SMTP_PASSWORD, $ SMTP_HOST, $ SMTP_FROM, $ SMTP_PORT = 25, $ SMTP_CHARSET = "UTF-8") {
    $ this- & gt; SMTP_USERNAME = $ SMTP_USERNAME;
    $ this- & gt; SMTP_PASSWORD = $ SMTP_PASSWORD;
    $ this- & gt; SMTP_HOST = $ SMTP_HOST;
    $ this- & gt; smtp_from = $ SMTP_FROM;
    $ this- & gt; SMTP_PORT = $ SMTP_PORT;
    $ this- & gt; smtp_charset = $ smtp_charset;
  }
  / **
  * Sending letter
  *
  * @Param String $ Mailto - Letter Recipient
  * @Param String $ Subject - letter 
* @Param string $ message - the message body
  * @Param string $ headers - the message header
  *
  * @Return bool | string in the sending case will return true, otherwise the error text *
  * /
  function send ($ mailTo, $ subject, $ message, $ headers) {
    $ ContentMail = "Date:". date ( "D, d M Y H: i: s"). "UT \ r \ n";
    $ ContentMail =. 'Subject: =?' . $ This- & gt; smtp_charset. '? B?' . base64_encode ($ subject). "= = \ R \ n?";
    $ ContentMail. = $ Headers. "\ R \ n";
    $ ContentMail. = $ Message. "\ R \ n";
    Try {
      if (! $ socket = @fsockopen ($ this- & gt; smtp_host, $ this- & gt; smtp_port, $ errorNumber, $ errorDescription, 30)) {
        throw new Exception ($ errorNumber $ errorDescription. ".".);
      }
      if (! $ this- & gt; _parseServer ($ socket, "220")) {
        throw new Exception ( 'Connection error');
      }
      $ Server_name = $ _SERVER [ "SERVER_NAME"];
      fputs ($ socket, "HELO $ server_name \ r \ n");
      if (! $ this- & gt; _parseServer ($ socket, "250")) {
        fclose ($ socket);
        throw new Exception ( 'Error of command sending: HELO');
      }
      fputs ($ socket, "AUTH LOGIN \ r \ n");
      if (! $ this- & gt; _parseServer ($ socket, "334")) {
        fclose ($ socket);
        throw new Exception ( 'Autorization error');
      }
      fputs ($ socket, base64_encode ($ this- & gt; smtp_username) "\ r \ n".);
      if (! $ this- & gt; _parseServer ($ socket, "334")) {
        fclose ($ socket);
        throw new Exception ( 'Autorization error');
      }
      fputs ($ socket, base64_encode ($ this- & gt; smtp_password) "\ r \ n".);
      if (! $ this- & gt; _parseServer ($ socket, "235")) {
        fclose ($ socket);
        throw new Exception ( 'Autorization error');
      }
      fputs ($ socket, "MAIL FROM: & lt;". $ this- & gt; smtp_username "& gt; \ r \ n".);
      if (! $ this- & gt; _parseServer ($ socket, "250")) {
        fclose ($ socket);
        throw new Exception ( 'Error of command sending: MAIL FROM');
      }
      $ MailTo = ltrim ($ mailTo, '& lt;');
      $ MailTo = rtrim ($ mailTo, '& gt;');
      fputs ($ socket, "RCPT TO: & lt;". $ mailTo "& gt; \ r \ n".);
      if (! $ this- & gt; _parseServer ($ socket, "250")) {
        fclose ($ socket);
        throw new Exception ( 'Error of command sending: RCPT TO');
      }
      fputs ($ socket, "DATA \ r \ n");
      if (! $ this- & gt; _parseServer ($ socket, "354")) {
        fclose ($ socket);
        throw new Exception ( 'Error of command sending: DATA');
      }
      fputs ($ socket, $ contentMail "\ r \ n \ r \ n.".);
      if (! $ this- & gt; _parseServer ($ socket, "250")) {
        fclose ($ socket);
        throw new Exception ( "E-mail did not sent");
      }
      fputs ($ socket, "QUIT \ r \ n");
      fclose ($ socket);
    } Catch (Exception $ e) {
      return $ e- & gt; getMessage ();
    }
    RETURN TRUE;
  }
  private function _parseServer ($ socket, $ response) {
    while (@substr ($ responseServer, 3, 1)! = '') {
      if (! ($ responseServer = fgets ($ socket, 256))) {
        RETURN FALSE;
      }
    }
    if (! (substr ($ responseServer, 0, 3) == $ response)) {
      RETURN FALSE;
    }
    RETURN TRUE;
  }
}
? & gt;

=== WHAT TO DO IN FILE index.php ===

This insert your email, from which messages will be sent, it is best to use mail.ru

// the mailbox from which the messages will be sent
$ Smtp_username = ‘[email protected]’;

Here insert the password of the mailbox

// password that mailbox
$ Smtp_password = '12345';

Correctly pick sending mail server

// server sending mail
/ *
gmail.com - ssl: //smtp.gmail.com
mail.ru - ssl: //smtp.mail.ru 
List.ru - ssl: //smtp.list.ru
bk.ru - ssl: //smtp.bk.ru
INBOX.RU - SSL: //SMTP.inbox.ru
* /
$ SMTP_HOST = 'ssl: //smtp.mail.ru';

Insert email here to which messages

// recipient mail
$ to = '[email protected]';

index.php

& lt;? php
// Connect the file
Require_ONCE ('sendmailsmtpclass.php');
? & gt;
& lt;? php
/ *
It is desirable that the mailbox would be posted on one of the mail servers
Yandex.ru or Mail.ru or Gmail.com
* /
// Mailbox from which messages will be sent
$ SMTP_USERNAME = '[email protected]';
// Password of this mailbox
$ SMTP_PASSWORD = '12345';
// send mail server
/ *
gmail.com - ssl: //smtp.gmail.com
Mail.Ru - ssl: //smtp.mail.ru
List.ru - ssl: //smtp.list.ru
bk.ru - ssl: //smtp.bk.ru
INBOX.RU - SSL: //SMTP.inbox.ru
* /
$ SMTP_HOST = 'ssl: //smtp.mail.ru';
// Your name or name of your site or organization
// in the message, in the field "From whom" will be this information
$ SMTP_FROM = 'Letter from Anton';
//port
$ SMTP_PORT = 465;
$ MailSmtp = New SendMailsmtpClass ($ SMTP_USERNAME, $ SMTP_PASSWORD, $ SMTP_HOST, $ SMTP_FROM, $ SMTP_PORT);
? & gt;
& lt;! DOCTYPE HTML & GT;
& lt; html & gt;
& lt; Head & gt;
 & lt; Meta Charset = "UTF-8" / & gt;
 & lt; Title & gt; new page & lt; / title & gt;
& lt; / Head & gt;
& lt; Body & gt;
 & lt; Form id = "Form" action = "" Method = "POST" & gt;
 & lt; Input Type = "Text" name = "text" required = "Required" Placeholder = "Text" / & GT;
 & lt; br / & gt;
 & lt; br / & gt;
 & lt; Input Type = "Submit" name = "Buttonsend" value = "Send" / & gt;
 & lt; / form & gt;
& lt;? php
// If the Send button was pressed
if ($ _ require ['buttonsend']) {
 // If the POST method was used
 if ($ _ Server ['Request_Method'] == 'Post') {
 if ((ISSET ($ _ POST ['Text']) & amp; & amp; $ _post ['text']! = "")) {
  // Mail Recipient
  $ to = '[email protected]';
  // Talking messages
  $ SUBJECT = 'Message Tallets';
  // message in the form of text
  $ message = $ _post ['text'];
  // message encoding for text
  $ Headers = "Content-Type: text / plain; charset = utf-8 \ r \ n";
  //sending letter
  $ result = $ MailSmtp- & gt; Send ($ to, $ subject, $ message, $ headers);
 }
 If ($ result === True) {
  Echo '& lt; H1 & GT; The letter has been successfully sent! & lt; / h1 & gt;';
 }
 ELSE {
  Echo '& lt; h1 & gt; Departure error:'. $ result. '& lt; / h1 & gt;';
 }
 }
}
? & gt;
 & lt; script src = "https://cdnjs.cloudflare.com/ajax/libs/JQuery/1.12.2/jquery.min.js" & gt; & lt; / script & gt;
 & lt; script & gt;
 $ (Document) .ready (Function () {
 });
 & lt; / script & gt;
& lt; / body & gt;
& lt; / html & gt;

!!! Now everything works !!! THANKS TO ALL!!! You can copy these two files, substitute your values ​​and test


Answer 2

To send letters in your case, you must use, for example, phpmailer
Replace settings to those that will give you a mailer.

& lt;? php
// If the Send button was pressed
if ($ _ require ['buttonsend']) {
 // If the POST method was used
 if ($ _ Server ['Request_Method'] == 'Post') {
 if ((ISSET ($ _ POST ['Text']) & amp; & amp; $ _post ['text']! = "")) {
  // Mail Recipient
  $ to = '[email protected]';
  // Talking messages
  $ subject = 'message';
  // message in the form of text
  $ message = $ _post ['text'];
  // message encoding for text
  $ Headers = "Content-Type: text / plain; charset = utf-8 \ r \ n";
  Require 'phpmailerautolaoad.php';
  $ Mail = New phpMailer;
  // $ Mail- & gt; SMTPDEBUG = 3; // Start for debugging
  $ Mail- & GT; ISSMTP (); // Set Mailer To Use SMTP
  $ mail- & gt; host = 'smtp1.example.com; smtp2.example.com'; // Specify Main and Backup SMTP Servers
  $ Mail- & gt; smtpauth = true; // Enable SMTP Authentication
  $ Mail- & gt; username = '[email protected]'; // SMTP Username.
  $ Mail- & gt; password = 'secret'; // SMTP Password. 
$ Mail- & gt; smtsecure = 'TLS'; // Enable TLS Encryption, `SSL` Also Accepted
  $ Mail- & gt; port = 587; // TCP PORT TO CONNECT TO
  $ mail- & gt; setfrom ('[email protected] ',' mailer '); // from which address send a letter
  $ Mail- & gt; AddAddress ($ to); // Recipient Message
  $ Mail- & gt; subject = $ subject;
  $ Mail- & gt; Body = $ Message;
  If (! $ Mail- & gt; send ()) {
   DIE ('Mailer Error:'. $ Mail- & gt; errorinfo);
  }
 }
 }
}
? & gt;
& lt;! DOCTYPE HTML & GT;
& lt; html & gt;
& lt; Head & gt;
 & lt; Meta Charset = "UTF-8" / & gt;
 & lt; Title & gt; new page & lt; / title & gt;
& lt; / Head & gt;
& lt; Body & gt;
 & lt; Form id = "Form" action = "" Method = "POST" & gt;
 & lt; Input Type = "Text" name = "text" required = "Required" Placeholder = "Text" / & GT;
 & lt; br / & gt;
 & lt; br / & gt;
 & lt; Input Type = "Submit" name = "Buttonsend" value = "Send" / & gt;
 & lt; / form & gt;
 & lt; script src = "https://cdnjs.cloudflare.com/ajax/libs/JQuery/1.12.2/jquery.min.js" & gt; & lt; / script & gt;
 & lt; script src = "js / jQuery-1.12.2.min.js" & gt; & lt; / script & gt;
 & lt; script & gt;
 $ (Document) .ready (Function () {
 });
 & lt; / script & gt;
& lt; / body & gt;
& lt; / html & gt;

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