PHPMailer connection error in using gmail smtp - php

I'm having error in using the PHPMailer in github. I tried to modify the ones on youtube and still not working
My error
My code
PS: Changed the username/pw to sample

use this code :
$mail = new PHPMailer(true);
$mail->IsSMTP();
try {
$mail->Host = "mail.yourDomain.com";
$mail->SMTPAuth = true;
$mail->Username = "username";
$mail->Password = "pass";
$mail->AddAddress($receiverMail, '');
$mail->SetFrom('test#yoerdomain.com', 'my company name');
$mail->Subject = 'subject';
$mail->AltBody = 'Alternate text'
$mail->CharSet = 'UTF-8';
$mail->ContentType = 'text/html';
$msg="<body style='text-align: right;direction: rtl'>"."your text body"."<br>< company name ></body></html>";
$mail->MsgHTML($msg);
$mail->Send();
}catch (phpmailerException $e) {
echo $e->errorMessage();
}

Related

Save PHPMailer debug to database not working

I have a php script that sends emails using PHPMailer see below:
$mail = new PHPMailer(true);
try {
$debug = NULL;
$mail->Debugoutput = function($str, $level) {
$GLOBALS['debug'] .= "$level: $str\n";
};
$mail->isSMTP();
$mail->Host = 'xxxxxxx';
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->Username = 'xxxxxx';
$mail->Password = 'xxxxxx';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('xx-xx#xx.com', 'xxx');
$mail->addAddress('xx-xx#xx.com');
$mail->isHTML(true);
$mail->Subject = 'xx';
$email_content = '<p>test</p>';
$mail->Body = $email_content;
$mail->send();
fncSaveLog($debug);
} catch (Exception $e) {
fncSaveLog($mail->ErrorInfo);
}
the fncSaveLog is a function that saves debug info to database but I see always NULL for debug in my DB, it seems the output is never caught. Any idea how to fix this please?
Thanks.
Your setting NULL to save into the db. Try the below instead:
$mail = new PHPMailer(true);
try {
$mail->Debugoutput = function($str, $level) {
$GLOBALS['debug'] .= "$level: $str\n";
};
$mail->isSMTP();
$mail->Host = 'xxxxxxx';
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->Username = 'xxxxxx';
$mail->Password = 'xxxxxx';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('xx-xx#xx.com', 'xxx');
$mail->addAddress('xx-xx#xx.com');
$mail->isHTML(true);
$mail->Subject = 'xx';
$email_content = '<p>test</p>';
$mail->Body = $email_content;
$mail->send();
$debug = $GLOBALS['debug'];
fncSaveLog($debug);
} catch (Exception $e) {
fncSaveLog($mail->ErrorInfo);
}
To throw an exception in Php you have to use
throw new Exception('Your exception message');
Then your catch statement will show you your error message,
use this to look for errors.

SMTP Error: Could not authenticate. SMTP connect() failed. PHPMailer

I am composing a email function for sending newsletters to many recipients in PHP.
<?php
require_once('class.phpmailer.php');
require_once('class.smtp.php');
$mailer = new Mailer();
Class Mailer{
function __construct(){
$dbCon = mysqli_connect("localhost", "root", "", "dbppa");
}
public function sendMail($e,$message,$subject){
$mail = new PhpMailer;
$body = $message;
$subject = "PPA Newsletter";
$clientName = $e;
$mail->SMTPDebug = 2;
$mail->CharSet = 'utf-8';
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'tls://smtp.gmail.com:587';
$mail->Port = 587;
$mail->Username = "myemail#mail.com";
$mail->Password = "password";
$mail->SMTPSecure = 'tls';
$mail->setFrom("myemail#mail.com","PPA Administrator");
$mail->addReplyTo('myemail#mail.com', 'PPA Admin');
$mail->Subject = $subject;
$mail->AltBody = "This is the official news letter from PPA ";
$mail->Body = $body;
$address = $e;
$mail->AddAddress($address, $clientName);
if(!$mail->Send()){
echo "NOT SENT";
}
else{
echo "SENT";
}
}
}//class mailer
?>
In the code given above. I provided correct email username and email password. From Gmail. But it does not send the given email message to the recipient(s). It always return an ErrorInfo of SMTP Error:
Could not authenticate. SMTP connect() failed.
Why? Please help.
You can use an old version of phpmailer, it will work with your code. I use version 5.2.8.

