PHP PEAR Mail sends 2 emails [duplicate] - php

This question already has answers here:
Blank PHP Emails
(2 answers)
Closed 5 years ago.
This script works, but it sends a blank email when it is visited. Then a real one when submitted. I want to get rid of the first blank one it sends. This is mostly edited code I found here and another place I pieced together to work for my needs.
<?php
ini_set("include_path", '/home/user/php:' . ini_get("include_path") );
require_once "Mail.php";
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$contact = $_POST['contact'];
$comments = $_POST['comments'];
$from = "Info <info#domain.net>";
$to = "Info <info#domain.net>";
$subject = "Customer Contact Form";
$body = "If this works it will be edited a bit \n" . "First Name: " . $first_name . "\n" . "Last Name: " . $last_name . "\n" . "Email: " . $email . "\n" . "Address: " . $address . "\n" . "Phone: " . $phone . "\n" . "Please contact me by: " . $contact . "\n" . "Other Comments: " . $comments;
$host = "mail.domain.net";
$username = "info#domain.net";
$password = "emailpassword";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
//if (PEAR::isError($mail)) {
// echo("<p>" . $mail->getMessage() . "</p>");
//} else {
// echo("<p>Message successfully sent!</p>");
//}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Address: <input type="text" name="address"><br>
Phone: <input type="text" name="phone"><br>
Perferred Form of Contact: <input type="text" name="contact"><br>
Message:<br><textarea rows="5" name="comments" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

Because you don't check the form data!
Just use a simple if(isset($_POST['first_name'])) around your code for generating and sending the mail and it will just send an email if you've sent the form.

Related

