PHPMailer Message could not be sent.Mailer Error: Message body empty - php

So this is my code, it seems like it doesn't catch my "content" input field which is not empty btw, however I get this error
Message could not be sent.Mailer Error: Message body empty
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'intrepid.servers.prgn.misp.co.uk'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'secret#secret.co'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom($_POST["userEmail"], $_POST["userName"]);
$mail->addAddress('myemailforreceivingemails#gmail.com'); // Add a recipient
$mail->addReplyTo($_POST["userEmail"], $_POST["userName"]);
if(is_array($_FILES)) {
$mail->AddAttachment($_FILES['attachmentFile']['tmp_name'],$_FILES['attachmentFile']['name']);
}
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["content"];
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
This is from my index.php file
function sendContact() {
var valid;
valid = validateContact();
if(valid) {
$.ajax({
url: "contact_mail.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data){
$("#mail-status").html(data);
},
error: function(){}
});
}
}

Related

Weird $_POST behavior in PHPMailer

I have a weird PHP issue. I'm using PHPMailer along with JQuery to itterate through a few email addresses and send them individually.
It works. BUT...PHPMailer raises an exception that sender is not found. I have tried var_dump(), and echoing the $_POST or $_GET for the sender email, and I get nothing. The other $_POST/$_GET show up just fine, just the email fails to show, even in the console.log.
Did I mention it works? That's the weird part; the script processes fine, and the emails are sent. I just get a PHPMailer exception and can't see the $_POST/$_GET variable.
Any help appreciated!
Here's is the JQuery -
// ------ Mail Send-Multi Button ------------
$(document).on('click','#send-multi',function(e){
e.preventDefault;
var subj = $('#subject').val();
var msg = $('#msg').val();
var i = 0;
$(".mailchek input:checkbox").each(function () {
if (this.checked) {
var sto = $(this).val();
//alert($(this).val());
i++;
//alert(i);
var itm = "sendto="+sto+"&subject="+subj+"&msg="+msg;
alert(itm);
//ajax for multi email
$.ajax({
type: "GET",
url: "webmail.php",
data: itm,
success: function(result){
//alert(result);
alert("test");
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});//End AJAX
} //end Address loop
});
Here is the PHPMailer -
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 0; // Verbose debug off. Change to '2' for debug echo
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.somehost.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'info#somecompany.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
//Recipients
$mail->setFrom('info#somecompany.com', 'Name');
$mail->addAddress($_GET[sendto]); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $_GET[subject];
$mail->Body = $_GET[msg];
$mail->send();
echo "here ".$send;
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>

issue in sending mails in bulk in php

I am design a form for sending mail in bulk via phpMailer, for example i want to send a mail to 50 persons.but the issue is after sending mail 10 to 15 persons my form gives an file not found exception.
my ajax call is given below
.on('success.form.bv', function(e) {
e.preventDefault();
$('#send_email_next').modal({
backdrop: 'static'
});
$.ajax({
type: "POST",
url: "email_next.php",
data: $("#emailform").serialize(),
success: function(html){ $("#modal_body").html(html);}
});
});
Phpmailer function called in email_next.php is given below
require_once("class.phpmailer.php");
require_once("class.smtp.php");
$mail = new PHPMailer();
$body=" ";
set_time_limit(0);
$mail->IsSMTP();
$mail->Timeout = 10000;
$mail->SMTPKeepAlive = true;
$mail->SMTPDebug = false;
$mail->do_debug = 0;
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'tls'; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port
$mail->Username = $account_user_name; // GMAIL username
$mail->Password = $password; // GMAIL password
$mail->From = $account_user_name;
$mail->FromName = 'Rayat Bahra Group';
$mail->Subject = $_POST['subject'];
$mail->AltBody = '';
$mail->WordWrap = 50;
$mail->MsgHTML($body);
$mail->MsgHTML($content);
$mail->IsHTML(true); // send as HTML
$SN = 0;
$error_mail=0;
$total_mails_to_send = count($email_array);
foreach($email_array as $recipient_address)
{
$mail2 = clone $mail;
$mail2->MsgHTML($content);
$mail2->AddAddress(trim($recipient_address));
$status = $mail2->Send();
if($status)
{
$SN++;
}
else{
$error_mail = 1;
}
echo $SN." out of ".$total_mails_to_send. " sent <br>";
}
if($error_mail == 1)
{
echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
}
else{
echo "Message has been sent";
}
$mail->SmtpClose();

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

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.

SMTP Email is not sending on iPhone cordova WebApp, working in browser?

I have a little problem with an iOS cordova Webapp and PHP. The code below works perfectly on the computer(Xampp):
<?php
require 'lib/sendMail.php';
require '../php/PHPMailerAutoload.php';
$mail = new PHPMailer;
$sendMail = new sendMail();
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'ex#ex.de';
$mail->Password = 'expw';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->From = 'ex#ex.com';
$mail->FromName = 'Mailer';
$mail->addAddress($_POST['termin']['cityemail']);
$mail->isHTML(true);
$mail->Subject = 'ex';
$mail->Body = $sendMail->getMessage($_POST);
$mail->AltBody = $sendMail->getMessage($_POST);
if(!$mail->send()) {
print 'Message could not be sent.';
print 'Mailer Error: ' . $mail->ErrorInfo;
} else {
print 'Message has been sent';
}
?>
Note: I am using PhpMailer to send mails via the phone.
The problem is, that the phone is going crazy. Everything after "->" is ignored or something. This is how the screen looks after submitting the form:
How can this happen? The email is sending successfully on the browser.
The device is iPhone 5S 16GB with iOS 8.1.
Okay I've got a working solution. Simply put your php files on a server, make an ajax call with type = " post " and there you go. Example code below(I'm using jQuery validation plugin) :
$("#Formular").validate({
submitHandler: function(form) {
var dataString = $(form).serialize();
$.ajax(
{
url : 'http://yoururl.de/file.php',
type: "POST",
data : dataString,
success:function(data, textStatus, jqXHR)
{
$('body').html(data);
}
});
},
rules: {
'phone': {
required: false,
phoneDE: true
}
},
messages: {
'phone': {
required: "Bitte gültige Telefonnummer eingeben"
}
}
});

Categories