phpmailer not redirecting to page - php

I am making a mail sending module on user approvals. The mail is being sent to multiple users on the checkbox values. The problem is once I am using the given code it sends mail to all the checked users and redirects to the page given but as a blank page.
Now when I remove set_time_limit(120); few users are being left and mail is not sent to them even after checking all the users in list and the page is being redirected successfully.
Here is my code, please let me know rectification into this. This will be highly appreciated.
set_time_limit(120);
$mail = new PHPMailer();
$subject = "Welcome mail";
$content ="AAFM";
$mail->IsSMTP();
$mail->Timeout = 120;
$mail->SMTPKeepAlive = true;
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "ssl";
$mail->Debugoutput = 'html';
$mail->Port = 465;
$mail->Username = "xyz#xyz.com";
$mail->Password = "xyz";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Mailer = "smtp";
$mail->SetFrom("xyz#xyz.com", "xyz");
$mail->AddAddress($row['email']);
$mail->Subject = $subject;
$mail->WordWrap = 80;
$mail->MsgHTML($content);
$mail->IsHTML(true);
if(!$mail->Send())
echo "Problem sending mail.";
else
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Candidates approval successful. Thankyou')
window.location.href='reportApprovalUser.php';
</SCRIPT>");
$mail->SmtpClose();
I have subsequently changed the time_limit variable in phpmailer Class too as 120.
Basically when mail is sent to all the listed users, the redirection shows a blank page and when redirection shows the actual page the list still has few users left even on selecting all for approval/mailing.

Solved the issue. Actually headers were not being sent due to whitespaces. Added ob_start() and its functional.

Related

Sending e-mail with attachment to gmail never gets received, using phpmailer

Something weird is going on when i'm trying to send an e-mail with attachment to gmail, using phpmailer.
Sometimes the email received after a delay of 5 minutes and sometimes it is never received. From phpmailer debugger i get that the email is actually sended, so i assume it has something to do with the attachment getting blocked by gmail.
The attachment is a zip file containing only .jpg and .pdf files.
If i remove the addAttachment('path/to/file') everything works fine.
The weird thing is that sometimes, even with the attachment, the email received immediately and other times the email never gets received.
Here is the code
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = 'my_host';
$mail->Port = 'my_port';
$mail->SMTPAuth = true;
$mail->Username = 'my_username#domain.com';
$mail->Password = 'my_password';
$mail->setFrom('my_username#domain.com', 'my_username#domain.com');
$mail->addAddress('test#gmail.com', 'test#gmail.com');
$mail->Subject = $subject;
$mail->msgHTML($msg);
$mail->AltBody = $subject;
$mail->addAttachment($zipfilename);
if($mail->send()){
echo "ok";
}else{
echo "error";
}
I don't have any clue why this is happening so any help will be appreciated.
EDIT:
I found out that with two changes everything works fine.
The changes are the setting
$mail->SMTPSecure = 'tls'
and i had a path like this ./path/to/file and changed it to 'path/to/file'.

How to resolve SMTP could not connect error? [post STARTTLS problem]