Sending email with PHP from a GMAIL SMTP server / it does not send [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 2 years ago.
I would like to send an email from a PHP script. I installed an Apache server on Ubuntu machine, and that's all. I prepared an index.html file:
<?php
if(isset($_POST['submit'])){
ini_set("SMTP", "smtp.gmail.com");
ini_set("sendmail_from", "xxxx#gmail.com");
ini_set("smtp_port", "587");
$to = "MY_ADDRESS#gmail.com"; // this is your Email address
$from = "xxxx#gmail.com"; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['email'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
I allowed the usage of the gmail on gmail settings, but the email is not being sent. Any ideas why?
you can use those credentials for testing: tmail4824#gmail.com, password: XXX_12345. Email sanding from Python (using the same credentials) works.
?php
$name ="";
$Email = "";
$service = "";
$Subject = "";
$phone = "";
if(isset($_POST['item.name']))
{
$name = $_POST['name'];
$Email = $_POST['email'];
$Subject = $_POST['subject'];
$Msg = $_POST['message'];
$phone = $_POST['phone'];
$mailTo = "xxxxxxxxxxxxxx";
$headers = "from :".$Email;
$txt = "xxxx\n\n".$name.".\n\n".$Email.".\n\n".$phone.".\n\n".$Msg;
mail($mailTo, $Subject, $txt, $headers);
header("location: xxx.php?mailsend");
}
?>

Form waits for second submission to push through first submission

I'm new to development and I'm trying to build a contact form using php that pulls the info from a contact form. My contact form works and collects the data as needed, but when I click submit the first time I don't receive the information to my inbox. When I refresh the page, complete the form a second time and click submit once more, I receive both submissions to my inbox at the same time. It's as if the first submission waits to be pushed through by the second submission. Can anyone explain why this is happening and how to stop it?
I've seen a lot of posts regarding submissions happening twice but my issue is the first doesn't come through until the second is sent.
Here is my PHP
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$body = $_POST['body'];
$radio = $_POST['time'];
$mailTo = "myemail#gmail.com";
$headers = "From: " . $mailFrom;
$txt = "You have received an enquiry from: " . $name .
"\n\n Phone Number: " . $phone .
"\n\n Address: " . $address .
"\n\n Message: " . $body .
"\n\n Time to call: " . $radio;
mail($mailTo, $subject, $txt, $headers);
header("Location: index-contact.php?submission");
}
I'm testing on localhost8888 using MAMP Pro.
Here is HTML:
<form class="contact-form" action="contactform.php" method="post" id="form">
<label for="name">Full Name:</label><br>
<input type="text" id="name" name="name" placeholder="Please enter your name" required>
<label for="email">Email Address:</label><br>
<input type="email" id="email" name="email" placeholder="Please enter your email address" required>
<label for="phone">Phone Number:</label><br>
<input type="text" id="phone" name="phone" placeholder="Please enter your phone number" required></input>
<label for="subject">Subject</label><br>
<input type="text" id="subject" name="subject" placeholder="Please enter a subject" required></input>
<label for="address">Postcode:</label><br>
<input type="text" name="address" placeholder="Please enter your postcode"></input><br>
<label for="body">Comments:</label><br>
<textarea name="body" placeholder="Please enter your message"></textarea>
<input type="radio" id="morning" name="time" value="morning">Morning</input>
<input type="radio" id="afternoon" name="time" value="afternoon">Afternoon</input>
<input type="radio" id="anytime" name="time" value="anytime">Anytime</input>
<button type="submit" id="submit" name="submit">Send Message</button>
</form>
This happens because when you update the page, you will be resending the POST.
As this is a contact form, i suggest installing Google reCaptcha. Thus, it will be necessary to resolve the captcha and this will not happen.
There are other ways. One of them would be to store user data in a database and only authorize a sending by IP or session at a certain time.
Or you can just work with direct session and prevent this from happening, using the function in your code:
function prevent_multi_submit($type = "post", $excl = "contact") {
$string = "";
foreach ($_POST as $key => $val) {
// this test is to exclude a single variable, f.e. a captcha value
if ($key != $excl) {
$string .= $val;
}
}
if (isset($_SESSION['last'])) {
if ($_SESSION['last'] === md5($string)) {
return false;
} else {
$_SESSION['last'] = md5($string);
return true;
}
} else {
$_SESSION['last'] = md5($string);
return true;
}
}
Your code will look like this:
session_start();
if (isset($_POST['submit']) and prevent_multi_submit()) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$body = $_POST['body'];
$radio = $_POST['time'];
$mailTo = "myemail#gmail.com";
$headers = "From: " . $mailFrom;
$txt = "You have received an enquiry from: " . $name .
"\n\n Phone Number: " . $phone .
"\n\n Address: " . $address .
"\n\n Message: " . $body .
"\n\n Time to call: " . $radio;
mail($mailTo, $subject, $txt, $headers);
header("Location: index-contact.php?submission");
}
EDIT 1:
Or, add a simple parameter to the URL that redirects, so that a second POST is not authorized:
if (isset($_POST['submit']) and $_GET["sent"] != "ok") {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$body = $_POST['body'];
$radio = $_POST['time'];
$mailTo = "myemail#gmail.com";
$headers = "From: " . $mailFrom;
$txt = "You have received an enquiry from: " . $name .
"\n\n Phone Number: " . $phone .
"\n\n Address: " . $address .
"\n\n Message: " . $body .
"\n\n Time to call: " . $radio;
mail($mailTo, $subject, $txt, $headers);
header("Location: index-contact.php?sent=ok");
}
Read more about HTTP GET variables at PHP.net

How to submit a form to an email in php

When I tried to submit the form , It's been sent successfully , But I didn't get any data in the email . Here's my code :
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="email.php" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$to = "aryanpallive#gmail.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$to = "acbhaskar1#gmail.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
if(mail($to,$subject,$message,$headers)){
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
}
else{echo "mail has not been sent";}
}
?>
[![Output][1]][1]
[1]: https://i.stack.imgur.com/hgY1g.png
try it
$to = 'aryanpallive#gmail.com';
$subject = "Form submission";
$message = 'testing';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to,$subject,$message,$headers);
check your INBOX otherwise check your spam

Adding more boxes to fill

<?php
if(isset($_POST['submit'])) {
$to = "receiver#domain.com"; // this is IT Support Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name']; // sender's first name
$last_name = $_POST['last_name']; // sender's last name
$subject = "Form submission"; // Email confirmation submission
from sender
$subject2 = "Copy of your form submission"; // Email confirmation submission
from receiver
$message = $first_name . " " . $last_name . " requested:" . "\n\n" . $_POST['message']; // Email header
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to, $subject, $message, $headers);
mail($from, $subject2, $message2, $headers2); // sends a copy of the message to the sender
echo "Request Submitted Succesfully. Thank you " . $first_name . ", we will
fulfill your request shortly.";
// You can also use header('Location: thank_you.php'); to redirect to
another page.
}
?>
<!DOCTYPE html>
<head>
<title>Device and Set Up Request Form</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
How do I add more labels below the message for the users to fill? For example, right after "Message:," I want to add "Supervisor name" and more labels. In addition, those labels not only appear on the website, but also is wrapped and get emailed to the receiver.

how to send a form to an email [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
hey guys i'm trying to send a form to an email and that's my form :
<?php
error_reporting(0);
if(isset($_POST['submit'])){
$to = "elbiheiry2#gmail.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="form.php" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Image: <input type="file" name="image_name"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
every time i click submit it tells me the mail is sent but when i check my inbox i find nothing can anyone help ???
I would suggest you switch to PHPMailer, it will take care of email configurations for you.

Categories