phpmailer error: invalid address

I am trying to build a mail function with php, but it's justing showing "Invalid address:" after execute. Pleas help.
Below is my code:
require 'PHPMailerAutoload.php';
require 'class.phpmailer.php';
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 2; // 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;
$mail->Username = 'myemail#gmail.com';
$mail->Password = 'mypassword';
$mail->From = "myemail#gmail.com";
$mail->FromName = "Web Site";
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->addAddress('myfriend#gmail.com');
$mail->AddReplyTo('myfriend#gmail.com');
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
Try This
Please write the below line.
$mail->CharSet = "UTF-8"; // To support special characters in SMTP mail
//$mail->SMTPDebug = 2; Comment this line of code
I hope this will help you. Good Luck

php mailer function not working on aws

I have used the PHP mailer function to send a mail. This code has worked on XAMPP and 000webhost.com, but its not working on aws.
<?php
require("\PHPMailer\PHPMailer\class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
//$mail->SMTPDebug = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "****#gmail.com"; // Enter your SMTP username
$mail->Password = "************"; // SMTP password
$webmaster_email = $_POST['email']; //Add reply-to email address
$email= "***#gmail.com"; // Add recipients email address
$name= "Recipient's name"; // Add Your Recipient’s name
$mail->From = $webmaster_email;
$mail->FromName = $_POST['name'];
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,$_POST['name']);
$mail->WordWrap = 500; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "website inquiry - Support";
$mail->Body = nl2br($_POST['message']);
$mail->AltBody = nl2br($_POST['message']); //Plain Text Body
if($mail->Send())
echo 1;
else
echo 0;
?>
Please tell me where I am going wrong.
When I am executing this code on aws..its echoing 0
Thanks in advance!
Try with throwing errors by adding "true" to constructor:
try {
new PHPMailer(true);
...your code here...
}
catch (phpmailerException $e) {
echo $e->errorMessage();
}
So perhaps there are some more informations about the error.

PHPMailer - AUTH not accepted

I'm trying to run PHPMailer for an internal contact form and I am getting the error ERROR: AUTH not accepted from server. Here is my current code..
require_once('/includes/class.phpmailer.php');
include("/includes/class.smtp.php");
if (isset($_POST['submit'])) {
$mail = new PHPMailer(true);
$mail->IsSMTP();
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
try {
$mail->Host = "192.168.6.6";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = True;
$mail->Username = 'xxxxx';
$mail->Password = '***';
$mail->Port = 25;
$mail->AddAddress('xxx#xxx.com', 'xxxxx');
$mail->SetFrom($email);
$mail->Subject = 'New message from Contact Form';
$mail->Body = $message;
$mail->Send();
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
};
This error basically means that your attempt to authenticate was rejected by the remote server. Different PHPMailer settings (well SMTP settings) are required by different remote mail servers.
This could be caused by
Using the wrong port
Using the wrong host
Incorrect user/pass
Incorrect SMTPSecure
Example SMTP setup:
Gmail: use of phpmailer class
Hotmail: phpmailer with hotmail?
If you are using this internally, you may not need to use SMTP authentication at all, depending on your server settings. Try this and see if it works:
require_once('/includes/class.phpmailer.php');
include("/includes/class.smtp.php");
if (isset($_POST['submit'])) {
$mail = new PHPMailer(true);
$mail->IsSMTP();
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
try {
$mail->Host = "192.168.6.6";
$mail->SMTPDebug = 0;
$mail->SMTPAuth = False;
$mail->Port = 25;
$mail->AddAddress('xxx#xxx.com', 'xxxxx');
$mail->SetFrom($email);
$mail->Subject = 'New message from Contact Form';
$mail->Body = $message;
$mail->Send();
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
};

Categories