PHPMailer doesn't work, page gets white - php

I'm trying to send an email with PHPMailer, but everytime I press to send, the page turns white. I tried to search in the error_log but nothing appeared inside. By the way, I'm using ajax, all the plugins are included and working.
This is the code I have:
$.ajax({
type: "POST",
url: "cod_ajax/enviar_email.php",
data: "nome="+nome+"&last="+last+"&email="+email+"&assunto="+assunto+"&texto="+texto,
success:function(e){
document.write(e);
$("#envia_email").prop("disabled", true);
setInterval(function(){
$("#envia_email").html("Email Enviado com sucesso! Recarregue a página para enviar outro!");
}, 2000);
$(".erros").fadeOut(0);
$("#name").val("");
$("#last").val("");
$("#email").val("");
$("#assunto").val("");
$("#texto").val("");
}
})
All variables are right, nothing is empty and receive well the information. This is my ajax file:
if(isset($_POST['name'])){
$nome=$_POST['nome'];
$last=$_POST['last'];
$email=$_POST['email'];
$assunto=$_POST['assunto'];
$texto=$_POST['texto'];
require '../mail/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setFrom('b_daniel_18#hotmail.com');
$mail->addAddress('b_sem_l#hotmail.com');
$mail->Subject = 'First PHPMailer Message';
$mail->Body = 'Hi! This is my first e-mail sent through PHPMailer.';
if(!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
}
I didn't use the variables because it didn't work so I tried adding information manually, still didn't work.
UPDATE
Removed the if(isset($_POST['name'])) and I can see the errors now. It doesn't send, appears Mailer error: Could not instantiate mail function.

You mistyped your form variable name in the php file. Change
if(isset($_POST['name']))
To:
if(isset($_POST['nome']))

Try this code
$mail = new PHPMailer();
$mail->SMTPSecure = 'tls';
$mail->Username = "b_daniel_18#hotmail.com";
$mail->Password = "mypassword";
$mail->AddAddress("b_sem_l#hotmail.com");
$mail->FromName = "My Name";
$mail->Subject = "My Subject";
$mail->Body = "My Body";
$mail->Host = "smtp.live.com";
$mail->Port = 587;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->From = $mail->Username;
if($mail->Send()){
echo 'Sent.<br/>';
}else{
echo 'Not sent: <pre>'.print_r(error_get_last(), true).'</pre>';
}

Related

EOF caught while checking if connectedThe following From address failed

I am trying to send emails using phpmailer and getting the error as mentioned in the title. I am using XAMPP for apache server.
I have searched almost every link on google and everywhere but nothing is working for me.
<?php
ini_set('display_errors', 1);
require 'autoload.php';
require 'class.phpmailer.php';
//require 'class.smtp.php';
$mail = new PHPMailer;
$mail->IsSMTP();
//$mail->SetLanguage("en", 'language');
$mail->Mailer = "smtp";
//$mail->SMTPSecure = "tls";
$mail->Port = 465;
$mail->SMTPDebug = 2;
$mail->SMTPAuth=False;
$mail->Host="smtp.gmail.com";
$mail->Username = "email#gmail.com";
$mail->Password = "password";
//$mail->setFrom("email#gmail.com");
$mail->From = "email#gmail.com";
$mail->addAddress('email#gmail.com','Rishabh Goel');
$mail->isHTML(false);
$mail->Subject = 'First Subject';
$mail->Body = 'Mail body in HTML';
$mail->AltBody = 'This is the plain text version of the email content';
/*try{
$mail->Send();
echo "Thanks. Bug report successfully sent.
We will get in touch if we have any more questions!";
} catch(Exception $e){
//Something went bad
echo "Mailer Error: - " . $mail->ErrorInfo;
}*/
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message has been sent successfully";
}
?>

How to redirect after mail send with PHPMailer

