Cześć. Borykam się z tym od kilku dni i nie wiem jak dodać dane z formularza. które poda osoba odwiedzająca naszą stronę czyli: imię i nazwisko, e-maila, czy numer telefonu w treści wiadomości( w jej nagłówku) Z góry Wam dziękuję za pomoc.
Problem rozwiązanyDodałem taką linijke do kodu poniżej:
$mail->Body = '<h1 align=centre>Imię i nazwisko: '.$_POST['userName'].'<br>Telefon: '.$_POST['userPhone'].'<br>E-mail: '.$_POST['userEmail'].'<br><br>'.$_POST['userMessage'];
Ale porady odnośnie kodu i jego poprawy mile widziane.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if(!empty($_POST["send"])) { require_once ('vendor/autoload.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->Port = 465;
$mail->Username = "***********";
$mail->Password = "************";
$mail->Mailer = "smtp";
$mail->CharSet = "UTF-8";
if (isset($_POST["userEmail"])) { $userEmail = $_POST["userEmail"];
}
if (isset($_POST["userName"])) { $userName = $_POST["userName"];
}
if (isset($_POST["userPhone"])) { $userPhone = $_POST["userPhone"];
}
if (isset($_POST["subject"])) { $subject = $_POST["subject"];
}
if (isset($_POST["userMessage"])) { $message = $_POST["userMessage"];
}
$mail->SetFrom("**********");
$mail->AddReplyTo($userEmail, $userName);
$mail->AddAddress("********l");
$mail->Subject = $subject;
$mail->WordWrap = 80;
$mail->MsgHTML($message);
$mail->IsHTML(true);
$mail->SMTPSecure = 'ssl';
$mail->Host = '***********';
if (! empty($_FILES['attachment'])) { $count = count($_FILES['attachment']['name']); if ($count > 0) {
for ($i = 0; $i < $count; $i ++) {
if (! empty($_FILES["attachment"]["name"])) {
$tempFileName = $_FILES["attachment"]["tmp_name"][$i];
$fileName = $_FILES["attachment"]["name"][$i];
$mail->AddAttachment($tempFileName, $fileName);
}
}
}
}
if (! $mail->Send()) {
$message = "Ups :( Error";
$type = "error";
} else {
$message = "Successfull";
$type = "success";
}
}
?>
<script type="text/javascript">
function validate() {
var valid = true;
$(".info").html("");
var userName = document.forms["mailForm"]["userName"].value;
var userPhone = document.forms["mailForm"]["userPhone"].value;
var userEmail = document.forms["mailForm"]["userEmail"].value;
var subject = document.forms["mailForm"]["subject"].value;
var userMessage = document.forms["mailForm"]["userMessage"].value;
if (userName == "") {
$("#userName-info").html("(required)");
$("#userName").css('background-color', '#FFFFDF');
valid = false;
}
if (userPhone == "") {
$("#userPhone-info").html("(required)");
$("#userPhone").css('background-color', '#FFFFDF');
valid = false;
}
if (userEmail == "") {
$("#userEmail-info").html("(required)");
$("#userEmail").css('background-color', '#FFFFDF');
valid = false;
}
if (!userEmail.match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/))
{
$("#userEmail-info").html("(invalid)");
$("#userEmail").css('background-color', '#FFFFDF');
valid = false;
}
if (subject == "") {
$("#subject-info").html("(required)");
$("#subject").css('background-color', '#FFFFDF');
valid = false;
}
if (userMessage == "") {
$("#userMessage-info").html("(required)");
$("#userMessage").css('background-color', '#FFFFDF');
valid = false;
}
return valid;
}
function addMoreAttachment() {
$(".attachment-row:last").clone().insertAfter(".attachment-row:last");
$(".attachment-row:last").find("input").val("");
}
</script>
<h1>PHP Contact Form with Add More File Attachment Option
</h1>
<div class="attachment-form-container"> <form name="mailForm" id="mailForm" method="post" action="" enctype="multipart/form-data" onsubmit="return validate()">
type="text" class="input-field" name="userName"
id="userName" />
class="input-field" name="userPhone" id="userPhone" />
class="input-field" name="userEmail" id="userEmail" />
class="input-field" name="subject" id="subject" />
<textarea name="userMessage" id="userMessage" class="input-field" id="userMessage" cols="60"
<div class="attachment-row"> <input type="file" class="input-field" name="attachment[]">
<div onClick="addMoreAttachment();" class="icon-add-more-attachemnt"
title="Add More Attachments">
<img src="icon-add-more-attachment.png" alt="Add More Attachments">
<input type="submit" name="send" class="btn-submit" value="SEND" />
<?php
if (! empty($message)) {
?>
<p class='<?php echo $type; ?>Message'>
<?php echo $message; ?></p> <?php
}
?>
Ten post edytował Qudamah 21.09.2019, 18:22:26