PHPMailer not working on Web Hosting - php

I have been able to send emails using xampp, but when I tried to use it in my online server it doesn't seem to work. I tried changing the values on what was written on my email account information. It doesn't seem work, I was hoping for any guidance or help for anyone who has encountered this problem.
This is the email account details
then this is my code for my mailer.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->Username = 'email';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->Port = 465;
$mail->setFrom('email', 'Test Message');
$mail->addAddress("email");
$mail->isHTML(true);
$mail->Subject = 'This is the Subject';
$mail->Body = 'This has been sent';
$mail->AltBody = 'This has been sent';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: <br>', $mail->ErrorInfo;
}
I get the error SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed.
I am still kinda new to this, so sorry in advance for anything. I feel I am soo close to getting it work haha!
Thanks in advance for any help! :D

it's so simple... Use this code:
<?php
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);
$name = 'Name to be displayed!';
$message = 'Never Give Up!';
$subject = 'Test mail!';
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'yourmail#gmail.com';
$mail->Password = 'youpass';
$mail->SMTPSecure = 'tls';
$mail->addReplyTo($to, $name);
$mail->setFrom($to, $name);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->msgHtml($message);
$mail->send();
?>
Hope you added PHPMailer Library in Root Directory if not then please check the path also

Related

PHPmailer unable to connect

I've been trying to get PHPmailer to work but I stumble constantly on the same error. I made a gmail account and normally all settings should be right.
I get the following error: Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.
This is the code I'm currently using:
<?php
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);
try {
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'mymail.com';
$mail->Password = 'mypassword';
$mail->SMTPSecure = 'ssl';encryption
$mail->Port = 465;
$mail->setFrom('mymail', 'Dev4');
$mail->addAddress('anothermail');
$mail->addReplyTo('no-reply#gmail.com', 'Information');
$mail->isHTML(true);
$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';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
You just need to fix the following two lines...
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
and add
$mail->Mailer = 'smtp';

Change Message-ID PHPMailer

Hello I am making a php based mailing application that will connect with a external smtp server and send emails. Now I have managed to match everything but the Message-ID's #domain-name and Sender domain name are not matching...
This is the result I am getting :
Wrong Message ID Header
and this is the result I should be getting (this email is sent from Mailwizz connected with the same SMTP server I am trying to connect with my application)
Expected Message ID Header
send.php file which I am using to connect with SMTP using PHPMailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp*****************';
$mail->SMTPAuth = true;
$mail->Username = 'notify#send.al***********';
$mail->Password = '**********';
$mail->SMTPSecure = 'tls';
$mail->Port = 80;
$mail->SetFrom('jon#al***********','John Adams');
$mail->Sender = 'notify#send.al*********';
$mail->addAddress('*********#gmail.com');
$mail->Subject = 'Hello This is a TEST FROM SMTP';
$mail->isHTML(false);
$mail->Body = 'Hello let me know when its received';
$mail->addCustomHeader('X-Sender', 'notify#send.al**********');
$mail->XMailer=null;
$mail->MessageID = md5('HELLO'.(idate("U")-1000000000).uniqid()).'-'.$type.'-'.$id.'#send.al*********';
if(!$mail->send()){
echo 'Error is '.$mail->ErrorInfo;
}
else{
echo 'Message has been sent!';
}
?>
In my experience, you do not need to use addCustomHeader if you want to set the MessageID.
Assuming that you want to set the Message ID to be [random]#send.alok, then please use the following:
$mail->MessageID = "<" . md5('HELLO'.(idate("U")-1000000000).uniqid()).'#send.alok>';
Hence, please the following will be ok:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require './Exception.php';
require './PHPMailer.php';
require './SMTP.php';
$user='smtp_xxxxxx_user';
$pass='smtp_password';
$mail = new PHPMailer(true);
try {
$mail->CharSet ="UTF-8";
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.xxxxx.com';
$mail->Username = $user;
$mail->Password = $pass;
$mail->MessageID = "<" . md5('HELLO'.(idate("U")-1000000000).uniqid()).'#send.alok>';
$mail->setFrom('jon#alxxxx.com', 'Jon Al');
$mail->addAddress('jon#gmail.com');
$subject="test";
$message="test123";
$mail->Port = 25;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->send();
// echo 'Success';
} catch (Exception $e) {
//echo 'Failed';
}
?>
You may refer to the screen dump below for the result

PHPMailer redirecting to GitHub

When I try to run a simple PHPMailer test script it is redirecting me to GitHub site.
My script is given below. Can someone please help?
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../PHPMailer/src/PHPMailer.php';
$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 = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // or 587 or 465
$mail->IsHTML(true);
$mail->Username = "myaccountname";
$mail->Password = "mypassword";
$mail->setFrom('fromemailid', 'from name');
$mail->Subject = 'Test Mail';
$mail->Body = 'Test mail body';
$mail->AddAddress("someone#gmail.com");
if (!$mail->send())
{echo "Mailer Error: " . $mail->ErrorInfo;
return false;
} else {
return true;
}
?>
Which folder you exactly adding your code. As see your code

SMTP: CLIENT: 535 5.7.3 Authentication Unsuccessful

I'm trying to send an email every time someone inputs a form on my website but the SMTP isn't working. It keeps on showing me this error. The credentials of the email account are correct. Also, I have already tried SMTPAuth = False; but still the same result.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// 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;
//Load Composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->Port = 587;
$mail->SMTPSecure = 'TLS';
$mail->SMTPAuth = true;
$mail->Username = 'sender#test.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
//Recipients
$mail->setFrom('test#test.com', ' Services');
$mail->addAddress('recipient#test.com', $name); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Contact Us Message';
$mail->Body = 'Sent a message, This is the message :';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
header('Location: tempage.php');
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
What is going wrong? Thanks for your help.
I'm also struggling 2 days with same issue.
After i find this solution. It's working fine.
Try this.
$mail->Host = 'smtp.office365.com';
change to
$mail->Host = 'smtp.live.com';

PHPMailer XAMPP SMTP Authentication failed

I'm trying to use PHPMailer to send e-mail from my local virtual host on XAMPP with the code below. I have enabled extension=php_openssl.dll in php.ini, still I get the error messages below. Anyone knows why?
require_once 'PHPMailerAutoload.php';
require_once 'class.phpmailer.php';
require_once 'class.smtp.php';
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "xxx";
$mail->Password = "xxx";
$email = "xxx#gmail.com";
$name = "Test";
$email_from = "xxx#gmail.com";
$name_from = "Test";
$mail->AddAddress($email, $name);
$mail->SetFrom($email_from, $name_from);
$mail->Subject = "My Subject";
$mail->Body = "Mail contents";
try{
$mail->Send();
echo "Success!";
} catch(Exception $e){
echo "Fail - " . $mail->ErrorInfo;
}
Error output:
SMTP ERROR: Password command failed: 534-5.7.14
SMTP Error: Could not authenticate.
For those having issues with failing authentication using PHPMailer, and all settings and credentials are good, try using this:
$mail->AuthType = 'LOGIN';
in the PHP that is attempting to send the email.
I struggled for the longest, until I added this.
Cheers.

Categories