How can I redirect when the email is being sent with PHPMailer?
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
// $mail->Host = "email-smtp.us-east-1.amazonaws.com";
$mail->Host = "email-smtp.us-west-2.amazonaws.com";
$mail->Username = "AKIAIVSF45PCGR7NZWNQ";
$mail->Password = "Am2SBg4vluOvIc1+kycsWpCnxtf3jhGjYCAdBv7YYp/y";
//
$mail->SetFrom('test#gmail.com', 'Z-Reports'); //from (verified email address)
$mail->Subject = "Z-Reports (sent via smtp)"; //subject
//message
$body = emailZReports($total_sales, $inventory);
// $body = eregi_replace("[\]",'',$body);
$mail->MsgHTML($body);
//
//recipient
$mail->AddAddress("test#gmail.com", "Z-Reports");
//Success
if ($mail->Send()) {
echo "Message Sent!";
}
//Error
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
How can I redirect to a specific page instead of displaying Message Sent!?
if you are using SMTP, you can't redirect to another page if $mail->SMTPDebug is not set to 0. This is because it sends out headers which you can't modify with header("Location: path/to/redirect"). Set $mail->SMTPDebug = 0; so that you can redirect to another page after $mail->send(). but you have to do that only when you have finished debugging and your mail is sending successfully.
You can use php header function
//Success
if ($mail->Send()) {
header("Location:Yourfile.php");//echo "Message Sent!";
}
Make sure there is not echo or any output before this function
You can use like this,
See Header for more info.
It can be like this,
//Success
if ($mail->Send()) {
header('Location: nextpage.php');
}
Otherwise, you can use Javascript to redirect the user.
Just use
window.location = "http://www.nextpage.com/"
This works
if(!$mail->Send()) {
echo "Message could not be sent. ";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
} //echo getcwd();
header("Location:index.php") //redirects to a page named index.php;
// make another script for redirecting
<?php
require_once("mail_function.php");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
header("Location: form.php");exit; //redirect to form submit page
} else {
echo "Message sent!";
header("Location: index.php");exit; //redirect to your home page
}
?>
//mail_function.php
<?php
require 'class/class.phpmailer.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Mailer = "smtp";
$mail->SetFrom("xxxx#gmail.com", "web");
$mail->Port = '587'; //Sets the default SMTP server port
$mail->SMTPAuth = true; //Sets SMTP authentication.
$mail->Username = 'xxxx#gmail.com'; //Sets SMTP username
$mail->Password = 'xxxx'; //Sets SMTP password
$mail->SMTPSecure = 'tls'; //Sets connection prefix.
$mail->FromName = $_POST["name"];
$mail->AddAddress($email); //Adds a "To" address
$mail->AddCC($_POST["email"], $_POST["name"]); //Adds a "Cc" address
$mail->WordWrap = 50; //Sets word wrapping on the body
$mail->IsHTML(true); //Sets message type to HTML
$mail->Subject = "somthing";
$mail->MsgHTML($message_body);
$mail->Body = "somthing";
$result = $mail->Send();
return $result;
?>

PHP Mailer not sending SMTP mail

I am unable to send mail through Gmail smtp settings... On the other hand without any authentiacation i am getting mail from sg2nlhg096.shr.prod.sin2.secureserver.net server name.
SMTP settings code.
require_once "class.phpmailer.php";
require_once('class.smtp.php');
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'abc#gmail.com';
$mail->Password = '*************';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('johndoe#gmail.com', 'John');
$mail->addReplyTo('replyto#example.com', 'Doe');
$mail->addAddress('mr.shah118#gmail.com', 'John Doe');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
echo 0;
} else {
echo "Message sent!";
echo 1;
}
Simple Mail settings ...
require("class.phpmailer.php");
$mail = new PHPMailer(); // create a new object
$mail->From = $_POST['email1'];
$mail->FromName = $_POST['name1'];
$mail->addAddress("techdynastic#gmail.com", "Dynastictech Form");
//Address to which recipient will reply
$mail->addReplyTo($_POST['email1'], "Reply");
$mail->isHTML(true);
$mail->Subject = 'hidden_subject';
$mail->Body = 'body';
$mail->AltBody = "Thanks";
if (!$mail->Send()) {
//echo 'error'. mysqli_error($con);
echo 0;
} else {
//echo 'Message has beeen successfully send. we will contact as soon!';
echo 1;
}
My Html file with ajax call
$.ajax({
type: "POST",
url: "../send_form_email.php",
data: dataString,
cache: false,
success: function (data) {
//$(".success_mes").html(data);
if (data == 1)
{
$(".success_mes").html("Message has beeen successfully send.");
$('#name').val('');
$("#email").val('');
$("#phone").val('');
$("#subject").val('');
$("#comments").val('');
} else
{
$(".mail_error").html("*Something Went Wrong ..");
}
},
});
You can enable Debug to see more details:
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only

