I am pretty new at this so I apologize up front. Sorry for asking this question for the millionth time but I can't get my contact form to mail. I receive server error 500. I tried to find out what that meant without success. I have looked though all of the other posts here without success. Thanks for helping.
Here's my html:
Contact Form -->
<form id="contact-form" action="sendEmail.php" method="post">
<p>
<span>
<input placeholder="Name" type="text" name="name" required>
</span>
</p>
<p>
<span>
<input placeholder="Email" type="email" name="email" required>
</span>
</p>
<p>
<span>
<textarea placeholder="Message" name="message" cols="40" rows="10"></textarea>
</span>
</p>
<p>
<input type="submit" value="Send Message">
</p>
</form>
Here's the php:
<?php
// Form Variables
$sender_name = trim($_POST['name']);
$sender_email = $_POST['email'];
$sender_message = $_POST['message'];
// Configuration Vaiables
$receiving_email = 'name#email.com'; // Replace this with your own email address
$receiver_name = 'name'; // replace with your name
$email_subject = 'Email Sent from Contact Form'; // replace with any default title
// Require swiftMailer Library
require_once('lib/classes/swiftMailer/swift_required.php');
// Create the transport
$transport = Swift_SmtpTransport::newInstance('smtp.rickstrode.com') // enter SMTP address
->setPort(587) // enter SMTP port
->setEncryption('ssl') // enter encryption mode
->setUsername('name#email.com') // enter SMTP username
->setPassword('abcd1234') // enter SMTP password
;
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a Message
$message = Swift_Message::newInstance($email_subject)
->setFrom(array($sender_email => $sender_name))
->setTo(array($receiving_email, $receiving_email => $receiver_name))
->setBody($sender_message)
;
// Send the Message
$result = $mailer->send($message);
// Success and Failure Message
if ($result) {
echo "Congratulations, We've received your email. We'll be in touch as soon as we possibly can!";
} else {
echo "Unfortunately, Something went wrong while sending the message, Please try again!";
}
?>
Your SMTP server name is incorrect
$transport = Swift_SmtpTransport::newInstance('smtp.rickstrode.com')
should be
$transport = Swift_SmtpTransport::newInstance('mail.rickstrode.com')
Related
First off, I'm not a programming expert by any means. I had a company set PHPmailer for me and they created a simple form and the php code that makes it work. They didn't include radio buttons in the example and I haven't been able to figure out how to make them work.
I believe it's something simple but still over my head.
The link to the webpage: https://rappahannockfoundation.org/pages/opinion.php
Form code:
<form method="post" action="frmCode_opinion.php">
<p>Which concert did you attend? Click only one.</p>
<p>
<input name="concert" type="radio" value="Face 2 Face">
<label for="Face2Face">Face 2 Face</label>
</p>
<p>
<input type="radio" name="concert" ivalue="Capitol Steps">
<label for="CapitolSteps">Capitol Steps</label></p>
<p>
<input type="radio" name="concert" value="Russian String Orchestra">
<label for="RussianStringOrchestra">Russian String Orchestra</label></p>
<p>
<input type="radio" name="concert" value="Motones vs. Jerseys">
<label for="MotonesJerseys">Motones vs. Jerseys</label></p>
<p>
<input type="radio" name="concert" value="Leahy">
<label for="Leahy">Leahy</label></p>
<p>
<p class="padding_top_10">Would you like to see them for a return performance?</p>
<p><input name="return" type="radio" value="YES"> <label for="return">Yes </label>
<input name="return" type="radio" value="NO"> <label for="return">No </label>
</p>
<p class="padding_top_10">Please rate this program. 1 is poor 5 is excellent.</p>
<p>
<input type="radio" name="rate" value="1"><label for="one">1 </label>
<input type="radio" name="rate" value="2"><label for="two">2 </label>
<input type="radio" name="rate" value="3"><label for="three">3 </label>
<input type="radio" name="rate" value="4"><label for="four">4 </label>
<input type="radio" name="rate" value="5"><label for="five">5 </label>
</p>
<p class="padding_top_10">Please provide comments or suggestions for future performances.
</p>
<textarea style="margin-left:20px;" name="comments" cols="70" rows="5" id="comments"></textarea>
<p><div class="g-recaptcha padding_left_20 marginTop_20" data-sitekey="6LcXnhUUAAAAANRPRnVnY0sZEedUEvsr5lL80Zcj"></div></p>
<p> <input type="submit" name="submit" id="submit" value="Submit"></p>
</form>
PHP code:
<?php
/**
* This example shows how to handle a simple contact form using PHPMailer.
*/
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
$msg = '';
//Don't run this unless we're handling a form submission
if (array_key_exists('email', $_POST)) {
date_default_timezone_set('Etc/UTC');
// the below files are required in order to perform SMTP authentication using the PHPMailer library. If your page/script is under a subdirectory, rather than at the root of the Website directory, then you will need to update the paths here accordingly. For example, if your contact form is located under "/contact/form.php" then you should use a format like: "../phpmailer/src/PHPMailer.php"
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
require 'phpmailer/src/Exception.php';
//Create a new PHPMailer instance, the following lines will remain the same for all sites on your Plesk server
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'secure.emailsrvr.com';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
//$mail->SMTPAutoTLS = true;
//CAPTCHA CODE
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){ $err = "1";
}
$secretKey = "6LcXnhUUAAAAACRJ2RZ4A2CJmCFdNVxDfV4EbLPx";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
$err = "1";
}
// This will send information from the web server if form doesn't work
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->SMTPDebug = SMTP::DEBUG_CONNECTION;
$mail->SMTPDebug = 3; //this one is best
// The below parameters must be updated per Website:
// Username (address) and Password to use for SMTP authentication
$mail->Username = 'dave#lmarketing.com';
$mail->Password = 'Cr0ssT0ur!';
//Use the same "Username" fixed address in your own domain as the from address
$mail->setFrom('info#lmarketing.com', 'Dave Lipscombe');
//Set the recipient for this email (any email address and name)
$mail->addAddress('dave#lmarketing.com', 'Dave Lipscombe');
$mail->addBCC('lurbanna#gmail.com', 'Dave Lipscombe');
//Put the submitter's address in a reply-to header
//This will fail if the address provided is invalid,
//in which case we should ignore the whole request
if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
$mail->Subject = 'Submission from RFA Website';
$mail->isHTML(true);
//Build HTML message body
$emailMessage = "";
$emailMessage .= "<p>Concert: {$_POST['concert']}</p>";
$emailMessage .= "<p>Return performance: {$_POST['return']}</p>";
$emailMessage .= "<p>Program rating: {$_POST['rate']}</p>";
$emailMessage .= "<p>Comments: {$_POST['comments']}</p>";
// store the complete message string in the email body
$mail->Body = $emailMessage;
//set non-html message text for fallback in non-html compatible email software
//Build a simple message body
$mail->AltBody = <<<EOT
Concert: {$_POST['concert']}
Return Performance: {$_POST['return']}
Program rating: {$_POST['rate']}
Program rating: {$_POST['rate']}
Comments: {$_POST['comments']}
EOT;
//Send the message, check for errors
if (!$mail->send()) {
//If you need to debug an error sending, the reason for failing to send will be in $mail->ErrorInfo. Uncomment the below line to enable detailed error reporting, but be warned, this will display detailed error messages to anyone who uses the form while enabled!
print_r($mail->ErrorInfo); //comment this line when not in-use
//but you shouldn't display the details of those errors to users, so only this generic message will display publicly
$msg = 'Sorry, something went wrong. Please try again later.';
} else {
header("Location: thanks.php");
}
} else {
$msg = 'Invalid email address, message ignored.';
}
}
// Below you will find an example form, which includes the basic fields being sent in the email body above.
?>
I have a question about some PHP form handling. What I want is to take the data in the form below, and sent the data to my Gmail account. I do not own a mail server. I already discovered that you can not do it with the PHP mail function. I'm trying the PHPmailer out at this moment but it does not seem to have the right results, because it seems that PHPmailer only can be used to fill in a form and sent it to a person with your Gmail address.I want something different i want to let a user mail me to my Gmail address using the form inputs. So what shall I do? setting up a mail server with hmailserver and SquirrelMail or is there a better method.
<form id="form" action="PHPmail2.php" method="POST">
<input type="text" name="firstname"class="first" placeholder="first name"><font color="red">*</font>
<input type="text" name="lastname" class="first" placeholder="lastname"><font color="red">*</font>
<input type="email" name="email" class="first" placeholder="E-mail"><font color="red">*</font>
<br>
<input type="text" name="subject" class=" subject first" placeholder="subject">
<textarea type="text" class="textarea mail first" name="textarea" placeholder="fill somting in"></textarea>
<button type ="submit" id="submitbutton"name="submit">Send</button>
</form>
The PHP code:
<?php
if (isset($_POST['submit'])) {
if (!empty($firstname)) {
header('Location:index.php');
}else{
header('Location: filleErrorHandeling/nietAlleVeldenIngevuld.php');
}
if (!empty($lastname)) {
header('Location:index.php');
}else{
header('Location: filleErrorHandeling/nietAlleVeldenIngevuld.php');
}
if (!empty($from)) {
header('Location:index.php');
}else{
header('Location: filleErrorHandeling/nietAlleVeldenIngevuld.php');
}
}
adding some PHPmailer code all i have to do is link the form action to the file name.
<?php
$to="*";
$from=$_POST['email'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$subject=$_POST['subject'];
$msg=$_POST['textarea'];
$subject= "form submission";
$subject2="Copy of your form submission";
$message= $firstname. " ".$lastname. " Wrote the following: "."\n\n".$msg;
$message2= "Here is a copy of your message ". $firstname ."\n\n". $msg;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require ("composer/vendor/autoload.php");
$mail= new PHPMailer(true);
try{
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Port =25;
$mail->Username =$to;
$mail->Password = '*';
$mail->setFrom($email);
$mail->addAddress($email);
$mail->Subject='*';
$mail->Body=$msg;
$mail->send();
}
catch(Exception $e){
echo $e->errorMessage();
}
catch(\Exception $e)
{
echo $e->getMessage();
}
?>
What i basically want is when a user pressed on submit I want an email to my Gmail account with all the input data, or better even a part of it.
Thank you for reading my post if there are any questions regarding this topic let me know.
I'm trying to send a e-mail with a contact form but it`s not showing the data.
I've tried some answers here but can't get it to work.
My code:
<form class="form form-container method="post" action="assets/send_form_email.php">
<div class="left">
<input name="f_name" type="text" id="f_name" placeholder="Nome*" required />
<input name="f_email" type="email" id="f_email" placeholder="Email*" required />
<input name="f_phone" type="text" id="f_phone" required="required" maxlength="15" placeholder="Telefone*" />
<textarea name="f_msg" id="f_msg" placeholder="Mensagem" rows="6"></textarea>
</div>
<div class="right">
<input name="f_cnpj" type="text" id="f_cnpj" placeholder="CNPJ" style="font-weight: 700;" required />
<input name="f_sector" type="text" id="f_sector" placeholder="Setor da empresa" style="font-weight: 700;" required />
<input name="f_faturamento" type="text" id="f_faturamento" placeholder="Faturamento médio" style="font-weight: 700;" required />
<input name="f_valorMedio" type="text" id="f_valorMedio" placeholder="Valor médio de duplicatas" style="font-weight: 700;" required />
<button type="submit" class="botaoContato">Enviar</button>
</div>
</form>
Here is the PHP:
<?php
$nome = $_POST['f_name'];
$email = $_POST['f_email'];
$telefone = $_POST['f_phone'];
$cnpj = $_POST['f_cnpj'];
$setor = $_POST["f_sector"];
$faturamento = $_POST['f_faturamento'];
$valorMedio = $_POST['f_valorMedio'];
$msg = $_POST["f_msg"];
$mensagem = "Nome: '.$nome.' <br>
Email: '$email.' <br>
Telefone: '.$telefone.' <br><br>
CNPJ: '.$cnpj.' <br>
Setor: '.$setor.' <br>
Faturamento: '.$faturamento.' <br>
Valor Medio: '.$valorMedio.' <br>
Mensagem: '.$msg.' <br>";
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->SMTPDebug = 2;
$mail->IsSMTP();
$mail->Host = "smtp.****.com.br";
$mail->SMTPAuth = true;
$mail->Username = 'contato#****.com.br';
$mail->Password = '********';
$mail->From = "contato#*****.com.br";
$mail->Sender = "contato#****.com.br";
$mail->FromName = "****";
$mail->AddAddress('contato#*****.com.br');
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Subject = "Contato Site ";
$mail->Body = $mensagem;
$mail->AltBody = $mensagem;
$enviado = $mail->Send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
if ($enviado) {
echo "E-mail enviado com sucesso!";
} else {
echo "Não foi possível enviar o e-mail.
";
echo "Informações do erro:
" . $mail->ErrorInfo;
}
I've tried removing the variables and adding it directly to the $mail->Body but it does not work. I don't know much PHP so I'm relying on posts here or some tutorials but I can't find the error. :(
The most important thing when you ask a question like this is to describe exactly how it's not working - we can't guess.
First of all, you're using a very old version of PHPMailer, so get the latest, and base your code on the contact form example provided with PHPMailer.
You're sending only plain text content, so do this:
$mail->isHTML(false);
$mail->Body = $mensagem;
You don't need to set AltBody when you're doing this.
If your message is arriving, but doesn't contain what you expect, do this just before calling send() to confirm what parameters the script received, and what you asked it to send:
var_dump($_POST, $mail->Body);
You have SMTPDebug = 2, so you should be seeing debug output. If you get a false return value from send(), any error should be in $mail->ErrorInfo - you'll see how that can be used in the example code I linked to.
If you're seeing no SMTP debug output, it means it's having trouble connecting at all, in which case you should read the troubleshooting guide which tells you how to diagnose connection problems. The usual reasons are that your hosting provider may block outbound SMTP, or that you have outdated CA certificates.
I am having an issue with PHP Mailer. It is sending a blank email every time the page is loaded.
I'm sure it's something simple (maybe missing a condition if submit button is hit) to fix this.
Their documentation doesn't seem to have one though and the script first worked when I first used it but then started sending emails on page load after the first few times. Thanks!
<form method="post" action="">
<div class="form-group">
<input name="name" type="text" class="form-control" placeholder="Enter Name">
</div>
<div class="form-group">
<input name="email" type="email" class="form-control" placeholder="Enter Email">
</div>
<div class="form-group">
<input name="subject" type="text" class="form-control" placeholder="Enter Subject">
</div>
<div class="form-group">
<textarea name="message" class="form-control" rows="5" placeholder="Enter Message"></textarea>
</div>
<button name="submit" type="submit" value="submit" class="btn btn-submit">Submit</button>
</form>
<?php
require '/phpmailer/PHPMailerAutoload.php';
require_once('/phpmailer/class.phpmailer.php');
include("/phpmailer/class.smtp.php");
$emailaddress = 'info#newpointdigital.com';
$message=
'Name: '.$_POST['name'].'<br />
Email: '.$_POST['email'].'<br />
Subject: '.$_POST['subject'].'<br />
IP: '.$_SERVER['REMOTE_ADDR'].'<br /><br />
Message:<br /><br />
'.nl2br($_POST['message']).'
';
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
//$mail->SMTPDebug = 2; // 1 = errors and messages,2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "smtp.gmail.com"; // sets the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "info#newpointdigital.com"; // SMTP account username (the email account your created)
$mail->Password = "newpoint!##$"; // SMTP account password (the password for the above email account)
$mail->SMTPSecure = 'ssl'; // Enable encryption, 'ssl' also accepted
$mail->CharSet = 'UTF-8'; // so it interprets foreign characters
$mail->SetFrom($_POST['email']);
$mail->AddReplyTo($_POST['email']);
$mail->Subject = "Contact form from ".$_POST['name']." ";
$mail->MsgHTML($message);
$mail->AddAddress($emailaddress);
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
You don't check to see if a form submission occurred. You just call your email code when the page loads. There are several ways to decide if a form submission occurred. One way is to check to see if the form action is POST:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// email code goes here
}
You can also check to see if the submit button was pressed:
if (isset($_POST['submit'])) {
// email code goes here
}
Other things to know:
You don't do any data validation so it is still possible for someone to submit nothing resulting in a blank email being sent
You don't protect against header injections which makes your form vulnerable to sending spam
This happen cause you didn't apply any condition for this to run so code runs line by line on page load .
so keep your mail sending code in submit check condition so it will run after submit click
if(isset($_POST['submit'])) {
// your mail sending code
}
or using any of your post data like for email check
if(!empty($_POST['email'])) {
// your mail sending code
}
I have a website form where someone can send me a message. They have to enter their name, email and a message.
The form works to a point in that I get the email and the senders name and email they entered is shown in the email, but not the message they enter.
Can anyone see where the problem lies please?
Here is the HTML form
<form method="post" action="php/mail.php" name="cform" id="cform">
<input name="name" id="name" type="text" class="col-xs-12 col-sm-6 col-md-6 col-lg-6" placeholder="Your name..." >
<input name="email" id="email" type="email" class=" col-xs-12 col-sm-6 col-md-6 col-lg-6 noMarr" placeholder="Your email..." >
<textarea name="message" id="message" cols="" rows="" class="col-xs-12 col-sm-12 col-md-12 col-lg-12" placeholder="Your message..."></textarea>
<input type="submit" id="submit" name="send" class="submitBnt" value="Send message">
</form>
And here is the php/mail.com that is uses (connection details changed to anonymous details)
<?
require("class.phpmailer.php");
//form validation vars
$formok = true;
$errors = array();
//sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');
//form data
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP - mail or smtp.domain.com
$mail->Host = "myhost.myprovider.com"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "info#myaddress.com"; // SMTP username
$mail->Password = "mypassword"; // SMTP password
$mail->From = "info#myaddress.com"; // SMTP username
$mail->AddAddress("info#myaddress.com"); // Your Adress
$mail->Subject = "New contact request from ME !";
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Body = "<p>You have recieved a new message from the enquiries form on your website.</p>
<p><strong>Name: </strong> {$name} </p>
<p><strong>Email Address: </strong> {$email} </p>
<p><strong>Subject: </strong> {$subject} </p>
<p><strong>Message: </strong> {$message} </p>
<p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";
if(!$mail->Send())
{
echo "Mail Not Sent <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
There is no reason for, as you say, name and email to be shown and message to be not.
I think you need enctype for textareas ... but im not completely sure. Try it, it wont hurt you:
<form enctype="multipart/form-data" method="post" action="php/mail.php" name="cform" id="cform">
Might want to take a look at this similar question
Someone had the same issue and all the had to do was change the id and name to something different
Issue using $_POST with a textarea
As to the issue with the subject being shown I saw you commented about, you never pass a subject to $_POST unless you are in some other code that is not posted
EDIT 1:
If you are manually setting the subject then why try to get it from $_POST as well as trying to do <p><strong>Subject: </strong> {$subject} </p> will not work because $subject has no value from $_POST