PHP script with form not sending the email - php

I am having some problems with php. Well, the code, when uploaded to the server and run in browser is working and the email is arriving, using this code below:
<?php
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->SMTPDebug = 3;
$mail->IsSMTP();
//$mail->IsSMTP();
$mail->Debugoutput = 'html';
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPSecure = false;
$mail->SMTPAuth = false;
$mail->Username = "exemple#gmail.com";
$mail->Password = "password";
$name = "catarina";
$email = "myEmail#hotmail.com";
$message = "test";
$mail->setFrom($email, $name);
$mail->addAddress('me#exemple.io');
$mail->Subject = 'Contact Site';
$mail->Body = $message;
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
The problem is, when I change this code a little to send with a form, the email never arrives:
js:
function submitForm() {
$.ajax({type:'POST', url:'email-action.php', data:$('#contact-form').serialize(), success: function(result){
$('.submit').html('send');
$('.send').removeClass('no-show');
$('.send').removeClass('no-show-mobile');
}});
};
html:
<form action="/" onsubmit="return submitForm();" method="post" name="contactform" class="contact-form wow zoomIn" data-wow-delay="0.6s" id="contact-form">
<div class="col-md-6">
<div class="row">
<div class="col-md-12">
<input placeholder="Name" class="input-field" name="name" required="" type="text">
</div>
<div class="col-md-12">
<input placeholder="E-mail" class="input-field" name="email" required="" type="email">
</div>
</div>
</div>
<div class="col-md-6">
<textarea placeholder="Message" class="input-field" name="message"></textarea>
<input value="Send Message" class="input-send submit" type="submit" name="submit">
</div>
<div class="col-md-12 send no-show hidden-xs"><button class=" botao btn btn-sucess"><h4 class="">Message sent sucessfully! We will get in touch shortly.</h4></button></div>
<div class="col-md-12 send no-show-mobile visible-xs"><button class=" botao btn btn-sucess"><h4 class="">Message sent sucessfully!</h4></button></div>
</form>
php:
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->SMTPDebug = 3;
$mail->IsSMTP();
//$mail->IsSMTP();
$mail->Debugoutput = 'html';
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPSecure = false;
$mail->SMTPAuth = false;
$mail->Username = "exemple#gmail.com";
$mail->Password = "password";
///this is the thing that change for one script to another
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
/////
$mail->setFrom($email, $name);
$mail->addAddress('me#exemple.io');
$mail->Subject = 'Contact Site';
$mail->Body = $message;
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

Related

How would I get the phone number and name displayed on emails I recieve - PHPMailer

