Sending mail to php via smtp. Creating a feedback form

Mail PHP

This function is implemented by default as Send Email. This functionality does not require a third-party server to send Email. In order to check the correctness of this function, it is enough to create a test.php file in the folder with the site and place the following code there:

Where instead "emailRECIPIENT" enter the email address to which the email should be sent.

Place given file at the root of your site.

Next, enter www.YourDomain/test.php in the address bar. If a letter has been sent to the specified mail, then your hosting works correctly with the function of sending letters " Mail PHP". It is possible that the letter got into spam, so it also needs to be checked.

If the letter came in an incomprehensible encoding, change the line: "Checking Mail Sent" on the "Checking mail sent", "Content-type:text/html;charset=utf-8".

And repeat sending the letter through the address bar of your browser.

If, after the manipulations done, the test letter did not reach the final recipient, you need to contact the technical support of your hosting.

Of the shortcomings this method it is worth noting that the work is not permanent this method. There may be interruptions in sending emails on the hosting side.

The main drawback of this method is that all letters sent to clients' emails will not be recognized by mail services (gmail, mail.ru, yandex.ru) such that they were sent directly from your site.

The Gmail mail service displays the email data with a question mark and a caption: "Gmail could not confirm that this email was sent from the domain YourDomain This might be spam."

To avoid this situation .

SMTP (Simple Mail Transfer Protocol)

SMTP is a mail transfer protocol. In total, to set up mail transmission using this protocol, the SMTP server itself is required. The most common way to use yandex.ru corporate (domain) mail as an SMTP server is https://pdd.yandex.ru/.

First of all, you need account (mail) yandex, on the basis of which mails for your domain will be created: " [email protected] YourDomain" " [email protected] YourDomain"... The main account will not be visible anywhere and is used only to create domain mails on its basis in the future.

After authorization, domain mail is created in your personal yandex account at the link https://pdd.yandex.ru/. It is this domain mail that users will see in the “Sender” field

The "Sender" field is required when sending emails via SMTP. Using SMTP as sending emails, a bunch of " Domain" - "email", thus letters are recognized by mail services.


Among the shortcomings of this method, it is worth noting the impossibility of "loading" the portrait of domain mail, since the mail services themselves do not yet provide this functionality.

Since sending anonymous messages from virtual Windows hosting servers is prohibited, sending letters should be done through an SMTP server, for which it is also possible to use our mail system.

How to send emails using PHP?

Using any PHP class that supports authorization on the smtp server. For example, you can use the PHPMailer class set.

You can also use our example, which implements sending emails using an smtp server that requires authorization. Therefore, do not forget to add the appropriate access details to the script, for example:

// If you need to show the SMTP session log, you can uncomment the following line. // $_SERVER["debug"] = true; function MailSmtp ($reciever , $subject , $content , $headers , $debug = 0 ) ( $smtp_server = "smtp.site" ; // SMTP server address$smtp_port = 25 ; // SMTP server port$smtp_user = " [email protected]" ; // Username for authorization on the SMTP server$smtp_password = "pAsSwORd" ; // Password for authorization on the SMTP server$mail_from = " [email protected]" ; // Mailbox from which the letter is sent$sock = fsockopen($smtp_server , $smtp_port , $errno , $errstr , 30 ); $str = fgets ($sock , 512 ); if (! $sock ) ( printf ("Socket is not created\n" ); exit(1 ); ) smtp_msg ($sock , "HELO " . $_SERVER [ "SERVER_NAME" ]); smtp_msg($sock , "AUTH LOGIN" ); smtp_msg ($sock , base64_encode ($smtp_user )); smtp_msg ($sock , base64_encode ($smtp_password )); smtp_msg($sock , "MAIL FROM:<" . $mail_from . ">" ); smtp_msg ($sock , "RCPT TO:<" . $reciever . ">" ); smtp_msg ($sock , "DATA" ); $headers = "Subject: " . $subject . "\r\n" . $headers ; $data = $headers . "\r\n\r\n" . $content . "\r\n." ; smtp_msg ($sock , $data ); smtp_msg ($sock , "QUIT" ); fclose ($sock ); ) function smtp_msg ($sock , $msg ) ( if ( ! $sock ) ( printf ("Broken socket!\n" ); exit(1 ); ) if (isset($_SERVER [ "debug" ]) && $_SERVER [ "debug" ]) ( printf ("Send from us :%s
" , nl2br (htmlspecialchars ($msg ))); ) fputs ($sock , " $msg \r\n" ); $str = fgets ($sock , 512 ); if (! $sock ) ( printf ("Socket is down\n" ); exit(1 ); ) else ( if (isset($_SERVER [ "debug" ]) && $_SERVER [ "debug" ]) ( printf ("Got from server: %s
" , nl2br (htmlspecialchars ($str ))); ) $e = explode (" " , $str ); $code = array_shift ($e ); $str = implode (" " , $e ); if ($code > 499 ) ( printf ( "Problems with SMTP conversation.

