Wpis z mikrobloga

Hej Mireczki. Mam taki skrypt PHP do obsługi formularza.

php
/*
* CONFIGURE EVERYTHING HERE
*/

// an email address that will be in the From field of the email.
$from = 'automat@serwer.com.pl';

// an email address that will receive the email with the output of the form
$sendTo = 'admin@serwer.com.pl';

// subject of the email
$subject = 'Nowa wiadomosc z serwisu serwer.com.pl';

// form field names and their translations.
// array variable name = Text to appear in the email
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Temat', 'email' => 'Email', 'message' => 'Wiadomość');

// message that will be displayed when everything is OK :)
$okMessage = 'Dziękujemy! ODPOWIEMY NA TWOJĄ WIADOMOŚĆ NAJSZYBCIEJ JAK TO BĘDZIE MOŻLIWE.';

// If something goes wrong, we will display this message.
$errorMessage = 'Wystąpił błąd. Proszę spróbować później.';

/*
* LET'S DO THE SENDING
*/

// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);

try
{

if(count($_POST) == 0) throw new \Exception('Form is empty');

$emailText = "Nowa wiadomość z serwisu serwer.com.pl\n=============================\n";

foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}

// All the neccessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);

// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));

$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);

header('Content-Type: application/json');

echo $encoded;
}
// else just display the message
else {
echo $responseArray['message'];
}

Działa dobrze. Ale chciałbym go wzbogacić o funkcje wysyłania wiadomości do konkretnej osoby.

Do kogo wysłać

Sprzedaż - stefan@serwer.com.pl
Marketing - jolka@serwer.com.pl
Grafik- piotr.j@serwer.com.pl
Kadry - Iwona - a.b@serwer.com.pl

W formularzu wybieram konkretną osobę i wiadomość do niej wędruje.

#php #html #html5 #programowanie #webdev
  • 11
  • Odpowiedz
  • Otrzymuj powiadomienia
    o nowych komentarzach

Sprzedaż - stefan@serwer.com.pl
Marketing - jolka@serwer.com.pl
Grafik- piotr.j@serwer.com.pl
Kadry - Iwona - a.b@serwer.com.pl

I w skrypcie:
  • Odpowiedz
@pawel86: dodaj atrybut name na select'cie z listą maili, optionom daj value z samymi mailami i później w kodzie $sendTo = $_POST['nazwa_klucza_z_name'];
To tak najprościej, ale jak ktoś puknie curlem to będzie mógł wysłać maila na dowolny adres, dobrze by było walidować po stronie serwera czy przesyłana postem wartość jest na liście poprawnych adresów.
  • Odpowiedz
@Benzen: Dzięki. A może lepiej ukryć adres mailowy. Zabezpieczenie przed spam-bootami.

Sprzedaż - Katarzyna Jakas
Grafik- Stefan Jakis
i po stronie php obsłużyć value =1,2
  • Odpowiedz
ogólnie funkcja mail() jest niezalecana i przez niektóre hostingi wręcz blokowana, po to są narzędzia aby wymusić pewny poziom bezpieczeństwa, nauczysz się raz to będziesz umiał robić różne wersje contact form co procentuje na przyszłość
  • Odpowiedz