PHP Mailer Function not working - php

I am new to PHP and have a form that sends mails. The following is the PHP code and i get the message
"Message body empty message could not be sent.
mailer error:
"
I have been searching on google whole day of what the issue might be but have not been able to figure it out.
I am not understanding the part "Message body empty". I checked the $mail.body and it has the value.
<?php
require_once("class.PHPMailer.php");
$mail = new phpmailer();
$mail->issmtp(); // set mailer to use smtp
$mail->host = "smtp.office365.com"; // specify main and backup server
$mail->smtpauth = true; // turn on smtp authentication
$mail->username = "noreply#test.com"; // smtp username
$mail->password = "test123"; // smtp password
$mail->from = "noreply#test.com";
$mail->fromname = "no reply";
$mail->addaddress("johndoe#test.com", "John Doe");
$mail->wordwrap = 50; // set word wrap to 50 characters
//$mail->addattachment("/var/tmp/file.tar.gz"); // add attachments
//$mail->addattachment("/tmp/image.jpg", "new.jpg"); // optional name
$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";
echo $mail->subject;
try{
if(!$mail->send())
{
echo "message could not be sent. <p>";
echo "mailer error: " . $mail->errorinfo;
exit;
}
echo 'after mail send';
}
catch(exception $e) {
echo 'caught exception: ', $e->getmessage(), "\n";
}
echo "message has been sent";
?>
Also I have deployed in windows server.
Thanks in advance.

<h2>Feedback Form</h2>
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"])) {
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
From: <input type="text" name="from"><br>
Subject: <input type="text" name="subject"><br>
Message: <textarea rows="10" cols="40" name="message"></textarea><br>
<input type="submit" name="submit" value="Submit Feedback">
</form>
<?php
} else { // the user has submitted the form
// Check if the "from" input field is filled out
if (isset($_POST["from"])) {
$from = $_POST["from"]; // sender
$subject = $_POST["subject"];
$message = $_POST["message"];
// message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// send mail
mail("webmaster#example.com",$subject,$message,"From: $from\n");
echo "Thank you for sending us feedback";
}
}
?>

Related

phpmailer gives HTTP ERROR 500 when i try to test email on cloudways

