R
R
realt2014-10-09 21:33:59
PHP
realt, 2014-10-09 21:33:59

How to send a form by mail?

I have 5 inputs. After filling them out and clicking the "send" button, the data of these forms should come to the mail. The topic must be fixed. The email in the code must also be fixed, to which the letter should actually come. How can this be implemented? Is it possible to have a code example?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Denis, 2014-10-10
@realt

Let's say your form is:

<html>
<head>
  <title>Форма обратной связи</title>
</head>
<body>
  <form method="post">
Ваше имя: <input type="text" name="name" placeholder="Иван">
Email для связи: <input type="email" name="email" placeholder="адрес электронной почты">
Ваше сообщение: <textarea name="message" rows="5"></textarea>
<input type="submit" value="отправить">
</form>
</body>
</html>

Then create php with this content:
<?php
// несколько получателей
$to  = '[email protected]' . ', ';  // обратите внимание на запятую
$to .= '[email protected]';

// тема письма
$subject = 'Письмо с моего сайта';

// текст письма
$message = 'Пользователь' . $_POST['name'] . ' отправил вам письмо:<br />' . $_POST['message'] . '<br />
Связяться с ним можно по email <a href="mailto:' . $_POST['email'] . '">' . $_POST['email'] . '</a>'
;

// Для отправки HTML-письма должен быть установлен заголовок Content-type
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

// Дополнительные заголовки
$headers .= 'To: Иван <[email protected]>' . "\r\n"; // Свое имя и email
$headers .= 'From: '  . $_POST['name'] . '<' . $_POST['email'] . '>' . "\r\n";


// Отправляем
mail($to, $subject, $message, $headers);
?>

Edits for realt:
Let's say your form is:
<html>
<head>
  <title>Форма обратной связи</title>
</head>
<body>
  <form method="post">
Инпут 1: <input type="text" name="input1" placeholder="Инпут 1">
Инпут 2: <input type="text" name="input2" placeholder="Инпут 2">
Инпут 3: <input type="text" name="input3" placeholder="Инпут 3">
Инпут 4: <input type="text" name="input4" placeholder="Инпут 4">
Инпут 5: <input type="text" name="input5" placeholder="Инпут 5">
<input type="submit" value="отправить">
</form>
</body>
</html>

Then create php with this content:
<?php
// несколько получателей
$to  = '[email protected]' . ', ';  // обратите внимание на запятую
$to .= '[email protected]';

// тема письма
$subject = 'Письмо с моего сайта';

// текст письма меняется он!!
$message = $_POST['input1'] . '<br />' . $_POST['input1'] . '<br />' . $_POST['input1'] . '<br />' . $_POST['input1'];

// Для отправки HTML-письма должен быть установлен заголовок Content-type
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

// Дополнительные заголовки
$headers .= 'To: Иван <[email protected]>' . "\r\n"; // Свое имя и email
$headers .= 'From: '  . $_POST['name'] . '<' . $_POST['email'] . '>' . "\r\n";


// Отправляем
mail($to, $subject, $message, $headers);
?>

S
svd71, 2014-10-09
@svd71

php.net/manual/ru/function.mail.php
and below the heading "Example #4 Sending an HTML message".
in the nested form, the action must be specified with the name of the server and protocol. At least get requests sent from me.

M
melnikov_m, 2014-10-10
@melnikov_m

I prefer to use PHPmailer library. No need to fool around with headers, a lot of goodies and easy to expand if necessary

require_once('../class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"
$body             = "body"
$body             = eregi_replace("[\]",'',$body);

$mail->AddReplyTo("[email protected]","First Last");
$mail->SetFrom('[email protected]', 'First Last');
$mail->AddReplyTo("[email protected]","First Last");

$address = "[email protected]";
$mail->AddAddress($address, "John Doe");

$mail->Subject    = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);


$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question