PHPmail-Contact form not receiving mail and no errors - phpmailer

So I have a problem, I uploaded my index.php file that is a contact form to my website server and when I fill it out and click submit it just refreshes my browser and no error occurs and no message shows that it failed or it was submitted. Here is my html code: https://codepen.io/themandelaeffect/pen/poJPBdZ?editors=1000 and this is the php code that I'm using:
<?php
ini_set('display_errors', true);
error_reporting(1);
if(isset($_POST['submit']))
{
ini_set('display_errors', true);
require_once "assets/PHPMailer-5.2-stable/PHPMailer-5.2-stable/PHPMailerAutoload.php";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$Comments = $_POST['message'];
$mail = new PHPMailer;
//Enable SMTP debugging.
$mail->SMTPDebug = 3;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = "host";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = "username";
$mail->Password = "********";
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";
//Set TCP port to connect to
$mail->Port = 465;
$mail->From = "johndoe#email.com";
$mail->FromName = "Contact";
$mail->addAddress($email, $name);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $Comments;
$mail->AltBody = $Comments;
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
}
PHP is new to me and I am not sure what to do. I copied this code from another stack overflow question because they said their code works because the original code that I used gave me lots of errors. Can someone help me please?

i saw your code there i found you dont have any action page so if your php code in in other page just add page name to
<form action="your_page_name.php" method="post" class="signin-form mt-lg-5 mt-4">
and if your php code (above code you provided ) is in same page then no need to action , so your form starting simply would be like
<form method="post" class="signin-form mt-lg-5 mt-4">
so this was not exactly your problem , i just seen you mentioned you new in php so i just explained because your action was a bit wrong , you used action:"" insted of action=""
well i just found your submit button have no type mentioned and also no any name , means you submitting your php code with name submit "if(isset($_POST['submit'])) " you you must define name for button and to submit a form with php you must need to define button type="submit" but you just added button like
<button class="btn submit">Submit</button>
so it will by default only refresh page so change it with
<button type="submit" name="submit" class="btn submit">Submit</button>
then it will work fine it will hit your php code and it will execute.

First of all, you're running an old version of PHPMailer; upgrade.
You don't show your form, but if it doesn't contain a named input element called submit, your mail code will never run.
You are combining port 465 with tls encryption mode; that won't work - do what the examples provided with PHPMailer recommend, as other combinations won't work.
Your Host value should be your outgoing mail server, and any username and password should match that. If you have your own mail server, point at that, if not, use whatever your hosting provider recommends - we can't tell from out here.

Related

Feedback form hangs due to PHPMailer

I have a feedback form, this script is filtering and validating data and writing data to the database. At the very bottom, through include, I connected a script with PHPMailer, which sends the text of the feedback form to my mail via gmail smtp.
If I comment out the script connections, then the form is submitted either immediately or after 1 second. With him, he can wait 2-3 seconds.
I submit form data via XMLHttpRequest, remotely. Upon successful submission, the form is reset to zero and the submit button becomes inactive, and via pop-up notifications I display the server's response. So it happened that I managed to press the send button 2-3 times until the script worked and, accordingly, several records were created and several letters were sent to the mail. Is this how it should be or have I configured PHPMailer wrong?
Let me know what data I need to attach. This is my PHPMailer script:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require $_SERVER['DOCUMENT_ROOT'] . '/form/PHPMailer/PHPMailer.php';
require $_SERVER['DOCUMENT_ROOT'] . '/form/PHPMailer/Exception.php';
require $_SERVER['DOCUMENT_ROOT'] . '/form/PHPMailer/SMTP.php';
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->isSMTP();
$mail->SMTPDebug = 0; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
$mail->Host = gethostbyname("smtp.gmail.com");; // use $mail->Host = gethostbyname('smtp.gmail.com'); // if your network does not support SMTP over IPv6
$mail->Port = 587; // TLS only
$mail->SMTPSecure = 'tls'; // ssl is deprecated
$mail->SMTPAuth = true;
$mail->Username = 'mymail#gmail.com'; // email
$mail->Password = 'mypassword'; // password
$mail->setFrom($email, $name); // From email and name
$mail->addAddress('mymail#gmail.com', 'Admin'); // to email and name
$mail->Subject = $subject;
$mail->msgHTML("Message from: \n"."<h3>".$email."</h3>\n"."<h1>".$message."</h1>"); //$mail->msgHTML(file_get_contents('contents.html'), __DIR__); //Read an HTML message body from an external file, convert referenced images to embedded,
$mail->AltBody = 'HTML messaging not supported'; // If html emails is not supported by the receiver, show this body
// $mail->addAttachment('images/phpmailer_mini.png'); //Attach an image file
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->send();
// if(!$mail->send()){
// echo "Mailer Error: " . $mail->ErrorInfo;
// }else{
// echo "Message sent!";
// }
?>
You're doing some inadvisable things here.
$mail->Host = gethostbyname("smtp.gmail.com");
This sets Host to a literal IP address, and that in turn means that you will never have it match a TLS certificate name. As a result you're having to disable TLS verification, which is never a good thing. If you understand exactly why you're doing this, and what the consequences are, that's fine, but if not, you shouldn't be doing it.
There isn't any error checking in this script. I suggest starting again using the gmail example provided with PHPMailer which is much more careful.
You've discovered why sending to remote mail servers with SMTP during web form processing generally a bad idea: it's too slow, partly by design. The best way to work around this is to install a local mail server (postfix is good) and configure it as a relay to your gmail account – if you search for that you'll find plenty of examples. When that's done, you can submit messages to localhost, it will be more or less instant, and it will take care of queuing, throttling, bounces etc.