I have installed php mailer using composer composer require phpmailer/phpmailer. I followed instructions from this website https://www.geeksforgeeks.org/how-to-send-an-email-using-phpmailer.
Below is my code for sending email.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try
{
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'mail.example.in';
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('owner#gmail.com', 'Owner');
$mail->addAddress('receipent#gmail.com');
$mail->isHTML(true);
$mail->Subject = "Hello this is subject";
$mail->Body = "Hello this is message;
$mail->send();
echo "Mail has been sent successfully!";
}
catch (Exception $e)
{
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
When I comment $mail->isSMTP(); and send mail, I get the result as message sent but I dont get it in my Gmail inbox.
when I uncomment $mail->isSMTP(); I get error message as shown in below image.
My project is hosted in godaddy server.
Even if I use php mail() function to send mail, response is mail send successfully, but it does not get delivered into my Gmail inbox
If you want to use Gmail, you should follow the steps here to manually add Google MX records to your GoDaddy account:
https://uk.godaddy.com/help/point-my-domains-email-service-to-google-7936
Unfortunately it takes some days for changes to be reflected.
Then you need to disable security features, change your php mailer config like:
$mail->Host = 'localhost';
$mail->SMTPAuth = false;
$mail->SMTPAutoTLS = false;
$mail->Port = 25;
Hope this solves.
------EDIT------
Later I figured out that the emails go to spam folder when the port is 25. I changed the connection to TLS and now I receive them as normal.
Here is the code from my website, it works on Godaddy, I receive e-mails to my gmail account.
$mail = new PHPMailer();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'true';
$mail->Port = 587;
$mail->Host = "smtp.gmail.com";
$mail->Username = "********#gmail.com";
$mail->Password = "**********";
$mail->SetFrom("********#gmail.com");
$mail->AddAddress("********#gmail.com");
$mail->Subject = " your subject";
$mail->Body = "your body";

phpmailer sending but not receiving

Just finished setting up PHPMailer to send the PDF that is created from my html form (using FPDF, the pdf file is created without a problem). It says sent successfully but im not receiving anything?
I have checked other peoples code and it looks just like mine. Is there anything im doing wrong with the PHPmailer code at the bottom?
my host,username and password are all correct 100% as far as I know we dont use TLS or SSL. Maybe that has something to do with this?
My code:
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 4;
$mail->Host ="*****";
$Mail->SMTPAuth = true; // enable SMTP authentication;
$mail->Username = "*****";
$mail->Password = "****";
$mail->Port = 587;
$mail->SMTPSecure = "tls";
$mail->From = "******";
$mail->FromName = "Jurgen Hof";
$mail->addAddress("testingaccount23#gmail.com", "Tester");
$mail->isHTML(true);
$mail->Subject = 'Test Leave Application';
$mail->Body = 'Test.';
$mail->AddAttachment("/var/www/html/leaveform/AlpineLeaveApplication.pdf");
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Email Sent Successfully!';
?>
You're setting lots of SMTP properties, but not actually asking it to send via SMTP! Add this:
$mail->isSMTP();
Your browser will then explode with debug output, so I suggest you turn down SMTPDebug = 2.

PHPMailer - Multiple text messages to single recipient

I have a portion of my project that grabs some customer information from a DB and sends a text-message to a salesman, using PHP Mailer. Some of the customer info included:
Name
Phone
Phone 2
Address
City
State
Zip
Notes
As you can imagine, 160 characters won't cut it. I need to be able to send at least two text messages to the same number within the same function.
I have a single text message working, using PHP Mailer. I will post the relevent code below:
db_functions.php:
function send_text($name, $message){
require 'class.phpmailer.php';
$to = 'xxxxxxxxxx#vtext.com';
$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; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxxxx#gmail.com";
$mail->Password = 'xxxxxxx';
$mail->SetFrom('xxxxxx#gmail.com');
$mail->Subject = $name;
$mail->Body = $message;
$mail->AddAddress($to);
$mail->Send();
$mail->ClearAddresses();
return;
}
assign_lead.php:
include 'mysql_login_pdo.php';
include '../functions/db_functions.php';
if (!isset($_POST['leadID'])) {
return;
} else {
$leadID = $_POST['leadID'];
}
if (!isset($_POST['salesID'])) {
return;
} else {
$salesID = $_POST['salesID'];
}
//DB FUNCTIONS
db_assignLead($leadID, $salesID);
$message = db_assignLeadNote($leadID);
$name = db_assignLeadName($leadID);
db_assignAddNote($leadID, $message);
//-------------------------This is the problematic area---------------------
//SEND TEXT MESSAGE(s)
send_text($name, $message);
$message = '8104124200_230 N Main St_Davison_48423';
send_text($name, $message);
As you can see, I want a text message to send to a salesman with the customer's name and a note about the customer. Then, I want to send a second text message with the customer's name and address information. I've used a placeholder of '8104124200_230 N Main St_Davison_48423' for now, but it will be replaced by a function that searches for the address info in the DB.
The first text message sends fine, but the second refuses to send. I made it work once by using a 20-second sleep, but from what I've read, it may be unnecessary. Also, the 20-second sleep was completely unreliable.
As always, I appreciate any help.
I ended up doing the following, which worked. I'm hoping it won't cause issues down the road, but if it does, I'll come back and update this post.
Basically, instead of calling the send_text() function twice, I sent the message twice within the same function:
function send_text($name, $message){
include 'class.phpmailer.php';
//$to = $_POST['to'];
//$password = $_POST['password'];
$to = 'xxxxxx#gmail.com';
$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; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxxxx#gmail.com";
$mail->Password = 'xxxxxx';
$mail->SetFrom('xxxxxx#gmail.com');
$mail->Subject = $name;
$mail->Body = $message;
$mail->AddAddress($to);
$mail->Send();
//Send the second message
$mail->Body = 'Testing second message';
$mail->Send();
//End Second message
return;
}

how to remove root user from gmail message sent through phpmailer

I manage to sent message to my gmail account using phpmailer library through local
host in my xampp but the message has 'root user' shown instead of the gmail account set to $mail->SetFrom how to remove this 'root user' and show the appropriate name.
<?php
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true;
$mail->Mailer = "smtp";
$mail->SMTPSecure = "tls";
$mail->Username = "georgediamei1234#gmail.com";
$mail->Password = "password";
$mail->Host = 'ssl://smtp.gmail.com'; // SMTP server
$mail->Port = 465;
$mail->FromName ='georgediamei1234#gmail.com';
$mail->AddAddress("nowtonkhurai#gmail.com");
$mail->Subject = "";
$mail->Body = "Hello, <b>Is photo attached</b>!\n\n This message uses HTML entities!";
$mail->addStringAttachment("$string","08DsAlg.pdf", "base64","application/pdf");
if($mail->send()) {
echo "Email sent";
} else {
echo 'Not send '.$mail->ErrorInfo;
}
?>
$mail->setFrom('from#example.com', 'Mailer');
I also got stumbled with that root problem. You have to set a mail id and a name in setFrom property. As the question was asked 2 years ago it might not be helpful to you but it might help others who get stuck with the same stuff.
use both function for name and email address like this :
$mail->From = "from#example.com";
$mail->FromName = "Mailer";

Categories