code %d.
Message %s
"
, $code , $str ); exit(1); ) ) ) ?>

Download an example of a finished script with the MailSmtp() function: smtpauth.php.sample

You can use the MailSmtp() function described above to directly replace the mail() function, consider an example of the simplest form in PHP:

// Message headers, they define the message encoding, fields From, To, etc.$headers = "MIME-Version: 1.0\r\n" ; $headers .= "Content-type: text/html; charset=windows-1251\r\n"; $headers .= "To: $to \r\n" ; $headers .= "From: Sender name " ; // mail ($to, $subject, $message, $headers); require_once "smtpauth.php" ; MailSmtp ($to , $subject , $message , $headers ); ) ?>
To whom: Topic: Text: 
 
 

To given form worked without the mail() function, we included the smtpauth.php file via require_once and called the MailSmtp() function described in it, with the arguments similar to mail(). At the same time, we commented out the mail() call itself in the code in order to avoid an error when executing the script.

How to send emails using ASP.NET?

If you are using version 2.0 then using the MailMessage class System.Net.Mail), well described.

Below are examples of its use for "C#" and "VisualBasic" languages:

Example for "C#" language:

<% @Page Language="c#" %> <% @Import Namespace="System.Net.Mail" %> <% @Import Namespace="System.Net" %> <% MailMessage message = new MailMessage(); // create a new letter message.To.Add(" [email protected]"); // adding the recipient's address message.From = new MailAddress(" [email protected]domain.tld", "Sender name"); // specifying the name and address of the sender message.Subject = "Subject"; // indication of the subject of the letter message.BodyEncoding = System.Text.Encoding.UTF8; // specifying the letter encoding message.IsBodyHtml = false; // specifying the letter format (true - HTML, false - not HTML) message.Body = "Message body"; // specifying the text (body) of the letter SmtpClient client = new SmtpClient("smtp.site",25); // creating a new connection to the "smtp.site" server client.DeliveryMethod = SmtpDeliveryMethod.Network; // defines the method for sending messages client.EnableSsl = false; // disables the need to use a secure connection to the server client.UseDefaultCredentials = false; // disable the use of authorization details "by default" client.Credentials = new NetworkCredential(" [email protected]domain.tld", "***password***"); // specifying the required details (username and password) for authorization on the SMTP server client.Send(message); // send message %>

Download an example of a finished form that demonstrates the use of this method: MailForm.aspx .

Example for "VisualBasic" language:

<% @Page Language="VB" Debug="true" %> <% @Import Namespace="System.Net.Mail" %> <% @Import Namespace="System.Net" %> <% Dim smtpMssg AS new MailMessage smtpMssg.From = New MailAddress ("[email protected]domain.tld", "SenderName") smtpMssg.To.Add(" [email protected]") " Add recipient's address smtpMssg.Subject = "Subject" "indicating the subject of the letter smtpMssg.Body = "Message body" " specifying the text (body) of the letter smtpMssg.BodyEncoding = System.Text.Encoding.UTF8 "indicating the encoding of the letter smtpMssg.IsBodyHtml = false " indication of the letter format (true - HTML, false - not HTML) Dim SmtpMail As New SmtpClient("smtp.site", 25) " declaration of a new object of type "SmtpClient" SmtpMail.DeliveryMethod = SmtpDeliveryMethod.Network " specifying the delivery method of the message SmtpMail.UseDefaultCredentials = False " disable the use of authorization details "by default" SmtpMail.EnableSsl = False "disables the need to use a secure connection to the server SmtpMail.Credentials = new NetworkCredential(" [email protected]domain.tld", "***password***") " specifying the necessary details (username and password) for authorization on the SMTP server SmtpMail.Send(smtpMssg) "sending a message %>