So the emails I recieve only shows the subject, who it was sent from, who it was sent to but not the senders name or phone number, this is not done locally but from where my website is hosted. I want it to show the senders name and phone number after the body. This is my code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require 'vendor/autoload.php';
$mail = new PHPmailer();
$mail->Host = "";
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->Username = "";
$mail->Password = "";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->addAddress('');
$mail->setFrom($_POST['email']);
$mail->Subject = $_POST['subject'];
$mail->isHTML(true);
$mail->Body = $_POST['message'];
$mail->Name = $_POST['name'];
$mail->Phone = $_POST['number'];
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
//echo $mail->ErrorInfo;
?>
This is my html:
<form action="contact.php" method="post" class="signin-form mt-lg-5 mt-4">
<div class="input-grids">
<input type="text" name="name" placeholder="Full name or Business"
class="contact-input" required=""/>
<input type="email" name="email" placeholder="Your email" class="contact-
input" required=""/>
<input type="text" name="subject" placeholder="Subject" class="contact-
input" required=""/>
<input type="text" name="number" placeholder="Phone number"
class="contact-input" />
</div>
<div class="form-input">
<textarea name="message" placeholder="Type your message here"
required=""></textarea>
</div>
<div class="form-input mb-5">
<label class="container"><p>Send me a copy <a href="#">privacy policy.
</a></p>
<input type="checkbox">
<span class="checkmark"></span>
</label>
</div>
<button class="btn submit">Submit</button>
</form>
I am not sure if I need to do something like this:
if (isset($_POST['submit'])) {
require 'vendor/autoload.php';
$mail = new PHPmailer();
$mail->Host = "";
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->Username = "";
$mail->Password = "";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->addAddress('');
$mail->setFrom($_POST['email']);
$mail->Subject = $_POST['subject'];
$mail->isHTML(true);
$mail->Body = $body . "<br>Phone number: " . $number . "<br>Name: " . $name;
}
$body = $_POST['message'];
$name = $_POST['name'];
$phone = $_POST['number'];
and just give my submit button this attribute
<button class="btn submit" name="submit">Submit</button>
Last thing: I want to show that the message has been sent just above the contact form instead of at the top of the page, it is not top priority but something I was still wondering about.
You basically want to create a $message variable and assign that to $mail->Body.
The second example you have shown is close but you need to make sure that the below is within your $_POST['submit'] if statement.
$body = $_POST['message'];
$name = $_POST['name'];
$phone = $_POST['number'];
Like so:
if (isset($_POST['submit'])) {
require 'vendor/autoload.php';
$body = $_POST['message'];
$name = $_POST['name'];
$phone = $_POST['number'];
$mail = new PHPmailer();
$mail->Host = "";
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->Username = "";
$mail->Password = "";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->addAddress('');
$mail->setFrom($_POST['email']);
$mail->Subject = $_POST['subject'];
$mail->isHTML(true);
$mail->Body = $body . "<br>Phone number: " . $number . "<br>Name: " . $name;
$response = $mail->send(); //to actually send the email
}
For:
Last thing: I want to show that the message has been sent just above the contact form instead of at the top of the page, it is not top priority but something I was still wondering about.
You could subject the form via ajax and listen to the $response value. If its 1 then good to go, else there was an issue.

$_POST is returning empty array

