HTML/PHP to send email from form [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I've been working on this for some time and am quite new to php. I'm having trouble getting this to send. A second set of eyes on this code would be most helpful:
<?php
if(isset($_POST['submit'])){
$to = "myEmail"; // this is your Email address
$from = $_POST['emailAddress']; // this is the sender's Email address
$fullName = $_POST['fullName'];
$subject = "Form submission";
$message = $fullName . " wrote the following:" . "\n\n" . $_POST['comment'];
$message2 = "Here is a copy of your message " . $fullName . "\n\n" . $_POST['comment'];
$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 " . $fullName . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<form method="post" action="contact.php">
<div class="form-group">
<label for="fullName">Name</label>
<input type="text" class="form-control" id="fullName" name="fullName" placeholder="Jane Doe">
<label for="emailAddress">Email</label>
<input type="email" class="form-control" id="emailAddress" name="emailAddress" placeholder="jane.doe#example.com">
<label for="comment">Comment</label>
<textarea class="form-control" rows="3" name="comment" placeholder="Comment"></textarea>
<button name="submit" type="submit" class="btn">Submit</button>
</div>
</form>
Many thanks!

I would suggest to use PHPMailer. It is an open-source project available on GitHub : PHPMailer - GitHub
This class permits you to exploit the SMTP services of the most famous mailing platform to get a fast system to send mail using PHP.
It's really simple to set up a code with this class, starting from an HTML form :
<!DOCTYPE html>
<html>
<body>
<form method="post">
<input type="email" name="from" placeholder="From">
<input type="email" name="to" placeholder="To">
<input type="text" name="subject" placeholder="Subject">
<textearea name="content"></textarea>
</form>
</body>
</html>
<?php
require_once('PHPMailer_5.2.4\class.phpmailer.php');
if(isset($_POST["submit"])){
$username = 'YOUR_GMAIL_ACCOUNT';
$password = 'YOUR_ACCOUNT_PASSWORD';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
//$mail->addAttachment($_FILES["image"]["name"]);
$mail->IsHTML(true);
$mail->Username = $username;
$mail->Password = $password;
$mail->SetFrom($_POST["from"]);
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["content"];
$mail->AddAddress($to);
if(!$mail->Send())
{
echo "Mailer error : " . $mail->ErrorInfo . "<br>";
}
}
?>
As you can see in the PHP code, I'm using Gmail SMTP service to send this mail. Note that if you want to use other services you have to change the SMTP server.Furthermore, you need to login into your email service to get an access to the SMTP service, and you need really often to enable the possibility of accessing your mailing account by third part applications too.
In some cases, the SMTP server won't accept TLS or SSL encryption.
Is it possible to add attachment adding enctype="multipart/data" attribute to your form tag and getting the uploaded file through the $_FILES array.
I hope this is going to help you!

In your code parameters of second mail function are not completed you didn't define value of subject2 I think your first message send in the right way but the second will not

Related

Php form instead send mail download on computer

<div class="contact_form clearfix" id="Contact">
<h2>Hello... You can send me message to my universe here.</h2>
<img src="img/planeta1.png" alt="">
<form class="clearfix spaceForm" action="contactform.php" metod="post" >
<label for="name">Your name:</label>
<input type="text" name="name" id="name" placeholder="Jon Doe" required>
<label for="email">Your email:</label>
<input type="text" name="email" id="email" placeholder="something#mama.com" required>
<label for="subject">Subject</label>
<input type="text" name="subject" id="subject" required>
<label for="message">Your message:</label>
<textarea name="message" id="message" required></textarea>
<button type="submit" name="submit">Send mail</button>
</div>
and php code here...
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mailTo = "pisitenam#sammostalnisindikatstark.org.rs";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: index.html");
}
?>
My contact form instead to send message download on computer as php file. I uploaded my site to netfly but stil doesnt work.
Can anybody help and give me a hint where is problem?
On XAMPP im getting blank page and mail is not sent. When I uploaded site on netfly site works fine but contact from when click submit start download php file where is code writen for controling contact form.5 day im trying to find solution for this problem and im geting tired :D So if anybody can help...
you are having some spell mistake in your form tag, first of all correct the method spell in your code as it is not correct so it can't redirect and post your data to contact form.
mail library contains various function.
for example:
<?php require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'your_smtp_domain.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('john#example.net', 'John doe'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
this can works for you if you use library.

How to create email form PHP (HTML5) [duplicate]

This question already has answers here:
How can I send an email using PHP?
(20 answers)
Closed 6 years ago.
I'm a little bit new with with php and i just want to ask how can i make the the "Send Message" button send the inputted information on the form i created to my email.
Here's the code:
<section id="three">
<h2>Email Me!</h2>
<p>You will receive a reply within 24-48 hours.</p>
<div class="row">
<div class="8u 12u$(small)">
<form method="post" action="MAILTO:sample#email.com">
<div class="row uniform 50%">
<div class="6u 12u$(xsmall)"><input type="text" name="name" id="name" placeholder="Name" /></div>
<div class="6u$ 12u$(xsmall)"><input type="email" name="email" id="email" placeholder="Email" /></div>
<div class="12u$"><textarea name="message" id="message" placeholder="Message" rows="4"></textarea></div>
</div>
</form>
<ul class="actions">
<li><input type="submit" value="Send Message" /></li>
</ul>
</div>
<div class="4u$ 12u$(small)">
<ul class="labeled-icons">
<li>
<h3 class="icon fa-home"><span class="label">Address</span></h3>
1234 Somewhere Rd.<br />
Nashville, TN 00000<br />
United States
</li>
<li>
<h3 class="icon fa-mobile"><span class="label">Phone</span></h3>
000-000-0000
</li>
<li>
<h3 class="icon fa-envelope-o"><span class="label">Email</span></h3>
hello#untitled.tld
</li>
</ul>
</div>
</div>
</section>
Thanks!
I'm not quite sure but email from php server almost all of them ends up in spam folder (trust issues by mail provider). But if you're interested, you can send mail via email function:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
source: PHPDocs
Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
What I recomend using is Mail sending servive like SendGdrid or MailChimp, those are easy to use and have pretty simpe API to work with. Free plan has a lot to offer, And you can send plain html through their api and it will be fine.
<?php
if(isset($_POST['submit'])) // on submit click no need to action of the form
{
$name = $_POST['name'];
$email = $_POST['email'];
$to = "somebody#example.com";
$subject = "My subject";
$body = "name:" . $name . "Email:" . $email;
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
mail($to,$subject,$body,$headers);
}
?>
I would recommend using PHPMailer to send email from PHP. Here's the steps to accomplish this.
Go to the Github repository.
Download the ZIP.
Extract it in your public_html directory.
include '/path/to/PHPMailer/PHPMailerAutoload.php'; at the top of your PHP script.
Get the values from the HTML form like you normally would.
Here's an example...
index.html
<form action="index.php" method="post">
<input type="email" name="email">
<input type="text" name="name">
<input type="text" name="subject">
<input type="text" name="message">
</form>
index.php
include '/path/to/PHPMailer/PHPMailerAutoload.php';
$email = $_POST['email'];
$name = $_POST['name'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'localhost'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, "ssl" also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('your email', 'your name'); // from
$mail->addAddress($email, $name); // to
$mail->isHTML(true); // if html
$mail->Subject = $subject;
$mail->Body = $message; //HTML
if($mail->send()){
echo 'Mail sent!';
}
else {
echo 'Mail failed!';
}

My website doesn't send the mail even though it works perfectly on wamp

Here is my website: www.accurateaccountsinc.tk
The problem is that the website just loads and doesn't do anything.
Everything works properly in localhost/wamp but it doesn't work on the actual site when running the task.
here's the code for the form
<form method="POST" action="index.php">
<input type="text" required name="name" id="name" placeholder="Enter Your Name" />
<input type="text" required name="email" id="email" placeholder="Enter Your Email" />
<input type="text" required name="phone" id="phone" placeholder="Phone Number" />
<textarea name="message" required id="message" placeholder="Enter Your Message"></textarea>
<input type="submit" name="mailed" value="Submit" />
</form>
Here's the php:
<?php
include 'dbconn.php';
if(isset($_POST['mailed'])){
$name = mysqli_real_escape_string($con,$_POST['name']);
$emailadd = mysqli_real_escape_string($con,$_POST['email']);
$contact = mysqli_real_escape_string($con,$_POST['phone']);
$entrymessage = mysqli_real_escape_string($con,$_POST['message']);
include "class.phpmailer.php";
include "class.smtp.php";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMPTDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = "email here";
$mail->Password = "password here";
$mail->SetFrom("email here", 'Website Entry');
$subject = "Client Entry";
$message = "<br>Client Name: " . $name;
$message .= "<br>Email Address: " . $emailadd;
$message .= "<br>Contact Number: " . $contact;
$message .= "<br>Message: " . $entrymessage;
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AddAddress("email here", $name);
if($mail->Send()){
unset($_POST['mailed']);
echo "<script>window.open('index.php','_self')</script>";
}
}
?>
if there's any more info you'd like to know please do ask..
also note that I only used the class.phpmailer.php and class.smtp.php to minimize the work needed if that's related to the problem..
Typo: $mail->SMPTDebug should be $mail->SMTPDebug, and you should set it to 2 for useful feedback.
You're also not displaying any errors that it may have caused, so echo $mail->ErrorInfo; if sending fails.
You can't send through gmail on port 25, only to gmail. Stick to tls on Port 587, like all the examples and docs say. I don't know where you got your code, but it looks like you used an old example - you should base it on the examples provided with PHPMailer, particularly the ones for gmail.

php email sending not working [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
This is my php code to send email. I got the massage 'Message was sent, you can send another one' But email is not sending.
<h4>Please fill out the following form and we will be in touch with you soon.</h4>
<form action="mytest.php" method="post" id="contactform">
<ol>
<li>
<label for="name">Your Name <span class="red">*</span></label>
<input id="name" name="name" class="text" />
</li>
<li>
<label for="email">Your email <span class="red">*</span></label>
<input id="email" name="email" class="text" />
</li>
<li>
<label for="subject">Subject</label>
<input id="subject" name="subject" class="text" />
</li>
<li>
<label for="message">Message <span class="red">*</span></label>
<textarea id="message" name="message" rows="6" cols="50"></textarea>
</li>
<li class="buttons">
<input type="image" name="imageField" id="imageField" src="images/send.gif" class="send" />
<div class="clr"></div>
</li>
</ol>
</form>
<?php
if(!$_POST) exit;
$email = $_POST['email'];
$errors=0;
if($errors==1) echo $error;
else{
$email_from = $_POST['email'];
$email_from = "from#gmail.com";
$headers = "From: " . strip_tags( $_POST['name'] ) . "\r\n";
$mail_to_send_to = "to#gmail.com";
$your_feedbackmail = "from#gmail.com";
$sendflag = 'send';
if ( $sendflag == "send" )
{
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $your_feedbackmail" . "\r\n" . "Reply-To: $email" . "\r\n" ;
$a = mail( $mail_to_send_to, "Feedback Form Results", $message, $headers );
if ($a)
{
print("Message was sent, you can send another one");
} else {
print("Message wasn't sent, please check that you have changed emails in the bottom");
}
}
}
?>
I'm Using Cpanel to host my web site. Is there any special configurations to do this? I'm new to php. Please help me.
mail function doesn't provide authentication functionality. Your have to use Mail class from Mail Pear package. See here for an example:
example
I am assuming you are on shared hosting based on the fact that you are using Cpanel, some shared hosting solutions don't play nicely with php's mail() function, I have found that using phpmailer https://github.com/PHPMailer/PHPMailer works better and provides more functionality
to use phpmailer :
download from the link, place the files in your a folder accessible to your web application.
code :
$email_from = $_POST['email'];
$email_from = "from#gmail.com"; // this overwrites the $_POST['email'] value, check this
$email_from_name = "Nishanthi";
$gmailUsername = "from#gmail.com";
$gmailPassword = "mysecretpassword";
$mail_to_send_to = "to#gmail.com";
$your_feedbackmail = "from#gmail.com";
$emailSubject = "Place Subject Here";
$emailContent = "This message can contain <b>HTML</b>";
require '[path_to_your_phpmailer_files]/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2; //Enable SMTP debugging
$mail->Debugoutput = 'html'; //Ask for HTML-friendly debug output
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $gmailUsername; // SMTP username
$mail->Password = $gmailPassword; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom($email_from, $email_from_name);
$mail->addAddress($mail_to_send_to);
$mail->addReplyTo($your_feedbackmail);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $emailSubject;
$mail->Body = $emailContent;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message was sent, you can send another one';
}
?>

Posting form to custom PHP script within Wordpress

I added this contact form to a custom page template in wordpress:
<form id="contact_form" action="inc/mail.php" method="POST">
<input id="name" name="name" placeholder="Name">
<input id="email" name="email" placeholder="Email">
<input id="phone" name="phone" placeholder="Phone Number">
<textarea id="comments" name="comments" placeholder="Comments / Questions"></textarea>
<input class="button" type="submit" id="submit" value="Submit">
</form>
And I created a PHP page to handle the form once posted:
<?php
// Gather form data
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
$client = "";
$clientname = "";
$password = "";
if($name && $email && $comments) {
// Create instance of PHP mailer
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = $client;
$mail->Password = $password;
$mail->From = $client;
$mail->SetFrom($email, $name);
$mail->AddAddress($client, $clientname);
$mail->IsHTML(false);
$formcontent="From: \n $name \n\n Email: \n $email \n\n Phone: \n $phone \n\n Comments: \n $comments";
$mail->Subject = "Miss Mary's Inquiry";
$mail->Body = $formcontent;
if(!$mail->Send()){
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
}
?>
The form and handler work perfectly outside of Wordpress, but I think Wordpress is preventing the PHP script (mail.php) from being accessed. Is there a way to post the form to my PHP script without Wordpress / htaccess interfering?
Thanks!
If you want to send mails in wordpress try using plugins. There are tons of wordpress plugins for that.
Conatact form 7 is the one which I like the most. Still you can develop your own but create it as plugin. You can convert the above code into a shortcode plugin. Check this link
wp_mail( $to, $subject, $message, $headers, $attachments )
This function is normally used for sending mails in wordpress

Categories