There is also a deprecated (and deprecated) SmtpMail class (using the namespace System.Web.Mail). Below is an example of its use for the "VisualBasic" language:

<% @Page Language="VB" Debug="true" %> <% @Import Namespace="System.Web" %> <% @Import Namespace="System.Web.Mail" %> <% Dim smtpMssg = new MailMessage " declaring a new object of type "MailMessage" smtpMssg.From = "SenderName domain.tld>" " specifying the name and address of the sender smtpMssg.to=" [email protected]domain.tld" " specifying the recipient's address smtpMssg.BodyFormat = MailFormat.Text "indicating the format of the letter smtpMssg.BodyEncoding = Encoding.UTF8 "indicating the encoding of the letter smtpMssg.Subject = "Subject" "indicating the subject of the letter smtpMssg.Body = "Message body" "indicating the text of the letter smtpMssg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1) " specifying the need for SMTP authorization on the server smtpMssg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", " [email protected]domain.tld") "login username smtpMssg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "***password***") "login password SmtpMail.SmtpServer = "smtp.site" "specify the SMTP server address SmtpMail.Send(smtpMssg) "sending a message %>

How to send emails using ASP?

Please note that when using our smtp server, authorization is required, so do not forget to add the appropriate access details to the script, for example:

<% iConfig = Server.CreateObject("CDO.Configuration") Set iConfig = Server.CreateObject("CDO.Configuration") With iConfig.Fields .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.сайт" .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 .Item("http://schemas.microsoft.com/cdo/configuration/languagecode") = "ru" .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "[email protected]domain.tld" .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password" .Update End With Set cdoMessage = CreateObject("CDO.Message") With cdoMessage Set .Configuration = iConfig .From = "Test user domain.tld>" .To = "User " .Subject = "Hello, User" .TextBody = "I write this message from ASP script." .Send End With Set cdoMessage = Nothing Set iConfig = Nothing %>

Some CMS already have built-in tools for sending correspondence or corresponding modules, let's consider the most popular of them.

Wordpress

For this CMS, there is a special module "WP Mail SMTP", an example of its configuration is described on the page of the module.

Pay attention to the illustration in the example, in the conditions of our mail system, port 25 is required and SSL encryption is not required.

Joomla

In the administration panel, go to the "General Settings" section, select the "Server" tab. In the " Mail" field, you need to select the sending method as " SMTP server", In the fields " SMTP server address"And" SMTP login"And" SMTP password"Enter the appropriate details of the mail system, for example: smtp.site, mailbox@your_domain and corresponding password.

Drupal

This CMS also has its own module for working with the SMTP server, based on PHPmailer. You can download this module on its page on the CMS Drupal website, the description of the module installation is available in the archive with it.

NetCat

This CMS does not have built-in functions for working with the SMTP server. To send mail using site scripts, we can offer to use our solution, to connect it, you must perform the following steps:

  • Download from our website an archive with the necessary files ( send_auth_mail.php and mail.inc.php) and unpack it on your computer;
  • Edit File send_auth_mail.php from the archive, specifying the connection details to the desired SMTP server in it:

    • $mail_smtpServer = "smtp.site"; (SMTP server address)
    • $mail_port = "25" ; (connection port)
    • $mail_username = "[email protected]" ; (username for authorization on the SMTP server)
    • $mail_password = "Password"; (password for authorization on the SMTP server)
  • Create a file backup domain.tld/www/netcat/admin/mail.inc.php;
  • download files send_auth_mail.php and mail.inc.php to the virtual site to the directory domain.tld/www/netcat/admin/

domain.tld should be replaced with your domain name.

The proposed solution works with all NetCat modules that use the standard functions of this CMS to send mail.

Sending mail via SMTP from a local server allows you to test sending messages from a site located on a local machine, or, more simply, a local server. To do this, you can use any mail service yandex, google or mail.ru.

First of all, SMTP (Simple Mail Transfer Protocol) is a widely used network protocol for transferring email over TCP/IP networks. And all popular mail services have such protocols.