php/ajax - {"success":{"title":"Message Sent"}}

I've never used ajax before but from what I can tell that's the solution to my problem, basically I'm using a PHPmailer form on indk.org/contact.html and when you hit submit it displays a new window (taking you away from the website nav) with {"success":{"title":"Message Sent"}} shown
How do I stop this happening, or at the very least, have a message sent confirmation appear on the webpage itself, want to avoid taking people off site as much as I can as it's a personal portfolio.
Welcome any aid! Thanks :) D
<?php
require '../_lib/phpmailer/PHPMailerAutoload.php';
// CONFIG YOUR FIELDS
//============================================================
$name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$formMessage = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
// CONFIG YOUR EMAIL MESSAGE
//============================================================
$message = '<p>The following request was sent from: </p>';
$message .= '<p>Name: ' . $name . '</p>';
$message .= '<p>Email: ' . $email . '</p>';
$message .= '<p>Message: ' . $formMessage .'</p>';
// CONFIG YOUR MAIL SERVER
//============================================================
$mail = new PHPMailer;
$mail->isSMTP(); // Enable SMTP authentication
$mail->SMTPAuth = true; // Set mailer to use SMTP
$mail->Host = 'mailout.one.com'; // Specify main and backup server (this is a fake name for the use of this example)
$mail->Username = 'dk#indk.org'; // SMTP username
$mail->Password = 'XXX'; // SMTP password
$mail->SMTPSecure = 'SSL'; // Enable encryption, 'ssl' also accepted
$mail->Port = 587;
$mail->From = 'dk#indk.org';
$mail->FromName = $name;
$mail->AddReplyTo($email,$name);
$mail->addAddress('dk#indk.org', $name); // Add a recipient
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Contact request';
$mail->Body = $message;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
$data['error']['title'] = 'Message could not be sent.';
$data['error']['details'] = 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
$data['success']['title'] = 'Message Sent, click back to return to indk.org';
echo json_encode($data);
?>
$(document).ready(function() {
'use strict';
$('#contact-form').validate({
// Override to submit the form via ajax
submitHandler: function(form) {
$('#contact-panel').portlet({
refresh:true
});
$.ajax({
type:$(form).attr('method'),
url: $(form).attr('action'),
data: $(form).serialize(),
dataType: 'json',
success: function(data){
console.log(data);
//Set your Success Message
clearForm("Thank you for Contacting Us! We will be in touch");
},
error: function(err){
$('#contact-panel').portlet({
refresh:false,
//Set your ERROR Message
error:"We could not send your message, Please try Again"
});
}
});
return false; // required to block normal submit since you used ajax
}
});
function clearForm(msg){
$('#contact-panel').html('<div class="alert alert-success" role="alert">'+msg+'</div>');
}
$('#contact-panel').portlet({
onRefresh: function() {
}
});
});
You can do it either way. You can use a simple bootstrap pop-up to show the confirmation or as you said you can use AJAX.

PHPmailer how to show message

How I can show success message after to click send in my form? my phpmailer is working but I want to also after to click send to redirect to my homepage (index.html) and show my message div. This is my php code.
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$subject = $_POST['subject'];
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.xxxxxxx.pl'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxxx#xxxxxxxx.pl'; // 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($email, $name);
$mail->addAddress('xxxx#gmail.com', 'xxxxxxxx'); // Add a recipient // Name is optional
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = 'Body test';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
header('Location: index.html');
}
And this is my Jquery code to show/hidden div succes:
$('.box-up').animate({top : 150}, 'normal').delay(3000).animate({top : -500}, 'normal');
You could send extra parameter with your request :
header('Location: index.php?r=1');
Then in your index page get the parameter and use it to show the box :
<script>
var result="<?php echo $_GET['r']; ?>";
if(parseInt(result)){
$('.box-up').animate({top : 150}, 'normal').delay(3000).animate({top : -500}, 'normal');
}
</script>
Hope this helps.

Categories