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();
}
};
Related
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();
}
I have a web app using PHP, MySQL, and PHPMailer to send emails. The issue I am having is PHPMailer will send some of the emails, but not always. I'm thinking maybe it is because different list of emails have data issues? Below is my code that loops through the email addresses and send some, but not all the emails. I have tried to catch errors, but I can't see any in the logs.
How can I debug what is going on, and why some don't send?
else if($current_version == 'PROD'){
date_default_timezone_set('America/New_York');
require 'PHPMailer-master/PHPMailerAutoload.php';
$pageURL = 'xxxxxx';
$subject = "xxxxx ";
$body = "xxxxxx \r\n";
$body .= "xxxxx \r\n \r\n";
$body_html = str_replace("\r\n","<br/>",$body);
try {
$mail = new PHPMailer();
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "xxx#xxxx.com";
$mail->Password = "xxxx";
$mail->setFrom('xxxxx#xxxx.com', 'xxxx');
$mail->addReplyTo('xxxx#xxxx.com', 'xxxx');
$mail->Subject = $subject;
$mail->Body = $body_html;
$mail->AltBody = $body;
$sql_email_list = "SELECT email FROM tbl_email
WHERE distro_name='xxxxs'
AND is_active='Y' ";
$result_email_list = mysql_query($sql_email_list);
$num_email_list = mysql_num_rows($result_email_list);
$lost_email_list = mysql_result($result_email_list,0,'email');
$lost_email_array = explode(";",$lost_email_list);
foreach ($lost_email_array as $key => $val){
$mail->AddAddress($val);
if($carrier_email_flag_global == 'ON'){
$mail->send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
echo '<hr>Email Sent to: '.$val;
}
}
$sql_email_one = "SELECT table2.email
FROM table1
INNER JOIN table2 ON table1.cntct = table2.cntct_id
WHERE id='$id' ";
$result_email_one = mysql_query($sql_email_one);
$num_email_one = mysql_num_rows($result_email_one);
for($iiz=0;$iiz<$num_email_one;$iiz++){
$one_email = mysql_result($result_email_one,$iiz,'email');
$mail->AddAddress($one_email);
if($carrier_email_flag_global == 'ON'){
$mail->send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
echo '<hr>Email Sent to: '.$val;
}
}
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
echo '<hr>ERROR 001';
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
echo '<hr>ERROR 002';
}
}
Does anybody have a working example how I can work with PHPMailer in Laravel 5? In Laravel 4 it was quiet simple to use but the same method doesn't work in L5. Here it is what I did in L4:
Added in composer.json:
"phpmailer/phpmailer": "dev-master",
And in the controller I've used it like this:
$mail = new PHPMailer(true);
try {
$mail->SMTPAuth(...);
$mail->SMTPSecure(...);
$mail->Host(...);
$mail->port(...);
.
.
.
$mail->MsgHTML($body);
$mail->Send();
} catch (phpmailerException $e) {
.
.
} catch (Exception $e) {
.
.
}
But it doesn't work in L5. Any idea? Thanks!
Well there are multiple mistakes i think...
This is a working example of sending mail with PhpMailer in Laravel 5. Just tested it.
$mail = new \PHPMailer(true); // notice the \ you have to use root namespace here
try {
$mail->isSMTP(); // tell to use smtp
$mail->CharSet = "utf-8"; // set charset to utf8
$mail->SMTPAuth = true; // use smpt auth
$mail->SMTPSecure = "tls"; // or ssl
$mail->Host = "yourmailhost";
$mail->Port = 2525; // most likely something different for you. This is the mailtrap.io port i use for testing.
$mail->Username = "username";
$mail->Password = "password";
$mail->setFrom("youremail#yourdomain.de", "Firstname Lastname");
$mail->Subject = "Test";
$mail->MsgHTML("This is a test");
$mail->addAddress("recipient#anotherdomain.de", "Recipient Name");
$mail->send();
} catch (phpmailerException $e) {
dd($e);
} catch (Exception $e) {
dd($e);
}
die('success');
And of course, you need to do a composer update after adding the depency to composer.json
However, i would prefer the laravel built in SwiftMailer.
http://laravel.com/docs/5.0/mail
In Laravel 5.5 or Above, you need to do the following steps
Install the PHPMailer on your laravel Application.
composer require phpmailer/phpmailer
Then goto your controller where you want to use phpmailer.
<?php
namespace App\Http\Controllers;
use PHPMailer\PHPMailer;
class testPHPMailer extends Controller
{
public function index()
{
$text = 'Hello Mail';
$mail = new PHPMailer\PHPMailer(); // create a n
$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 = "testmail#gmail.com";
$mail->Password = "testpass";
$mail->SetFrom("testmail#gmail.com", 'Sender Name');
$mail->Subject = "Test Subject";
$mail->Body = $text;
$mail->AddAddress("testreciver#gmail.com", "Receiver Name");
if ($mail->Send()) {
return 'Email Sended Successfully';
} else {
return 'Failed to Send Email';
}
}
}
i am trying to send email to user and to admin at the same time using phpmailer but only one email is sent.. either to user or to admin..
i have tried every possible way with no luck. please help
below is my code.
$html.="This is my message to user";
$bcc = "xxx#gmail.com";
$to= $clientemail;
$subject = "This is my subject;
$host = $SmtpServer;
$username = $SmtpUser;
$password = $SmtpPass;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "$host"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $username; // SMTP username
$mail->Password = $password; // SMTP password
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
try {
$mail->From = $from;
$mail->FromName = $Name;
$mail->AddAddress(''.$to.'');
$mail->AddBCC('xxx#gmail.com');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $html;
$mail->AltBody = $html;
$mail->Send();
// echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
$message="Thank you for your request.";
$html="this is my second message to admin";
$bcc = "xxx#gmail.com";
$to= $admin;
$subject = "message to admin;
$host = $SmtpServer;
$username = $SmtpUser;
$password = $SmtpPass;
$mail2 = new PHPMailer();
$mail2->IsSMTP();
$mail2->Host = "$host"; // specify main and backup server
$mail2->SMTPAuth = true; // turn on SMTP authentication
$mail2->Username = $username; // SMTP username
$mail2->Password = $password; // SMTP password
$mail2->Port = 587;
$mail2->SMTPSecure = 'tls';
try {
$mail2->From = $from;
$mail2->FromName = $from;
$mail2->AddAddress(''.$to.'');
$mail2->AddBCC('xxx#gmail.com');
$mail2->WordWrap = 50;
$mail2->IsHTML(true);
$mail2->Subject = $subject;
$mail2->Body = $html;
$mail2->AltBody = $html;
$mail2->Send();
// echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
I'm not sure if you want to send the same email or not to differents persons, but the first improvement to do is to factorize your code.
// here an example of a function to send the SAME email to differents persons
// $emails is an array
function sendMail($emails, $subjects, $msg) {
$mail = new PHPMailer();
$mail->IsSMTP();
try {
$mail->Host = "$host"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $username; // SMTP username
$mail->Password = $password; // SMTP password
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->From = $from;
$mail->FromName = $Name;
foreach ($emails as $to) {
$mail->AddAddress($to);
}
$mail->AddBCC('xxx#gmail.com');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $html;
$mail->AltBody = $html;
if(!$mail->Send()) {
// log if needed
}
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
}
now you can use this function with differents messages or subjects... whatever you want!
EDIT :
to send different message to users and admins :
//settings for admin
$emails = array('admin#mail.com', 'admin2#mail.com'); // or just array('admin#mail.com');
$subjects = 'my sub';
$msg = 'my msg';
sendMail($emails, $subjects, $msg);
//settings for users
$emails = array('user#mail.com');
$subjects = 'different or not sub';
$msg = 'different or not msg';
sendMail($emails, $subjects, $msg);
I am trying to send mail and it just brought me to blank page. The echo had not been hit and i couldn't see the error.
try {
$mail->SetFrom($email, $name);
$mail->SMTPDebug = 2;
$address = "xx#gmail.com";
$mail->AddAddress($address, "xx");
$mail->Subject = "Contact Form Submission | " . $name;
$mail->MsgHTML($email_body);
$mail->SMTPSecure = "tls";
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'xx#gmail.com';
$mail->Password = 'password1234';
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
else
echo 'sent';
//header("Location: contact.php?status=thanks");
} catch (phpmailerException $e) {
echo 'error1';
} catch (Exception $e) {
echo 'error2';
}
As mentioned, you should turn error reporting on with the following two lines on the top of your script:
ini_set('display_errors', 1);
error_reporting(E_ALL);
Also, have you included the SMTP class of PHPMailer, since you're sending mail through SMTP? The file "class.smtp.php" should be placed in the same directory as "class.phpmailer.php" - if that is missing, then PHPMailer will error out when it's trying to include it