PhpMailer sends the registration form to the e-mail 3 times

The same registration form information comes to my mail more than once.
My Mailbox is filled with exactly the same forms.
Can you help me solve this problem?
PHP CODE:
 <?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer(); // create a new object
$mail = new PHPMailer(); // create a new object
$subject = "[XX] Registration Form";
$question = nl2br(stripcslashes($_POST['question']));
$question = trim($question);
$email_message = "<b>Name:</b> ".stripcslashes($_POST['fname'])."<br><br>";
$email_message .= "<b>Surname:</b> ".stripcslashes($_POST['lname'])."<br><br>";
$email_message .= "<b>Birthday:</b> ".stripcslashes($_POST['dob'])."<br><br>";
$email_message .= "<b>Phone:</b> ".stripcslashes($_POST['phone'])."<br><br>";
$email_message .= "<b>Email:</b> ".stripcslashes($_POST['email'])."<br><br>";
$email_message .= "<b>Address:</b> ".stripcslashes($_POST['adres'])."<br><br>";
$email_message .= "<b>Location:</b> ".stripcslashes($_POST['Location'])."<br><br>";
$body = $email_message;
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->Host = "smtp.yandex.com.tr";
$mail->SMTPSecure = 'ssl';
$mail->Port = 465; // or 465
$mail->IsHTML(true);
$mail->CharSet ="utf-8";
$mail->SetFrom("info#xx.com", "XX REGISTRATION FORM"); // Mail adresi
$mail->Username = "info#xx.com"; // Mail adresi
$mail->Password = "xxx"; // Parola
$mail->Subject = $subject;
$mail->body = $body;
$mail->MsgHTML($body);
$mail->AddAddress("info#xx.com");
$mail->addReplyTo(stripcslashes($_POST['emailer']), "");
if(!$mail->Send()){
echo "Mail Error".$mail->ErrorInfo;} else {
echo "Mesaj Gönderildi";
}
It's most likely that the script is simply being run more than once. This can be caused by browser plugins, or by not preventing processing of empty forms. Protect against sending empty messages during page load by wrapping it in a condition like this:
if (isset($_POST['emailer'])) {
//Rest of your script
The main thing to check first is whether a single run of your script is sending multiple messages, or whether the script is being run more than once (sending one message each time). Try writing something to a log file, or making it obvious in received messages by appending a random number to the end of the subject line. If you receive two messages with the same number in the subject, they are true duplicates; if the numbers are different subjects, your script is being run more than once:
$mail->Subject = 'Hi ' . rand();
Some other problems:
$mail->body = $body;
PHP is case-sensitive for properties, so that should be $mail->Body = $body;, but you're also calling $mail->MsgHTML($body);, which sets the Body property for you, so you don't need to set body yourself as well.
While you're working on a script, $mail->SMTPDebug = 1 isn't very useful as it doesn't show what the server is saying; set $mail->SMTPDebug = 2. Don't forget to set it to 0 in production!
I can also see that you're using a very old version of PHPMailer, and have based your code on an obsolete example, so update.
The problem was solved when I edited my codes like this.
The code executed when the form is submitted includes the following:
<script type="text/javascript">
function checkForm(form) // Submit button clicked
{
//
// check form input values
//
form.myButton.disabled = true;
form.myButton.value = "Please wait...";
return true;
}
</script>
While the HTML for the form itself looks something like:
<form method="POST" action="..." onsubmit="return checkForm(this);">
...
<input type="submit" name="myButton" value="Submit">
</form>

SMTP connect() failed error after 70 successful sends

A number of people had shown similar question, using phpmailer with g-mail. This problem is slightly different and may need a different answer.
The phpmailer code is fairly standard (below). I have a loop that goes through some data in the table and builds customised messages for recipients, sending a message for each row in the table. When the message is sent, it successfully completes about 75 out of some 100 recipients, and for the last 25, it reports the SMTP connect() failed error. This doesn't happen every time, and occasionally, the script goes through the loop without errors.
<?php
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPsecure = "ssl";
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "smtp.gmail.com"; // sets the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "xx"; // SMTP account username
$mail->Password = "xx"; // SMTP account password
$mail->SetFrom("xx#gmail.com");
$mail->isHTML(true);
$mail->CharSet = "utf-8";
$mail->Subject = $msgsubject;
$mail->AltBody = "Invitation";
do {
$membername = $row_musicians['name'];
$membernumber = $row_musicians['number'];
$emailaddress = $row_musicians['email'];
$rhdate = $row_musicians['rhdate'];
$mail->clearAttachments();
$mail->Body = 'Dear '.$membername.',
<p>'.$maintext.'</p>
';
// some more text using custom variables from database
// closing part of the message
$mail->Body .= '<p>'.nl2br($closing, false).'</p>
</body>
</html>';
$mail->AddAddress($emailaddress, $membername); // sending each message
sleep(1); // wait one second between sends, to avoid spam filters
echo '<br />
Recipient: '.$membername.' ('.$emailaddress.') -- ';
if(!$mail->Send()) {
echo "Mailer Error: <div style='color:#009999'>" . $mail->ErrorInfo." </div>";
} else {
echo "Message sent!";
}
$mail->ClearAddresses();
} while ($row_musicians = mysql_fetch_assoc($musicians));
mysql_free_result($musicians);
?>
I recommend basing your code on the mailing list example provided with PHPMailer.
You're doing most things right - initialising PHPMailer first and setting properties that are common to all messages, but one key thing is missing:
$mail->SMTPKeepAlive = true;
Without this it means that it closes and reopens a connection for every message, and you're mostly likely running into connection rate limits.
you're also using the combination of Port= 587 and SMTPSecure = 'ssl' which will generally not work; switch to Port = 465.
I'd also recommend against the do/while loop - it will fail if your database query fails to find anything because it's bottom-tested and thus will always run once, even if there is no data. Use a while or foreach loop instead.
The mailing list example does all of these things already.
Final thing - if this is all your code, it looks like you're running an old version of PHPMailer, so get the latest.

PHPMailer in combination with Gmail smtp sending mail twice

I have come across some weird behaviour in PHPMailer. I am using my gmail account for the SMTP settings. But every time PHP sends an e-mail(in this case, it's for resetting a password), I receive the same e-mail too. Except for when the password reset is requested for my gmail account.
Here is the code;
//set email settings
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'ssl://smtp.gmail.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Port = 465;
$mail->Username = 'mymail#gmail.com'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->From = 'noreply#bla.com';
$mail->FromName = 'Bla';
$mail->AddAddress($email); // Name is optional
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'Bla registration';
$mail->Body ="
<html>
<header><title>Welcome to Bla</title></header>
<body>
<p>Hello,</p>
<p>Welcome to Bla. Thank you for registering an account. Before you can start inventing music, please activate your account</p>
<p><a href='http://www.bla.com/register/activate.php?activation=$activationcode&email=$email'> http://www.Bla.com/register/activate.php?activation=$activationcode&email=$email</a></p>
See you soon on Bla!
</body>
</html>";
$mail->Send();
There is no error or something like that. The e-mail is actually send, and received. Only problem is that I receive as well because the e-mail address in Username is my e-mail address. Does anyone know why this happens?
Add ClearAddresses like this->
$mail->Send();
$mail->ClearAddresses();
And solved.
Greetens
Try to change
$mailer->isSMTP();
to
$mailer->Mailer = 'smtp';

Awardspace Email Setting

I got a Award Space Mail Hosting account.I created that account only for the free email sending feature.
Tried
PHP mailer
swiftmailer ect..
But no use.Can anyone give me a full working code of php and mail form,so that i can just test it in my server .I cound send emails when using this file :
http://www.html-form-guide.com/php-form/php-form-validation.html
But,i dont want that peace of code.I want a perfect code that can work on My server.
Sample code:(Has an html form submit form)
<?php
if(isset($_POST['submit'])) {
$myemail = “support#domain.dx.am”;
$subject = $_POST['subject'];
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers = “From:Contact Form <$myemail>\r\n”;
$headers .= “Reply-To: $name <$email>\r\n”;
echo “Your message has been sent successfully!”;
mail($myemail, $subject, $message, $headers);
} else {
echo “An error occurred during the submission of your message”;
}
?>
Or i tried doing the same with php mailer:
<?php
require 'filefolder/mailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'localhost'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'support#whatever.dx.am'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->From = 'support#whatever.dx.am';
$mail->FromName = 'Support';
$mail->AddAddress('anything#gmail.com'… // Name is optional
$mail->WordWrap = 50; // Set word wrap to 50 characters
$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->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
?>
I am doing something wrong. But cant figure out what. Can anyone help me out .
The 'from' header should be an existing email account inside your Email Manager of your Hosting Control Panel.
as their FAQ says:
Tip: Please note that the 'From' header should be an existing email account inside your Email Manager of your Hosting Control Panel.
FAQ

Resources