With the advent of local servers, there is no need to choose a hosting provider in order to check the performance of individual scripts or cms systems, and even more so to pay for it. It is much easier to test everything on a computer, and then you can show everyone what you have “done”.

Many of these servers already have built-in software and the necessary functionality for working with mail, you just need to configure it correctly.

To check the performance of such mail, you need the bare minimum:

  • You can use Openserver as a local server.
  • And a simple script, the template of which can be taken below.
  • Any mail server can be used.

Sending php mail via SMTP script setup

A simple script template that can be copied below, or downloaded from hundreds of similar sites, needs to be edited.

First you need to change some values ​​in the script template.
to - change to the postal address where the mail message will be sent.
subject - letter subject
message - the message itself, or the body of the letter.

Example of my script:

Of course, you can expand the functionality a little and make the script issue a message about sending mail. Although it is the SMTP server that actually sends it, and the script only forms the message.

For example, you can write like this:

All substituted values ​​must be in quotes, otherwise the script will throw an error. After that, you can save the script to the local server folder.

For example: domains/send/index.php and start setting up sending mail via SMTP opensrver.

Mail will be sent to any address that will be specified in the script instead of the value ‘to’ , but the SMTP that will be specified in the openserver settings will process and send this mail.

  1. Starting the server
  2. Open the openserver module with the settings in the "mail" menu
  3. We fill in all the fields as shown in the picture, while we substitute the username, sender's e-mail and password from a real mailbox on Yandex.

We save the settings and after the server is restarted, you can refer to the script itself. From the "My Sites" folder, open the "send" folder, which contains the previously saved index.php script.

As soon as this script is called, a new browser window will display information that the script has worked.

After that, you should check your mailbox, the address of which was specified in the script, whether the letter has arrived.

If it is not there, then something is not configured correctly, or the letter ended up in the spam folder.

All other settings look similar, but just in case, additional information would not hurt.

In order to set up sending mail via SMTP mail ru, you just need to replace the Yandex settings with the mail ru settings.

And in order not to rebuild the local server every time, it's best to set up different openserver profiles. How to do this is shown in the video tutorial.

In this case, it will be enough just to load the profile and all the settings that were specified for this profile will be available after the server is restarted. Very handy and very easy to work.

For each profile (it does not matter at all what it will be called), its own settings must be made, unless of course it is planned to use scripts, cms-systems and applications that differ in their parameters.
If not, you can get by with the default settings.

As for SMTP mail ru, everything is identical here.

Yandex data changes to mail ru data

Sending mail via smtp google

In order to set up sending mail via Google SMTP, you should try a little.

Firstly, creating the profile itself in Google is more difficult than in other Yandex and mail ru systems.
Secondly, the system won't let the email through just like that, even if you provide your login details. To begin with, she will send a letter in which there will be all the necessary information about further instructions.

As for the rest for Google it is configured in the same way. All data changes.

To set up sending mail via other SMTP, all the data will be approximately the same.

If everything worked out for you and the letters reached the specified addressee, then everything was done correctly. It would be great if you share your experience in the comments.

SMTP mail settings

There are cases when site builders are faced with the problem of e-mail site work on CMS Joomla. For example, when sending a letter through the feedback form, errors of the following type may appear: "Could not instantiate mail function" or "Could not call mail function". It is also possible to send a letter without errors, but as a result it still does not reach the addressee.

Why do these mail problems occur? To answer this question, you need to go to the control panel along the following path: "System" - "General settings" - the "Server" tab - the "Mail settings" section.

Joomla CMS provides three mechanisms for sending emails: PHP Mail, Sendmail and SMTP. By default, PHP Mail is used, which often causes problems, which are mainly related to the settings of the hosting used.

Based on the foregoing, we conclude: either we turn to the hosting provider for help, or we use the method of sending Sendmail or SMTP emails. Let's focus on using SMTP.

SMTP mail settings

SMTP (English Simple Mail Transfer Protocol) is a network protocol used to transfer e-mail. To use SMTP, you must correctly set the settings of the specific mail server that will be used.

To see the SMTP settings, you need to select "SMTP" in the "Sending Method". Consider each setting of popular mail servers: Yandex, Mail, Gmail, Rambler and Yahoo.