I am a newbie in web development. I wrote a php code to send emails from a form and i hosted it in cloudways. I get a HTTP ERROR 500 whenever i hit the submit button.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
# code...
$name = trim($_POST["fullname"]);
$email = trim($_POST["email"]);
$phone_no = trim($_POST["phone_num"]);
$partner_type = $_POST["Partner_type"];
if ($name == "" || $email == "" || $phone_no == " ") {
echo "please fill the required fields name , email , phone number";
}
if ($_POST["adds"] != "") {
echo "Bad form input";
exit;
}
require 'class.phpmailer.php';
$mail = new PHPMailer();
if (!$mail->ValidateAddress($email)) {
# code...
echo "Invalid Email Address";
exit;
}
$email_body = "";
$email_body .= "Name " . $name . "\n";
$email_body .= "Email " . $email . "\n";
$email_body .= "Phone Number " . $phone_no . "\n";
$email_body .= "Partner Type " . $partner_type . "\n";
$mail->setFrom($email, $name);
$mail->addAddress('rescuetl#localhost', 'gbubemi'); // Add a recipient
/**$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
**/
$mail->isHTML(false); // Set email format to HTML
$mail->Subject = 'Become a Partner ' . $name ;
$mail->Body = $email_body;
$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;
} else {
echo 'Message has been sent';
}
header("location:index.php?status=thanks");
}
?>
please it is really frustrating i need help. Thanks.
First you need to debug the application weather email is sending or not or may be it's stuck in a queue. you can check it by running "mailq" on terminal. Next first try to send simple email on Cloudways like this:
<?php
require_once "vendor/autoload.php"; //PHPMailer Object
$mail = new PHPMailer; //From email address and name
$mail->From = "from#yourdomain.com";
$mail->FromName = "Full Name"; //To address and name
$mail->addAddress("recepient1#example.com", "Recepient Name");//Recipient name is optional
$mail->addAddress("recepient1#example.com"); //Address to which recipient will reply
$mail->addReplyTo("reply#yourdomain.com", "Reply"); //CC and BCC
$mail->addCC("cc#example.com");
$mail->addBCC("bcc#example.com"); //Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else { echo "Message has been sent successfully";
}
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
If it works and email sent then try sending by applying smtp credentials like this:
<?php
require_once "vendor/autoload.php";
$mail = new PHPMailer;
//Enable SMTP debugging.
$mail->SMTPDebug = 3;                           
//Set PHPMailer to use SMTP.
$mail->isSMTP();        
//Set SMTP host name                      
$mail->Host = "smtp.gmail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;                      
//Provide username and password
$mail->Username = "name#gmail.com";             
$mail->Password = "super_secret_password";                       
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";                       
//Set TCP port to connect to
$mail->Port = 587;                    
$mail->From = "name#gmail.com";
$mail->FromName = "Full Name";
$mail->addAddress("name#example.com", "Recepient Name");
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
?>
You can also set up Smtp on Cloudways Platform in server settings:
Read the full article here: https://www.cloudways.com/blog/send-emails-in-php-using-phpmailer/
You should use the function PHPMailerAutoload which is in the package .. it is simple and works very well. I use it myself on a project. And in addition, it is very easy to assimilate.
function sendMail($destinaire, $code, $prename){
require 'phpmailer/PHPMailerAutoload.php';
$userMail = $destinataire; //Recipient
$mail = new PHPMailer();
$mail->Host = "smtp.gmail.com";
$mail->isSMTP();
$success_send = " Le code de reservation a éte envoyé à ".$userMail;
$failed_send = "Echec: le mail in not send verifier votre email";
//On ouvre l'authentification
$mail->SMTPAuth = true;
//Les creditentials
$mail->Username = "Sender#gmail.com";
$mail->Password = "Senderpass";
//set the type of protection
$mail->SMTPSecure ="ssl"; //tls
//Definissons un port
$mail->Port ="465"; //587 pour tls
//L'objet du message
$mail->Subject ="Reservation enregistré";
//Le message en lui meme, afin le contenu quoi
$mail->Body ="Bonjour '.$prename.' Nous avons enregistré votre reservation.
Veuillez presenter le Code suivant le jour du voyage ".$code;
//Bref, qui envoi?
$mail->setFrom('sender#gmail.com', 'Indiza - Travel Dimension');
//set where we are sending this email(à qui)
$mail->addAddress($userMail);
//Envoyer l'email
if($mail->send())
return $success_send;
else
return $failed_send;
}
Utility
To start (if you use Gmail) you should lower the security level of the account that sends the messages
https://support.google.com/accounts/answer/6010255?hl=en
Github Php Mailer
And if is necessary, This link will be very useful Codingpassive/PHPMailer
Just call a function like this
$idz = sendMail("$receiverMail","anyCode","receiverName");
Is it really computer? No gentleman, we do crafts.
Est-ce réellement de l'informatique? Non mon ami, on fait juste du bricolage.

Unable to send email from godaddy server to godaddy or zoho mail

I am trying to send email from my website to my godaddy mail and zoho mail but its not working.
I tried it on my gmail account and its working fine.
I am using phpmailer.
MY CODE-
require_once "PHPMailerAutoload.php";
//PHPMailer Object
$mail = new PHPMailer;
//From email address and name
$mail->From = "test#deltware.com";
$mail->FromName = "Himanshu Mishra";
$mail->addAddress("my godaddy webmail"); //Recipient name is optional
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
Please help!!!!!
Your From syntax is wrong
Instead of
$mail->From = "test#deltware.com";
$mail->FromName = "Himanshu Mishra";
It should be
$mail->setFrom('test#deltware.com', 'Himanshu Mishra');
Check this link https://github.com/PHPMailer/PHPMailer

using phpmailer mail sent but couldnt got form content

