I am using slim framework to create api's for android app.In CreateUser api, i am sending mail to user using phpmailer.Phpmailer is sending the mails to user perfectly but my array response is returning null array response due to it.When i remove the code of phpmailer response is correct and when i add it ,it generates null reponse.
Here is my code in Dbhandler.php:
$app->post('/requestPreview', function() use ($app) {
verifyRequiredParams(array('fname', 'lname', 'email','event_code','app_version'));
global $user_id;
$response = array();
$fname = urldecode($app->request->post('fname'));
$lname = urldecode($app->request->post('lname'));
$email = urldecode($app->request->post('email'));
$event_code = urldecode($app->request->post('event_code'));
$app_version = urldecode($app->request->post('app_version'));
$db = new DbHandler();
//$db->sendmail($email);
$res = $db->signup($fname, $lname, $email,$event_code,$app_version);
if ($res['status']=="one") {
//$password=$res['password'];
$response["event_code"] = $event_code;
$response["email"] = $email;
$response["fname"] =$fname;
$response["lname"] = $lname;
$response["CreateUser"] = true;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "abc.abc#gmail.com";
$mail->Password = "***********";
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->From = "abc.abc#gmail.com";
$mail->AddAddress($email);
$mail->isHTML(true);
$mail->Subject = "Welcome to www.abc.com .Your Account has been created and your password is:";
$mail->Body = "Welcome to www.abc.com .Your Account has been created and your password is:";
$mail->send();
} else if ($res['status']=="two"){
$response["error"] ="already_in_system";
$response["fname"] = null;
$response["lname"] = null;
}
echoRespnse(200, $response);
});
Update to latest PHPMailer and base your code on an up to date example, then read the troubleshooting docs. It's very likely you're running into Google auth issues.
Your PHPMailer code never touches $response, suggesting it may be exiting prematurely due to an error, so trace what's happening in your function.
Related
I have a script that I'm running using phpmailer - it's supposed to send 2 emails - a verification email and a request email. for some reason it's sending the verification email twice. I've gone over the code 100 times and I can't figure out why. Relevant code is below - it's not nested in any loop or condition. the "code" is a get variable and "auth" is pulled from a database.
<?php
if ($code==$auth) {
$mailtitle="message 1";
$message="message text";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "mail.mysite.org";
$mail->From = "authorize#mysite.org";
$mail->FromName = "mysite";
$mail->AddAddress($dre);
$mail->SMTPAuth = "true";
$mail->SMTPSecure = 'tls';
$mail->Username = "authorize#mysite.org";
$mail->Password = "thepassword";
$mail->Port = "587";
$mail->Subject = $mailtitle;
$mail->Body = $message;
$mail->isHTML(true);
$mail->WordWrap = 180;
$mail->send();
$mailtitle="message 2 title";
$message="message text";
$mail1 = new PHPMailer();
$mail1->IsSMTP();
$mail1->Host = "mail.mysite.org";
$mail1->From = "authorize#mysite.org";
$mail1->FromName = "mysite";
$mail1->AddAddress('authorize#mysite.org');
foreach ($pea as $px) {
$mail1->addBCC($px);
}
foreach ($sea as $sx) {
$mail1->addBCC($sx);
}
$mail1->SMTPAuth = "true";
$mail1->SMTPSecure = 'tls';
$mail1->Username = "authorize#mysite.org";
$mail1->Password = "thepassword";
$mail1->Port = "587";
$mail1->Subject = $mailtitle;
$mail1->Body = $message;
$mail1->isHTML(true);
$mail1->WordWrap = 180;
$mail1->send();
}
?>```
You may disable the BCC by REMOVING the following
foreach ($pea as $px) {
$mail1->addBCC($px);
}
foreach ($sea as $sx) {
$mail1->addBCC($sx);
}
So I am trying to set up a contact us page on a website i am creating but PHPMailer just refuses to work for me.
From what I can see, it just isn't recognizing the PHPMailer class at all and I am not really sure why. Here is my PHP code for the form to send the email, as far as i can tell i have correctly installed PHPMailer/Composer, and have done everything else correctly, but i still get errors when trying to use PHPMailer at all. "PHPMailer cannot resolved to a type" and "The import PHPMailer\PHPMailer\PHPMailer cannot be resolved"
<?php
use PHPMailer\PHPMailer\PHPMailer;
require_once './vendor/autoload.php';
// Bunch of code validating all the variables from the form
function send_mail($toEmail, $endSubject, $endMessage, $endName) {
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = "smtp.domain.com";
$mail->SMTPAuth = true;
$mail->Username = "LOGIN EMAIL"; // Email
$mail->Password = "PASSWORD"; // Email password
$mail->SMTPSecure = "tls";
$mail->Port = "587";
$mail->From = "example#domain.com"; //RANDOM EMAIL TO SEND THESE MESSAGES TO OUR EMAIL
$mail->FromName = $endName; //CUSTOMERS NAME
$mail->addAddress($toEmail); //WHOEVER WE ARE SENDING THESE EMAILS TO
$mail->isHTML(true);
$uri = 'http://'. $_SERVER['HTTP_HOST'];
$mail->Subject = $endSubject;
$mail->Body = $endMessage;
if($mail->send()) {
return true;
} else {
return false;
}
}
?>
Inconsistent duplicate emails occurring when using php mailer.
Function that mails:
function SendEmail($to,$cc,$bcc,$subject,$body) {
require( GetPHPMailPath() );
$mail = new PHPMailer();
$addresses = explode(',', $to);
foreach ($addresses as $address) {
$mail->AddAddress($address);
}
if($cc!='') {
$mail->addCustomHeader("CC: " . $cc);
}
if($bcc!=''){
$mail->addCustomHeader("BCC: " . $bcc);
}
$mail->IsSMTP();
$mail->SMTPAuth = true; // turn on 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;
$mail->Username = "email#email.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "email"; //Reply to this email ID
$name=$email;
$mail->From = $webmaster_email;
$mail->FromName = "Service";
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = $subject;
$mail->Body = $body;
return $mail->Send();
}
How I am calling the function:
echo SendEmail($toAddress,$ccAddress,$bccAddress,$subject,$body);
The really odd part about this whole ordeal is that it is inconsistent which means there may be nothing wrong with the code but the connection to gmail?
Any ideas maybe its a php.ini problem?
This was a lag related issue.
PHPMailer functioned properly. User was sending duplicate requests. Fixed by adding comparison check with MySQL database records.
I am trying to setup PHPMailer for a customer. He has his own mail server located at a certain IP address. When asked to give me the information to send email through the system, he gave the following:
Host: xx.xxx.x.x
Port: 25
Domain: mydomain.local
Username: myemail#mydomain.local
Password: <myemailpassword>
From: myemail#anotherdomain.xx
(Which he confirmed is being used for external email sending)
I tried to setup PHPMailer by setting the parameters to the exact namings above.
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "xx.xxx.x.x";
$mail->Port = 25;
$mail->Username = "myemail#mydomain.local";
$mail->Password = <myemailpassword>;
$mail->SetFrom('myemail#anotherdomain.xx', 'Webname');
$mail->[...]
I got the following error:
Failed to connect to server (0)
So I try to send an email through telnet to check if it's the customer's email server or the PHPMailer settings:
telnet xx.xxx.x.x 25
It goes through, I'm connected to the server.
helo mydomain.local
I'm getting 'Hello' as a reply. This leads me to believe it might be the PHPMailer settings that are wrong here.
I also try not using SMTP:
$mail->Host = "ssl://xx.xxx.x.x";
$mail->Port = 25;
$mail->Username = "myemail#mydomain.local";
$mail->Password = "password";
$mail->SetFrom('myemail#anotherdomain.xx', 'Webname');
$mail->[...]
Again no go. Am I going about this wrong? I'm only familiar with setting up PHPMailer to use Gmail before so I'm at a loss as to what could be the issue because I'm using a 'personal' email server.
Thanks Loadparts for your assistance.
I'm still not sure what the issue was but it seems it has resolved itself. It might have been from the email server side because coding wise, I didn't change anything. This is the final code I used.
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Port = 25;
$mail->Host = "xx.xxx.x.x"; // SMTP server
$mail->Username = "myemail#mydomain.local";
$mail->Password = <myemailpassword>;
$mail->From = "myemail#anotherdomain.xx";
$mail->FromName = <Web_Name>;
$mail->AddAddress("email#domain.com");
$mail->Subject = <Subject>;
$mail->AltBody = <Alt_Body>
$mail->WordWrap = 80;
$body = "test message";
$mail->MsgHTML($body);
$mail->IsHTML(true);
$mail->Send();
I use a test function that I know works 100% to test the email servers when using PHPMailer.
I'm not sure why you are having your problem, but try to use the function I have ( I know it's messy but it does the trick). Just replace all the XXXX with your info and make sure you have both class.phpmailer.php and class.smtp.php in the same folder.
<?php
error_reporting(E_ALL);
$toemail = 'XXXX';
$toname = 'XXXX';
$subject = 'Testing Email Sending...';
$bodyhtml = '<H1>yeah</h1>';
$bodytext = 'heres Hoping it works';
$fromemail = 'XXXX';
$fromname = 'XXXX';
var_dump(sendemail($toemail,$toname,$subject,$bodyhtml,$bodytext,$fromemail,$fromname));
function sendemail($toemail,$toname,$subject,$bodyhtml,$bodytext,$fromemail,$fromname)
{
require_once("class.phpmailer.php");
$mail = new phpmailer();
$mail->IsSMTP();
$mail->From = $fromemail;
$mail->FromName = $fromname;
$mail->Host = "XXXX";
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "XXXX"; // SMTP username
$mail->Password = "XXXX"; // SMTP password
$mail->Port="25";
$mail->SMTPDebug=true;
if(strlen($bodyhtml)>0) {
$mail->Body = $bodyhtml;
$mail->IsHTML(true);
}
else if(strlen($bodytext)>0){
$mail->Body = $bodytext;
}
if(strlen($bodytext)>0 && strlen($bodyhtml)>0){
$mail->AltBody = $bodytext;
}
$mail->AddReplyto($fromemail,$fromname);
$mail->Subject = $subject;
// Check if multiple recipients
if(preg_match("/;/",$toemail))
{
$tmp_email=preg_split("/;/",$toemail);
$tmp_contact=preg_split("/;/",$toname);
$mail->AddAddress($tmp_email[0], $tmp_contact[0]);
// echo "<!-- multi email:".$tmp_email[0]." contact:".$tmp_contact[0]." -->\n";
for($j=1;$j<count($tmp_email);$j++)
{
if(preg_match("/\#/",$tmp_email[$j]))
{ $mail->AddCC($tmp_email[$j], $tmp_contact[$j]);
// echo "<!-- multi email cc:".$tmp_email[$j]." contact:".$tmp_contact[$j]." -->\n";
}
}
}
else{
$mail->AddAddress($toemail, $toname);
}
$error= false;
if($mail->Send()){
$error =true;
}
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
return $error;
}
If this doesn't work, my first try would be using port 80 - which usually isn't blocked, then you can work on getting SSL to work.
PS: because it's a local domain, you may want to consider adding the domain to your /etc/hosts just to be sure.
Best of Luck!
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;
}