SMTP settings for Yandex

  1. Site e-mail: mailbox on yandex.ru, for example: [email protected]
  2. SMTP security: SSL
  3. SMTP Server Port: 465
  4. SMTP username: yandex.ru mailbox login, for example: khasanov (without @yandex.ru)
  5. SMTP server: smtp.yandex.ru

SMTP settings for Mail

  1. Site e-mail: mailbox on mail.ru, for example: [email protected]
  2. Letter sender: the entry that will be displayed for the recipient in the "Sender" field
  3. Disable mailing: on / off. mass mailing functions
  4. Authorization on the SMTP server: Yes
  5. SMTP security: SSL
  6. SMTP Server Port: 465
  7. SMTP username: mailbox on mail.ru, for example: [email protected]
  8. Password for SMTP: mailbox password
  9. SMTP server: smtp.mail.ru

SMTP Settings for Gmail

  1. Website email: mailbox on gmail.com, for example: [email protected]
  2. Letter sender: the entry that will be displayed for the recipient in the "Sender" field
  3. Disable mailing: on / off. mass mailing functions
  4. Authorization on the SMTP server: Yes
  5. SMTP security: SSL
  6. SMTP Server Port: 465
  7. SMTP username: mailbox on gmail.com, for example: [email protected]
  8. Password for SMTP: mailbox password
  9. SMTP server: smtp.gmail.com

SMTP settings for Rambler

  1. Site e-mail: mailbox on rambler.ru, for example: [email protected]
  2. Letter sender: the entry that will be displayed for the recipient in the "Sender" field

In some cases, in order to test or diagnose the operation of mail, the post administrator needs to check the sending of letters through their Exchange servers (and not only) from certain hosts. In the event that the server does not require authorization (open-relay server), you can send mail. However, in most cases, mail servers require authorization to send mail. In this example, we will show how to perform AUTH LOGIN authentication on an SMTP server in the telnet console and send an email.

AUTH LOGIN- in Exchange terminology, this is basic authentication, when the user name and password are transmitted over the network in an encoded algorithm base64 form. On most internal Exchange servers, administrators do not disable BasicAuthentication. You can check its support in the settings of the receiving connector.

Note. Please note that when accessing the communication channel, an attacker can easily intercept and decrypt the user credentials encoded in Base64. Therefore, this authorization method is recommended to be used only in private corporate networks.

To authorize on the mail server using AUTH LOGIN, we need to convert the username and password of the user from which the letter will be sent to the Base64 format. This can be done using scripts or online services. I take advantage of the site https://www.base64encode.org/ .

Username: [email protected] contoso.com, in Base64 encoding it turned out: dGVzdHVzZXJAY29udG9zby5jb20=

Password: $ up3RsTr)ng- in Base64 JHVwM1JzVHIpbmc=

Now, in the command line, using Telnet, we connect to port 25 (SMTP) of our mail server (I will highlight the entered commands in blue):

telnet mail.contoso.com 25

If it's Exchange it will return something like;

Let's introduce ourselves:

ehlo sender.contoso.com

The server will return a list of supported authorization types and capabilities. As you can see, basic authorization (AUTH LOGIN) is on the list.

250-mail.contoso.com Hello
250-SIZE 36700160
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-AUTH LOGIN
250-8BITMIME
250-BINARYMIME
250 CHUNKING

AUTH LOGIN

The server should respond:
334 VXNlcm5hbWU6

Now paste in the Base64 username we encoded earlier:
dGVzdHVzZXJAY29udG9zby5jb20=

The server should respond:

334 UGFzc3dvcmQ6.

Now it's time to paste the password in Base64 format:
JHVwM1JzVHIpbmc=

If the username and password are correct, the server will respond.
235 2.7.0 Authentication successful

If not:

535 5.7.8 Error: authentication failed: UGFzc3dvcmQ6

Now you can fill in the standard fields of the letter:

mail from: [email protected]
250 2.1.0 Sender OK
rcpt to: [email protected]
250 2.1.5 Recipient OK
data
354 Start mail input; end with.
from: TestUserovich
to: TheAdmin< [email protected] >
Subject: Test BASE SMTP Authenticated via Telnet
This is test
.
250 2.6.0 Queued mail for delivery

QUIT

221 2.0.0 Closing connection.
Connection closed by foreign host.

That's all, the test letter should be successfully delivered to the recipient's mailbox.

Internet