I get email but i am not getting the content from the form though I have removed enctype tag from form Below is output i get in body of email
Name:
Organisation:
Postal Address:
City:
Country:
Email:
Telephone:
Comments:
what is wrong thing done by me can anyone help.
<?php
require("/home/public_html/phpMailerTesting/PHPMailer_5.2.0/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "sendfromphpmailer#xxx.com"; // SMTP username
$mail->Password = "12345"; // SMTP password
enter code here
$name=$_POST['name'];
$email=$_POST['mail'];
$message=$_POST['comment1'];
$mail->From ="sendfromphpmailer#xxx.com" ;
$mail->FromName = $_POST["name"];
$mail->AddAddress("receivefromphpmailer#xxx.com");
// name is optional
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "You have received feedback from your website!";
$mail->Body = "Name:$name
Organisation: $organisation
Postal Address: $PostalAddress
City: $City
Country: $Country
Email: $email
Telephone: $telephone
Comments: $comments";
//$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>

Unable to send email using core php

I'am newbie in php. I'am trying to send email using php but I don't know what's wrong in my code. I googled a lot but nothing has worked yet. Here is my php code. I'am using class.phpmailer.php.
<?php
require("phpmailer-master/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "myemail#googlemail.com"; // SMTP username
$mail->Password = "mypassword"; // SMTP password
$webmaster_email = "recipient#googlemail.com"; //Reply to this email ID
$email="username#domain.com"; // Recipients email ID
$name="myname"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
I could finally send a mail using php. Here is the code:
<?php
require_once('class.phpmailer.php');
include("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = 'sender#mail.com';
$mail->Password = 'sender_password';
$mail->SMTPAuth = true;
$mail->From = 'sender#mail.com';
$mail->FromName = 'sender';
$mail->AddAddress("sender#mail.com");
$mail->AddReplyTo("sender#mail.com", 'Information');
$mail->IsHTML(true);
$mail->Subject = "Sample exmple to check proper working of mail function";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->Body = "Hello ";
$path = $_POST['upload'];
$mail->AddAttachment($path); // attachment
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message sent!";
}
?>
<?php
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
// Comment out this line here it is wrong
// IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username#gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "username#doamin.com"; //Reply to this email ID
$email = "username#domain.com"; // Recipients email ID
$name = "name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email, $name);
$mail->AddReplyTo($webmaster_email, "Webmaster");
$mail->WordWrap = 50; // set word wrap
// i would also comment out these lines, get it working without attachments first
// then add then back in after (if you want attachments)
// $mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
// $mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
// at the end of the pasted code above, you have these lines (below here) doubled up.
// remove them
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>

PHP Mailer Attachments Not Uploading?

I am trying to set up PHP Mailer to allow me to send attachments, mainly just images, from a form. I have the following code working except I am not receiving the attachments, they are not being uploaded. Can anybody help me or point me in the right direction as to what I am missing?
PHP
<?php
require 'class.phpmailer.php';
date_default_timezone_set('Europe/London');
$mail = new PHPMailer;
$current=date('l F dS, Y, H:i a');
$mail->From = 'me#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('karina#live.co.uk', 'Karina McG');
$mail->addAddress('me#live.com');
$mail->addReplyTo('noreply#tempur.com');
$mail->addCC('');
$mail->addBCC('');
$mail->addAttachment('/var/tmp/file.tar.gz');
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');
$mail->isHTML(true);
$name=$_POST['name'];
$subject=$_POST['subject'];
$system=$_POST['system'];
$country=$_POST['country'];
$description=$_POST['description'];
$mail->Subject = "TEMPUR Web Support Request - $system";
$mail->Body = "Dear $name,<br>
<br>
We have received your TEMPUR request form! - $current<br>
<br>
We will be in contact shortly regarding your issue/s.<br>
<br>
Name : $name <br>
Country : $country <br>
Subject : $subject <br>
System : $system <br>
Description : $description";
$mail->AltBody = 'Hehehe';
//$to="$email";//Will change to Digital Users email!
//Send email
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
?>
HTML
Upload File:<input type="file" name="upload" id="upload"><br>
add enctype="multipart/form-data" to the form as attribute if it is not their.
also try $mail->addAttachment($_FILES["upload"]["tmp_name"]);
It will work.

Categories