I've gone through a lot of forums today to figure out exactly why this is happening. I've set up PHPMailer and everything works fine. However, at the top of the code you can see that I attempt to define variables which are initialized with $_POST, however for example if I run var_dump($_POST['name']); it will return NULL for the value. The form data simply is not being sent over. It might be a simple mistake but insight would be appreciated.
PHP Code:
<?php
require 'PHPMailerAutoload.php';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
print_r($_POST['name']);
// if(isset($_POST['name'])) {
// $name = htmlspecialchars($_POST['name']);
// }
// if(isset($_POST['email'])) {
// $email = htmlspecialchars($_POST['email']);
// }
// if(isset($_POST['message'])) {
// $message = htmlspecialchars($_POST['message']);
// }
$mail = new PHPMailer();
$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 = 'xxxxxxxxxxxx'; // SMTP username
$mail->Password = 'xxxxxxxxxx'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = $email;
$mail->FromName = $name;
$mail->addAddress('xxxxxxxx'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = "Contact Request Form From $name";
$mail->Body = "
<table width='450px'>
<tr>
<td><strong>Name: </strong></td>
<td>$name</td>
</tr>
<tr>
<td><strong>Email: </strong></td>
<td>$email</td>
</tr>
<tr>
<td><strong>Message: </strong></td>
<td>$message</td>
</tr>
</table>
";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
Here is my HTML form code:
<div class="contact-form">
<h3>Contact Me</h3>
<p>Please use the form below to get in contact with me</p>
<form action="process.php" method="post">
<div class="form-group">
<label for="name-1">Name</label>
<input type="text" name="name" id="name-1" placeholder="Enter Name" class="style">
</div>
<div class="form-group">
<label for="email-1">Email</label>
<input type="email" name="email" id="email-1" placeholder="Enter Email" class="style">
</div>
<div class="form-group">
<label for="message-1">Message</label>
<textarea name="message" id="message-1" placeholder="Enter Message"></textarea>
</div>
<div>
<input type="submit" value="Submit" class="btn confirm">
</div>
</form>
</div>
JavaScript Code:
// If submit button is clicked on contact form
document.querySelector('.confirm').onclick = function() {
window.location.href = 'confirmed.html';
}

Jquery Ajax, PHPMailer doesn't send emails even if there's no error but works if i use stand alone page with fix values

I have this simple bootstrap form that i run on my localhost, it accepts name email and message and a custom captcha in a form of a math question; On submit i used jquery ajax to fetch the data to emailme.php that contains the phpmailer code.. My problem is, no email was sent even if there's no error and the network status is 200 and i can also see this:
http://localhost/emailme.php?name=myname&email=myemail#gmail.com&message=test
however it works if i just use the phpmailer stand alone code with fixed values. how can i make it work?
HTML:
<form id="emailme" method="post">
<div class="form-group">
<input type="text" class="form-control" name="name" id="name" placeholder="e.g. John Doe">
<span id="err_name" class="text-danger"></span>
</div>
<div class="form-group">
<input type="text" class="form-control" name="email" id="email" placeholder="e.g. example#domain.com">
<span id="err_email" class="text-danger"></span>
</div>
<div class="form-group">
<textarea name="message" id="message" cols="30" rows="10" class="form-control" placeholder="Your message"></textarea>
<span id="err_message" class="text-danger"></span>
</div>
<div class="form-group">
<label for="human">5 + 2 = ?</label>
<input type="text" id="human" name="human" class="form-control" placeholder="Your Answer">
<span id="err_human" class="text-danger"></span>
</div>
<input type="submit" class="btn btn-primary btn-sm" value="Send">
</form>
JQUERY:
$("#emailme").on('submit',function(e){
e.preventDefault();
var name = $("#name").val();
var email = $("#email").val();
var message = $("#message").val();
var human = $("#human").val();
var emailRegex = /(.+)#(.+)\.(com|edu|org|etc)$/g;
if(name!==''&&email!==''&&message!==''&&human!==''){
if(email.match(emailRegex)){
if(human==7){
$.ajax({
url:'emailme.php',
typ:'post',
data:{name:name,email:email,message:message},
success:function(response){
console.log(response);
$("#mail-status").html(response);
$("#emailme")[0].reset();
},
error:function(XMLHttpRequest,textStatus,errorThrown){
console.log(textStatus+errorThrown);
}
});
}
else{
$("#mail-status").html("unable to submit form, you're not human");
}
}
else{
$("#err_email").append("Invalid email format");
}
}
else if(name==''){
$("#err_name").append("Name cannot be empty");
}
else if(email==''){
$("#err_email").append("Email cannot be empty");
}
else if(message==''){
$("#err_message").append("Message cannot be empty");
}
else if(human==''){
$("#err_human").append("Please answer the security question");
}
});
PHP:
<?php
date_default_timezone_set('Etc/UTC');
require 'phpmailer/PHPMailerAutoload.php';
// Set the email address submissions will be sent to
$email_address = 'myemail#gmail.com';
// Set the subject line for email messages
$subject = 'Test email from localhost using PHPMailer';
// Check for form submission
if(isset($_POST['name'], $_POST['email'], $_POST['message'])){
//Create a new PHPMailer instance
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "myemail#gmail.com";
$mail->Password = "mypassword";
$mail->setFrom($_POST['email'], $_POST['name']);
$mail->addAddress($email_address, 'myname');
$mail->Subject = $subject;
$mail->Body = 'From:'.$_POST['name'].'(' . $_POST['email'] . ')'.'<p><b>Message</b><br/>' . $_POST['message'] . '</p>';
$mail->AltBody = 'This is a plain-text message body';
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
?>

php mailer is not working

Here's my phpmailer code, its not working , dont know where i am getting error.
I have to embed this code in certain script and i think for gmail port no port no 465 .
<?php
if(isset($_POST['submit'])) {
require_once('phpmailer/class.phpmailer.php');
$email=$_POST['email'];
$subject1=$_POST['subject'];
$message=$_POST['message'];
smtpmailer($email,$subject1,$message);
function smtpmailer($to,$subject,$body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "aman****589#gmail.com";
$mail->Password = "*****02589";
$mail->From = "aman***589#gmail.com";
$mail->FromName = "Cor****tions";
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}
}
?><html><body>
<form method="post" action="index.php">
Email: <input name="email" id="email" type="text" /><br />
Subject:<br />
<textarea name="subject" id="subject" rows="2" cols="40"></textarea><br />
Message:<br/>
<textarea name="message" id="message" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" name="submit"/>
</form>
</body>
</html>
Can anybody help ?? where i m getting error?
when i run this script its giving error-
Fatal error: Call to undefined function smtpmailer() in C:\xampp\htdocs\phpm\index.php on line 8
You have to declare a function with its parameters, before you call it. At the moment, your program is searching for the function smtpmailer() - but it is not even defined yet.
<?php
if(isset($_POST['submit'])) {
require_once('phpmailer/class.phpmailer.php');
$email=$_POST['email'];
$subject1=$_POST['subject'];
$message=$_POST['message'];
function smtpmailer($to,$subject,$body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "aman****589#gmail.com";
$mail->Password = "*****02589";
$mail->From = "aman***589#gmail.com";
$mail->FromName = "Cor****tions";
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}
smtpmailer($email,$subject1,$message);
}
?><html><body>
<form method="post" action="index.php">
Email: <input name="email" id="email" type="text" /><br />
Subject:<br />
<textarea name="subject" id="subject" rows="2" cols="40"></textarea><br />
Message:<br/>
<textarea name="message" id="message" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" name="submit"/>
</form>
</body>
</html>
Declare the function before you call it.

phpmailer secure approach by placing credentials out of the web root in INI file

first of all thank you for your time and helping me on this.
I have a simple contact form and i'm using phpmailer.
I want to store credentials in an INI file out of webroot and then include it in my mail.php file which is the mail sending script.
How to write the INI content and how to call them on mail.php file?
This is my HTML file which is contact us form:
<h3><font style="line-height:170%" size="4"> <a id="contactus">Contact us form</a> </font></h3>
<form method="post" action="email.php" accept-charset="UTF-8">
<p>
<label>your name</label>
<input name="dname" type="text" size="30" />
<label>your email</label>
<input name="demail" type="text" size="30" />
<label>your domain name</label>
<input name="ddomain" type="text" size="30" />
<label>Message</label>
<textarea name="dmessage" id="dmessage" rows="5" cols="5"></textarea>
<br />
<input class="button" type="submit" value="submit"/>
</p>
</form>
<br />
and this is the email.php file:
<?php
header('Content-type: text/plain; charset=utf-8');
$email = $_REQUEST['demail'] ;
$message = $_REQUEST['dmessage'] ;
$ddomain = $_REQUEST['ddomain'] ;
require("PHPMailer_v5.1/class.phpmailer.php");
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = true;
$mail->Username = "info#example.com"; // SMTP username
$mail->Password = "this is the password"; // SMTP password
$mail->From = $email;
$mail->AddAddress("info#example.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $ddomain;
$mail->Body = $message;
$mail->AltBody = $ddomain;
if(!$mail->Send())
{
echo "Error <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "we have received your email";
?>
I want to store credentials in a safe place.
Another problem is that whenever someone open email.php file in browser ( example.com/email.php) an empty email will be sent to me, how to prevent it?
I want the email.php file to be executed only a result of a customer's contact via filling the form and not by directly opening the email.php file
Thank you all
<?php
if (empty($_REQUEST['demail']) || empty($_REQUEST['dmessage'])) {
die();
}
$constants = parse_ini_file("/outside/web/sample.ini");
header('Content-type: text/plain; charset=utf-8');
$email = $_REQUEST['demail'] ;
$message = $_REQUEST['dmessage'] ;
$ddomain = $_REQUEST['ddomain'] ;
require("PHPMailer_v5.1/class.phpmailer.php");
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = true;
$mail->Username = $constants['username']; // SMTP username
$mail->Password = $constants['password']; // SMTP password
$mail->From = $email;
$mail->AddAddress("info#example.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $ddomain;
$mail->Body = $message;
$mail->AltBody = $ddomain;
if(!$mail->Send())
{
echo "Error <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "we have received your email";
?>
INI file (sample.ini) looks like:
username = "info#example.com"
password = "PASSW0RD"
Thank you for your helps.
I did some change on your code and it is excatly what is want.
instead of saying:
if (empty($_REQUEST['demail']) || empty($_REQUEST['dmessage'])) {
die();
}
i used this one:
if (empty($_REQUEST['demail']) || empty($_REQUEST['dmessage'])) {
echo "Please fill-in Email and Message sections completely!";
exit;
}
Thank you for your help

Categories