Hi I'm new to php and swift mailer and I face this problem when I want to send the data from a bootstrap form. When I use a bootstrap form I get an empty mail, but when I use a simple html form I get a .txt file with the information from the form in the mail. Here's my index.php code:
<form id="main-contact-form" name="contact-form" method="post" action="contact_email.php" role="form">
<div class="row wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
<div class="col-sm-6">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Name" required="required">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email Address" required="required">
</div>
</div>
</div>
<div class="form-group">
<input type="text" name="subject" class="form-control" placeholder="Subject" required="required">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control" rows="4" placeholder="Enter your message" required="required"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn-submit">Send</button>
</div>
</form>
and here's my contact_email.php code
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$subject = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
$mesage = filter_var($_POST['messsage'], FILTER_SANITIZE_STRING);
$data = 'Name: '.$name.'<br />
Subject: '.$subject.'<br />
Email: '.$email.'<br />
Message: '.$mesage;
require_once 'swift/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('ssl://smtp.gmail.com', 465)
->setUsername('myemail#gmail.com')
->setPassword('********');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Email From My Website')
->setFrom(array('myemail#gmail.com' => 'website'))
->setTo(array('myemail#gmail.com'))
->setBody($data, text/html);
if ($mailer->send($message)) {
echo 'Mail sent successfully.';
} else {
echo 'I am sure, your configuration are not correct. :(';
}
?>
</body>
</html>
please tell me why I'm getting an empty mail and how to fix it and why I'm getting the information in a .txt file. thank you in advance :)
Related
I never used PHP before, but I need to set up an HTML form from to be sent from my website to my gmail account. Checked some tutorials and came up with this code. But for some reason it is not working. When I hit submit it goes to a "Page Not Found
Looks like you've followed a broken link or entered a URL that doesn't exist on this site." Not sure what am I doing wrong.
I have my HTML document, and I have my PHP document. Saved together.
<form action="contact.php" method="POST" name="form">
<div class="row">
<div class="col-12 col-md-6">
<div class="contact-form">
<label for="name">Please enter your name:</label>
<input name="name" type="text" class="form-control" placeholder="Harry Potter">
</div>
</div>
<div class="col-12 col-md-6">
<div class="contact-form">
<label for="mail">Your email address:</label>
<input name="mail" type="email" class="form-control" placeholder="harry#potter.com">
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="contact-form">
<label for="message">How can I help you?</label>
<textarea name="message" class="form-control" id="form-text" cols="30" rows="10" placeholder="Let's do some magic"></textarea>
</div>
</div>
</div>
<div id="submit" class="text-center">
<button name="submit" type="submit" class="btn mt-5 mx-auto">Submit</button>
</div>
</form>
<?php
if(isset($_POST["submit"])) {
$name=$_POST["name"];
$mailFrom=$_POST["mail"];
$message=$_POST["message"];
$mailTo = "myaddress#gmail.com";
$subject = "Form from my website";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from ".$name."\n\n".$message;
if(mail($mailTo, $subject, $txt, $headers)) {
echo "<h1>I recieved your email! Will be in touch with you soon.</h1>";
}
else{
echo "<h1>Something went wrong! Try again.</h1>";
}
header("Location: https://www.whatever.com");
}
?>
I am using Ampps on Mac and trying to send an email using php from a contact form however I do not receive the mail and when the form is submitted it redirects me to the file page resulting in a blank display
my form :
<form action="email.php" method="post">
<div class="col-md-6 w3_agileits_contact_left">
<span class="input input--akira">
<input class="input__field input__field--akira" type="text" id="input-22" name="Name" placeholder="" required="" />
<label class="input__label input__label--akira" for="input-22">
<span class="input__label-content input__label-content--akira">Your name</span>
</label>
</span>
<span class="input input--akira">
<input class="input__field input__field--akira" type="email" id="input-23" name="Email" placeholder="" required="" />
<label class="input__label input__label--akira" for="input-23">
<span class="input__label-content input__label-content--akira">Your email</span>
</label>
</span>
<span class="input input--akira">
<input class="input__field input__field--akira" type="text" id="input-24" name="Subject" placeholder="" required="" />
<label class="input__label input__label--akira" for="input-24">
<span class="input__label-content input__label-content--akira">Your subject</span>
</label>
</span>
</div>
<div class="col-md-6 w3_agileits_contact_right">
<div class="w3_agileits_contact_right1">
<textarea name="Message" id="Message" placeholder="Your comment here..." required=""></textarea>
</div>
<div class="w3_agileits_contact_right2">
<input type="submit" value="Send">
</div>
<div class="clearfix"> </div>
</div>
<div class="clearfix"> </div>
</form>
my File :
$email = $_POST['Email'];
$name = $_POST['Name'];
$to = "lucvanrooyen#gmail.com";
$subject = $_POST['Subject'];
$userMessage =$_POST['Message'];
$headers = "From: $email\n";
$message = "$name has sent you the following message.\n
Message: $userMessage";
$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: lucvanrooyen#gmail.com\n";
$usermessage = "Thank you for your enquiry we will be in touch.";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);
The code you have shown is not enough to debug your problem.
If you are using the mail method which is built into the standard library, read this answer to find out a few troubleshooting steps you could follow.
If you are and are still finding it difficult to use, then you could consider using one of the PHP emailing solutions like -
PHPMailer
SwiftMailer
Please check your form tag. there is no opening tag for form
<form action="email.php" method="post">
I am unable to configure my email address on the submit button in this piece of code:
<div id="contact" class="spacer">
<div class="container contactform center">
<h2 class="text-center wowload fadeInUp">Get in touch with us</h2>
<div class="row wowload fadeInLeftBig">
<div class="col-sm-6 col-sm-offset-3 col-xs-12">
<form class="cmxform" id="commentForm" method="post" action="email.php">
<fieldset>
<input type="text" placeholder="Subject" id="csubject" name="subject" minlength="2" type="text" required>
<input type="text" placeholder="Email" id="cemail" type="email" name="email" required>
<textarea rows="5" placeholder="Message" id="ccomment" name="comment" required></textarea>
<input class="submit btn btn-primary" type="submit" value="Submit">
</fieldset>
</form>
</div>
</div>
I tried linking it to a php page (email.php), but it says server error. I don't know what to do. Can someone please help me?
To send an email you need the mail() function http://php.net/manual/en/function.mail.php
With your code you need to name the submit like name="submit" then inside action.php file you have to write something like this (or inside the same php where you have this code):
if(isset($_POST["submit"]))
{
//Remember to do input validations
$subject = $_POST["subject"];
$from = $_POST["email"];
$comment = $_POST["comment"];
$body = "User with email $from send the next comment: $comment";
//then here you set your email
$to = "myemail#email.com"; //change this
mail($to,$subject,$body);
}
This is only to explain some basic usage and set the variables you need, I advice you read also PHPmailer class
I have the following code in my PHP form. How would i adjust it to include my Google Anayltic Code below?:
<?php
$sendMail="";
if (isset($_POST["sendemail"])){
$from = $_POST["email"];
$subject = $_POST["name"];
$message = $_POST["message"];
$message = wordwrap($message, 70);
$m=mail(get_post_meta( get_the_ID(), 'Seller Email', true ),$subject,$message,"From: $from\n");
if($m){
$sendMail="Message sent successfully.";
}else{
$sendMail="Message not sent.";
}
} ?>
<!--=== Contact Form ===-->
<form role="form" class="contactform" method="post">
<div class="form-group">
<label for="email">Your email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email" data-original-title="" title="">
</div>
<div class="form-group">
<label for="name">Your name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter name" data-original-title="" title="">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" id="message" name="message" placeholder="Information regarding property REF:<?php the_title();?>" style="height:100px;"></textarea>
</div>
<div class="form-group">
<button type="submit" name="sendemail" class="btn btn-lg btn-color">Send</button>
</div>
<?php if($sendMail!='') echo '<div class="form-group">'. $sendMail .'</div>';?>
</form>
</div><div style="clear:both;"></div>
My Google Analytics is the following, and I am unsure how I could add this onto the form on submission:
<script>
(function(i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function() {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-50671476-1', 'auto');
ga('send', 'pageview');
</script>
You can track form submission by creating the Goal in your Google Analytics Account.
Goal Type : Destination Url
DO one thing, On Successfully Submitted the form take the user to another page and show "Message Successfully Sent" there and set the Goal on that page so that you can track the users who successfully submitted the form.
Code :
<?php
$sendMail="";
if (isset($_POST["sendemail"])){
$from = $_POST["email"];
$subject = $_POST["name"];
$message = $_POST["message"];
$message = wordwrap($message, 70);
$m=mail(get_post_meta( get_the_ID(), 'Seller Email', true ),$subject,$message,"From: $from\n");
if($m){
header('location:successfull.html');
}else{
$sendMail="Message not sent.";
}
} ?>
<!--=== Contact Form ===-->
<form role="form" class="contactform" method="post">
<div class="form-group">
<label for="email">Your email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email" data-original-title="" title="">
</div>
<div class="form-group">
<label for="name">Your name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter name" data-original-title="" title="">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" id="message" name="message" placeholder="Information regarding property REF:<?php the_title();?>" style="height:100px;"></textarea>
</div>
<div class="form-group">
<button type="submit" name="sendemail" class="btn btn-lg btn-color">Send</button>
</div>
<?php if($sendMail!='') echo '<div class="form-group">'. $sendMail .'</div>';?>
</form>
</div><div style="clear:both;"></div>
successfull.html :
Message Successfully Sent
Steps you have to done in Google Analytics account to setup the Goal :
Admin -> View -> Goals -> New Goal -> Goal Setup -> Custom
Okay so I am fairly new to web designing. How do I get the contact form on my current theme to work? This is the current html.
I need to know how to code the PHP file; is this correct?
<div class="form row-fluid clearfix">
<div class="field span5">
<label>Your name:</label>
<input type="text" value="" class="req" placeholder="Placeholder text..." />
</div>
<div class="field span5">
<label>Your email:</label>
<input type="email" value="" class="req" />
</div>
<div class="clearfix"> </div>
<div class="field full">
<label>Your comment:</label>
<textarea class="span12" cols="2" rows="7"></textarea>
</div>
<button class="extruded"><span>Submit</span></button>
</div>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: iclear';
$to = 'sales#tangledindesign.com';
$subject = 'Hello';
?>
And how do I link the PHP file for that contact form?
Step 1
Wrap your fields with a form HTML element that has its action property set to your php processing page
Step 2
Name the form fields according to what the php file expects
Step 3
Add some validation
Step 4
Submit and test
Example
HTML
<form action="process.php" method="post">
First Name: <input type="text" name="first_name">
<input type="submit">
</form>
PHP
<?php
$first_name=$_POST["first_name"];
if($first_name=="John")
{
echo "Hi John!";
}
else
{
echo "Sorry Buddy, Don't really know you";
}
?>
Note
The reason why i did not provide you a full solution is that you mentioned you are a newbie in that programming, and it would be injustice to just solve your problem and not guide you how to do it
You need to wrap your HTML with the tag, and don't forget to get include the submit button:
<form action="process.php" method="post">
<div class="form row-fluid clearfix">
<input type="text" name="name">
<input type="text" name="email">
<input type="text" name="message">
<input type="submit" name="submit">
</div>
</form>
Then here is the php (process.php) file to get all values from your HTML form:
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: iclear';
$to = 'sales#tangledindesign.com';
$subject = 'Hello';
Hope this help.
Try this code
<?php
$toaddress ="youremail#domain.com" //change to your email address
$error ="";
if($_SERVER['REQUEST_METHOD']=="POST") {
$name=$_POST['name'] ;
$email=$_POST['email'] ;
$comment=$_POST['comment'] ;
if(!isset($name) || $name==""){
$error .="Please Enter your name <br/>";
}elseif (!isset($email) || $email==""){
$error .="Please Enter your email Address.<br/>";
}elseif(!isset($comment) || $comment==""){
$error .="Please Enter your Comments.<br/>";
}
if ($error ==""){
mail($toaddress,"Contact form",$comment)
}
}
?>
<?php echo $error ;?>
<form method='post' action='' enctype='multipart/form-data' id='news_form' name='post_form' >
<div class="form row-fluid clearfix">
<div class="field span5">
<label>Your name:</label>
<input name="name" type="text" value="" class="req" placeholder="Placeholder text..." />
</div>
<div class="field span5">
<label>Your email:</label>
<input type="email" value="" class="req" name="email" />
</div>
<div class="clearfix"> </div>
<div class="field full">
<label>Your comment:</label>
<textarea class="span12" cols="2" rows="7" name="comment"></textarea>
</div>
<input type="submit" value="submit" />
</div>
</form>