D
D
Dmitry2018-06-17 20:57:34
PHP
Dmitry, 2018-06-17 20:57:34

How to set up sending applications from the site to telegram?

Hello!
It is necessary that applications from the forms are sent both to mail and telegrams. It is sent to the mail, but not in telegrams. Please tell me what I did wrong:

<?php

$method = $_SERVER['REQUEST_METHOD'];

//Script Foreach
$c = true;
if ( $method === 'POST' ) {

  $project_name = "Название";
  $admin_email  = "[email protected]";
  $form_subject = "Заявка с сайта domen.ru";
  $token = "602671840:AAFlgv2E5VLXwhJ-Sl8T_bnarOKOl2EUpsQ";
  $chat_id = "-291884814";

  foreach ( $_POST as $key => $value ) {
    if ( $value != "" && $key != "project_name" && $key != "admin_email" && $key != "form_subject" ) {
      $message .= "
      " . ( ($c = !$c) ? '<tr>':'<tr style="background-color: #f8f8f8;">' ) . "
      <td style='padding: 10px; border: #e9e9e9 1px solid;'><b>$key</b></td>
      <td style='padding: 10px; border: #e9e9e9 1px solid;'>$value</td>
    </tr>
    ";
  }
}

$sendToTelegram = fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$message}","r");

}

$message = "<table style='width: 100%;'>$message</table>";

function adopt($text) {
  return '=?UTF-8?B?'.Base64_encode($text).'?=';
}

$headers = "MIME-Version: 1.0" . PHP_EOL .
"Content-Type: text/html; charset=utf-8" . PHP_EOL .
'From: '.adopt($project_name).' <'.$admin_email.'>' . PHP_EOL .
'Reply-To: '.$admin_email.'' . PHP_EOL;

mail($admin_email, adopt($form_subject), $message, $headers );

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2018-06-18
@bsloungespa

The problem was with the $message variable. Apparently telegram cannot display the table.
Added another $txt variable:

foreach ( $_POST as $key => $value ) {
    if ( $value != "" && $key != "project_name" && $key != "admin_email" && $key != "form_subject" ) {
      $message .= "
      " . ( ($c = !$c) ? '<tr>':'<tr style="background-color: #f8f8f8;">' ) . "
      <td style='padding: 10px; border: #e9e9e9 1px solid;'><b>$key</b></td>
      <td style='padding: 10px; border: #e9e9e9 1px solid;'>$value</td>
    		</tr>
    		";
        $txt .= "<b>".$key."</b> ".$value."%0A";
  }
}

$sendToTelegram = fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$txt}","r");

A
Alexey Tutubalin, 2018-06-17
@Kennius

$sendToTelegram = file_get_contents("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$message}&amp;parse_mode=HTML&amp;disable_web_page_preview=false&amp;disable_notification=false");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question