I made a contact form to send information to my email, and whenever I submit the form, I get redirected to my php file (mywebsite.com/contact.php) and am shown a Internal Server Error (the 500 kind). What did I do wrong? Could it be because I have my html and php code in different files? I included my contact form as well just in case it is relevant.
<?php
if(isset($_POST['submit']))
{
$message=
'Full Name: '.$_POST['name'].'<br/>
Comments: '.$_POST['comments'].'<br/>
Email: '.$_POST['email'].'
';
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mail.yahoo.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'aroncea#yahoo.com'; // SMTP username
$mail->Password = 'letmejusteditthisout'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom($_POST['email'], $_POST['name']);
$mail->addReplyTo($_POST['email'], $_POST['name']);
$mail->addAddress('aroncea#yahoo.com'); // Add a recipient
$result = $mail->Send();
$message = $result ? 'Successfully sent!' : 'Sending Failed!';
unset($mail);
$mail->Subject = "New Form Submission";
$mail->MsgHTML($message);
?>
<!-- Container (Contact Section) -->
<form action="newcontact.php" method="POST" name='contactform' id='contactform' enctype="text/plain">
<div id="contact" class="container-fluid bg-grey">
<h2 class="text-center">CONTACT</h2>
<div class="row">
<div class="col-sm-5">
<p>Contact us and we'll get back to you within 24 hours. </p>
<p><span class="glyphicon glyphicon-map-marker"></span> Burlington, ON</p>
<p><span class="glyphicon glyphicon-phone"></span> 289-230-4510</p>
<p><span class="glyphicon glyphicon-envelope"></span> aroncea#yahoo.com</p>
</div>
<div class="col-sm-7 slideanim">
<div class="row">
<div class="col-sm-6 form-group">
<input class="form-control" id="name" name="name" placeholder="Name" type="text" required>
</div>
<div class="col-sm-6 form-group">
<input class="form-control" id="email" name="email" placeholder="Email" type="email" required>
</div>
</div>
<textarea class="form-control" id="comments" name="comments" placeholder="Comment (not required)" rows="3"></textarea><br>
<div class="row">
<div class="col-sm-12 form-group">
<input type="submit" name="submit" value="Submit" />
</div>
</div>
</div>
</div>
You are missing a } in your PHP-File. (You open a { after the if-statement but never close it.)
With 500 Errors, it usually helps to check the servers error-log.
assign your message and Subject before $mail->Send(); and close }.
try this code.
if(isset($_POST['submit']))
{
$message=
'Full Name: '.$_POST['name'].'<br/>
Comments: '.$_POST['comments'].'<br/>
Email: '.$_POST['email'].'
';
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mail.yahoo.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'aroncea#yahoo.com'; // SMTP username
$mail->Password = 'letmejusteditthisout'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom($_POST['email'], $_POST['name']);
$mail->addReplyTo($_POST['email'], $_POST['name']);
$mail->addAddress('aroncea#yahoo.com'); // Add a recipient
$mail->Subject = "New Form Submission";
$mail->MsgHTML($message);
$result = $mail->Send();
$message = $result ? 'Successfully sent!' : 'Sending Failed!';
unset($mail);
}
i hope it will be helpful
Related
<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.
Sorry for my bad English :(
I got a big problem here, I have html homepage with contact form in there. I would like to have this form send with PHPMailer, when I send a message I get this but without text :/ I see only the sample text like "Here is the subject". Can someone help me please?
here is the phpmailer.php code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.test.de'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'MY EMAIL'; // SMTP username
$mail->Password = 'MY PASSWORT!'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('MY EMAIL');
$mail->addAddress('MY EMAIL'); // Add a recipient
//Content
$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>';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
MY EMAIL ist just for secure here
And here ist the code from the html contact form:
<!-- Contact Form -->
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form action="phpmailer_new.php" method="POST">
<div class="form-group">
<input type="text" name="mail" class="form-control" placeholder="Email Address">
</div>
<div class="form-group">
<input type="text" name="subject" class="form-control" placeholder="Subject">
</div>
<div class="form-group">
<textarea class="form-control" name="text" rows="3" placeholder="Your Message"></textarea>
<button class="btn btn-default" type="submit">Send Message</button>
</div>
</form>
</div>
</div>
<!-- End Contact Form -->
Cheers Yves
You need to add your variables $_POST in your PHPmailer script :
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.test.de'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'MY EMAIL'; // SMTP username
$mail->Password = 'MY PASSWORT!'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('MY EMAIL');
$mail->addAddress($_POST['mail']); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $_POST['subject'];
$mail->Body = $_POST['text'];
$mail->send();
//echo 'Message has been sent';
header('Location: http://www.example.com/contact.php');
exit();
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
I am trying to get PHPMailer to work to send mail from form data(obviously).
ISSUE:
When submitting form, I am getting sent to a blank page. No errors are being reported. I have followed as many stackoverflow threads surrounding this issue as I could find, and no solutions in those threads resolved the issue for myself.
Thanks
ERRORS:
Fixed all errors aside from: 2016-03-26 22:10:43 SMTP ERROR: Failed to connect to server: Connection refused (111) 2016-03-26 22:10:43 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
PHP:
<?php
$message=
'First Name: '.$_POST['first'].'<br />
Last Name: '.$_POST['last'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['email'].'<br />
Message: '.$_POST['message'].'
';
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->CharSet = 'UTF-8';
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "tls"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 587;
// Authentication
$mail->Username = "xxxx#gmail.com"; // Your full Gmail address
$mail->Password = "xxxx"; // Your Gmail password
// Compose
$mail->SetFrom($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']);
$mail->AddReplyTo($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']);
$mail->Subject = "New Contact Form Enquiry"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress("xxxx#gmail.com", "xxxx"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
}
?>
HTML:
<form class="form-inline" name="contactform" action="mailer.php" enctype="multipart/form-data" method="POST">
<div class="col-sm-6 form-group form-sty">
<label class="sr-only" for="first" required>First Name</label>
<input type="text" class="form-control-sty" name="first" placeholder="First Name">
</div>
<div class="col-sm-6 form-group form-sty">
<label class="sr-only" for="last" required>Last Name</label>
<input type="text" class="form-control-sty" name="last" placeholder="Last Name">
</div>
<div class="col-sm-6 form-group form-sty">
<label class="sr-only" for="phone" required>Phone</label>
<input type="text" class="form-control-sty" name="phone" placeholder="Phone">
</div>
<div class="col-sm-6 form-group form-sty">
<label class="sr-only" for="email" required>Email</label>
<input type="email" class="form-control-sty" name="mailfrom" placeholder="Email">
</div>
<div class="col-sm-12 form-sty">
<label class="sr-only" for="comment">Comment</label>
<textarea class="form-control-sty" name="message" id="message" rows="7" placeholder="What's on your mind?" required></textarea>
</div>
<div class="col-sm-12 form-sty form-sty-btn">
<button type="submit" value="Submit" class="btn btn-default col-sm-12">Send</button>
</div>
<p><?php if(!empty($message)) echo $message; ?></p>
</form>
You need to redirect the header back to the form (I assume that is where you want to send them) Replace the location inside the header with your URL
<?php
$message=
'First Name: '.$_POST['first'].'<br />
Last Name: '.$_POST['last'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['email'].'<br />
Message: '.$_POST['message'].'
';
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->CharSet = 'UTF-8';
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "tls"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 587;
// Authentication
$mail->Username = "xxxx#gmail.com"; // Your full Gmail address
$mail->Password = "xxxx"; // Your Gmail password
// Compose
$mail->SetFrom($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']);
$mail->AddReplyTo($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']);
$mail->Subject = "New Contact Form Enquiry"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress("xxxx#gmail.com", "xxxx"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
if ($result)
{
header("Location: : http://www.example.com/contactform.php");
}
else {
echo "sending mail failed: " . $mail->ErrorInfo;
}
unset($mail);
}
?>
You should make sure there are no errors. Try placing this code at the top of your file to make sure all errors/warnings are reported and run the script again.
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(E_ALL);
Here is my html for my form, action and everything else should be correct, I'm thinking its probably my php?
<form class="form-inline" role="form" method="post" action="index.php">
<div class="form-group">
<label for="inputName">Name</label>
<input type="text" class="form-control" id="inputName" placeholder="Jane Doe">
</div>
<div class="form-group">
<label for="inputEmail">Email</label>
<input type="email" class="form-control" id="inputEmail" placeholder="jane.doe#example.com">
<button type="submit" class="btn btn-default" id="formBtn">Send Email</button>
</div>
</form>
Here is my PHP that I copied and then changed the details to fit myself from github.
<?php
require 'phpMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 3;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'email#gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->From = 'email#gmail.com';
$mail->FromName = 'Bradley';
$mail->addAddress('email#gmail.com', 'Brad'); // Add a recipient
$mail->addReplyTo('email#gmail.com', 'Reply address');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Email from Vote Unanimous';
$mail->Body = 'You have a new email from your Vote Unanimous account. You should have a name and new email';
$mail->AltBody = 'You have a new email from your Vote Unanimous account. You should have a name and new email';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Thanks for helping out. I had to change my password in my gmail account to make it work
I have mail form:
<div class="contact">
<form id="mailer-form" action="./plugins/mailer/gmail.php" method="post" name="message">
<input type="text" name="name" /> <span>Jméno <strong>*</strong></span>
<div class="clear"></div>
<input type="text" name="email" /> <span>Email <strong>*</strong></span>
<div class="clear"></div>
<input type="text" name="subject" /> <span>Předmět</span>
<input type="hidden" name="frompage" value="yes" />
<div class="clear"></div>
<span>Zpráva <strong> * </strong></span>
<div class="clear"></div>
<textarea name="message" id="message"></textarea><br />
<input type="submit" name="submit" value="Odeslat zprávu!" />
</form>
</div>
And PHPMailer script:
<?php
if ($_POST['frompage'] == "yes")
{
date_default_timezone_set('Etc/UTC');
require './PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
$mail->CharSet = 'UTF-8';
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "xxx";
//Password to use for SMTP authentication
$mail->Password = "xxx";
//Set who the message is to be sent from
$mail->setFrom($_POST['email'], $_POST['name']);
//Set an alternative reply-to address
$mail->addReplyTo('xxx', 'xxx');
//Set who the message is to be sent to
$mail->addAddress('xxx', 'xxx');
//Set the subject line
$mail->Subject = $_POST['subject'];
$mail->msgHTML($_POST['message']);
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Sending email works fine but after sending email the page redirects me to the gmail.php with Message sent echo.
How can I disable it beacuse of staying on the index.php with response in value of the submit button?
Thanks.
It's not redirecting you, you set the form action to "./plugins/mailer/gmail.php", that's where your form data gets sent.
Replace:
echo "Message sent!";
by
header("Location:YourFormPage.php");
It will send you back. It's possible to do what your looking for, you need to research about AJAX so using this will not be necessary to leave the first page and complete the form submition.