I am using PhpMailer to send emails. Inside my own script I want to parse some javascript inside the below if statement. Note that I have html enabled.
When the email is not sent I want to parse some javascript inside the .done function that the email is not sent. eg complete.html("Message Not Sent!"); When the email is sent, I want to show that the email is sent. How can I do that and where is better to do that, inside php file or inside the javascript?
var form = $('#contact');
form.submit(function(event) {
event.preventDefault();
var complete = $('#formAppend');
var $form = $(this);
var name = $("#fname").val();
var email = $("#email").val();
var Message = $("#msg").val();
var countryoption = $("#country").val();
$.ajax({
type: 'POST',
url: '../sendemail.php',
data: {
Name: name,
Email: email,
message: Message,
Country: countryoption
},
beforeSend: function() {
complete.html('Message is Sending...').fadeIn();
}
})
.done(function(data) {
//This should change depending on the php if statement at the bottom.
complete.html("Message Sent");
});
}); //end contact form
<form id="contact" method="post" action="#ss">
<div id="formAppend"></div>
<input type="text" id="fname" name="firstname" placeholder="Το όνομα σας.." required>
<input type="email" id="email" name="email" placeholder="Το email σας.." required>
<select id="country" required>
<option class="w3-center" value="" disabled selected value>-- Χώρα --</option>
<option value="Κυπρος">Κύπρος</option>
<option value="Ελλάδα">Ελλάδα</option>
<option value="Άλλο">Άλλη</option>
</select>
<textarea id="msg" name="message" placeholder="Γράψε το μήνυμα.." style="height:200px;max-height:400px;min-height:100px;" required></textarea>
<div id="op">
<button type="submit" style="" class="btn btn-primary img-responsive"> Αποστολή</button> </div>
</form>
PHP:
<?php
require 'PHPMailer-master/PHPMailerAutoload.php';
if(empty($_POST['Email'])){
$_POST['Email']= "";
}
if(empty($_POST['Name'])){
$_POST['Name']= "";
}
if(empty($_POST['message'])){
$_POST['message']= "";
}
if (isset($_POST["message"]) && !empty($_POST["message"])) {
$mymail=smtpmailer("[email protected]",$_POST['Email'], $_POST['Name'], $_POST['message']);
}else{
header('Location: http://webdominar.xyz'); exit();
}
function smtpmailer($to, $from, $from_name, $body) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = '';
$mail->Password = '';
$mail->SetFrom($from, $from_name);
$mail->Subject = "Εμβολιασμός Δέντρων ~ Φόρμα Επικοινωνίας ~ $body ";
$mail->CharSet = 'UTF-8';
$mail->isHTML(true); // Set email format to HTML
$Country = $_POST["Country"];
$mail->Body = "BLABLA ";//end email body
$mail->AddAddress($to);
//send the message, check for errors
if (!$mail->send()) { //Message not sent
echo "Mailer Error: " . $mail->ErrorInfo;
} else {//Message sent!
echo "Well done $from_name, your message has been sent!\nWe will reply to the following email: $from"
. "<br>Your Message: $body";
}
} //end function smtpmailer
?>