I am trying to send bulk mail. And It worked but the problem is when I am checking on Gmail this shows. I can use Bcc and solve this issue but I want to for learning purposes.
------------------------------------------
from: example <example#gmail.com>
to: example1#gmail.com,
example2#gmail.com,
example2#gmail.com
date: Dec 19, 2021, 1:18 AM
subject: Testing Mail
mailed-by: example
signed-by: example
security: Standard encryption (TLS) Learn more
---------------------------------------------
In the recipient why all subscriber's mail is showing in "to". Please help me to fix it. I want to show specific subscriber mail.
Here is my code
<?php
if(isset($_POST['sendmail'])) {
$status=1;
$sql = "SELECT * FROM subscribers WHERE status=? ORDER BY id ";
$stmt = $conn2->prepare($sql);
$stmt->bind_param("s", $status);
$stmt->execute();
$result15 = $stmt->get_result();
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = EMAIL; // SMTP username
$mail->Password = PASS; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
if (mysqli_num_rows($result15)>0) {
while ($row = mysqli_fetch_array($result15)) {
$mail->AddAddress($row['email']); // Add a recipient
}
}
$mail->setFrom(EMAIL, 'example');
//$mail->addReplyTo(NOREPLY);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $_POST['subject'];
$mail->Body = '<div style="border:2px solid red;">This is the HTML message body <b>in bold!</b></div>';
$mail->AltBody = $_POST['message'];
if (!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
?>
Related
This question already has answers here:
How do you make sure email you send programmatically is not automatically marked as spam?
(24 answers)
Closed 3 years ago.
I am using PHPMAILER and I am fairly new in it.
I have used the following code but for some reason the email is being sent to spam. Please have a look at the code and tell me what I need to fix. (I am new to using email
<?php
require 'php-mailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isMail(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'kfhcareer#gmail.com.com'; // SMTP username
$mail->Password = 'password12345'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('kfhcareer#gmail.com', 'KFH Bahrain');
$mail->addAddress('kfhcareer#gmail.com', 'Joe User'); // Add a recipient
$mail->AddReplyTo( 'mailer#blah.com', 'Contact BLah' );
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'KFH house bahrain';
$mail->Body = 'This is tthe message <b>in bold!</b>';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
require("PHPMailer/class.phpmailer.php");
$sender = "sender#gmail.com"; //gmail of the sender
$password = "senderpassword"; //the password
$receiver = "receiver#gmail.com"; // the receiver email
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->SMTPDebug = 2;
$mail->From = $sender;
$mail->FromName = $sender;
$mail->Host = "smtp.gmail.com"; // specif smtp server
$mail->SMTPSecure= "ssl"; // Used instead of TLS when only POP mail is selected
$mail->Port = 465; // Used instead of 587 when only POP mail is selected 465
$mail->SMTPAuth = true;
$mail->Username = $sender; // SMTP username
$mail->Password = $password; // SMTP password
$mail->AddAddress($receiver, $receiver); //replace myname and mypassword to yours
$mail->AddReplyTo($receiver, $receiver);
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "This is Subject";
$mail->Body = "This is Body";
if(!$mail->Send()){
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
I want to send otp mail to user by fetching email value from database . I am using php auto mailer . It works when i use mail address directly but when i retrieve it from database i cant send it . Can anyone please help me with sending it .
function mailit()
{
$rand=rand(100000,999999);
$user=$_SESSION['user'];
$sql=mysqli_query($db,"UPDATE `doctor_login` SET `otp`='$rand' WHERE `doctor_id`='1'");
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myusername '; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587 ; // TCP port to connect to
$mail->setFrom('dont_reply_back#example.xyz');
$mail->addAddress('$row'); // Add a recipient
$mail->addReplyTo('dont_reply_back#example.xyz');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'THis is your OTP';
$mail->Body = 'Your otp is '.$rand;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
}
Code for retrieving and displaying of email
$mailadd=mysqli_query($db,"SELECT `doctor_email` FROM `doctor_login` WHERE `doctor_id`='1'");
$row = mysqli_fetch_array($mailadd,MYSQLI_ASSOC);
echo $row['doctor_email'];
$row2=$rowa['doctor_email'];
echo $row2;
i have used both $row and $row2 in $mail->addAddress but doesnt seem to work . What changes can i do . it works whwn i give a specific address
I think this is a scope issue. If you are calling this code in the main body of the code
$mailadd=mysqli_query($db,"SELECT `doctor_email`
FROM `doctor_login`
WHERE `doctor_id`='1'");
$row = mysqli_fetch_array($mailadd,MYSQLI_ASSOC);
Then I assume you call mailit() after this, so pass the data you want to use as a parameter to the mailit() function so it is available inside the function scope.
function mailit($row)
{
$rand=rand(100000,999999);
$user=$_SESSION['user'];
$sql=mysqli_query($db,"UPDATE `doctor_login` SET `otp`='$rand' WHERE `doctor_id`='1'");
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myusername '; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587 ; // TCP port to connect to
$mail->setFrom('dont_reply_back#example.xyz');
$mail->addAddress($row['doctor_email']); // Add a recipient
$mail->addReplyTo('dont_reply_back#example.xyz');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'THis is your OTP';
$mail->Body = 'Your otp is '.$rand;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
}
$mailadd=mysqli_query($db,"SELECT `doctor_email`
FROM `doctor_login`
WHERE `doctor_id`='1'");
$row = mysqli_fetch_array($mailadd,MYSQLI_ASSOC);
mailit($row);
You should have been getting errors from your original code so in future add the following:
ini_set('display_errors', 1);
ini_set('log_errors',1);
error_reporting(E_ALL);
// if you are using the `mysqli` API
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
to the top of your script. This will force any mysqli_ errors to generate an Exception that you can see on the browser as well as normal PHP errors.
My PHPMailer seems to work, but loops through the mailer only 6 times instead of 24 times. if loop used without mailer, it does return 24 addresses. I'm connected to an internal network with access from outside. can anyone clear up what is going on. ($link -> close()) is done at a later stage. also the 24 emails sent two 2 different email accounts(for testing purposes), both receive 3 emails from this phpMailer. found many posts regarding phpMailer, I haven't encountered this one so far.
if ($result = $link->query("SELECT Adres FROM Emails")) {
//Alle variabelen voor de mail
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->SMTPKeepAlive = true;
$mail->Host = 'smtp-mail.outlook.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '**********#outlook.com'; // SMTP username
$mail->Password = '**********'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('**********#outlook.com');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'APP';
$mail->Body = 'is this working??';
$mail->AltBody = 'is this working??';
while($row = mysqli_fetch_row($result)) {
$variable = $row[0];
$mail->addAddress($variable);
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
$mail->ClearAllRecipients( );
}
$result->close();
}
You're not checking that addAddress succeeds, for example if the email addresses are invalid. If you set SMTPDebug = 2, you can watch the SMTP conversation.
Check addresses like this:
$variable = $row[0];
if (!$mail->addAddress($variable)) {
echo "skipping invalid address $variable";
continue;
}
When I trying to send a email using PHP SMTP email server, following error has occurred.
SMTP Error: Could not authenticate. Message could not be sent.
Mailer Error: SMTP Error: Could not authenticate.
Following is my code that I have used.
function supervisorMail(){
global $error;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = "***#gmail.com";
$mail->Password = "****";
$mail->SetFrom("***#gmail.com", "Employee Leave Management System");
$userID=$_SESSION['userID'];
$select_query = mysql_query("SELECT * FROM employee WHERE emp_id = '$userID'");
$select_sql = mysql_fetch_array($select_query);
$name=$select_sql['manager_name'];
var_dump($name);
$select_query1 = mysql_query("SELECT email FROM employee WHERE emp_id='$name'");
$select_sql1 = mysql_fetch_array($select_query1);
$email=$select_sql1['email'];
var_dump($email);
$mail->Subject = "subject ";
$mail->Body = "something.";
$mail_to = $email;
$mail->AddAddress($mail_to);
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
}
How can I fixed this error.
The error message is very clear "Could not authenticate".
I would say you are correctly using the PHPMailer class, but just not filling in the correct gmail account details.
I suppose that in your question the fields username and password look like
$mail->Username = "#gmail.com";
$mail->Password = "";
just because you, obviously, don't want to share them, I would suggest to present them like this in the question
$mail->Username = "********#gmail.com";
$mail->Password = "********";
So if you are using the correct username and password there are other two things to check
Maybe your setting "Access for less secure apps" is turned off.
After you login from the web you can turn it on from this page
https://www.google.com/settings/u/0/security/lesssecureapps
If that is On, it might be possible you have 2-Step Verification enabled in your gmail account. If this is the case, you need to create an App specific password.
Follow this link for a step by step guide on how to do it
http://email.about.com/od/gmailtips/fl/How-to-Get-a-Password-to-Access-Gmail-By-POP-or-IMAP.htm
Please unable your less secure app
and then
Try to comment this line
//$mail->IsSMTP();
I hope it will work!
You may try to use google app password for authentication:
https://support.google.com/accounts/answer/185833?hl=en
2-Step-Verification & 2. App Passwords
As mentioned in comment, you need to enter valid gmail id and password.
Please refer below demo code.
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '*****001#gmail.com'; // SMTP username
$mail->Password = 'ma****02'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom('ravi******#gmail.com', 'Mailer'); // Add set from id
$mail->addAddress('er.ravihirani#gmail.com', 'Ravi Hirani'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//$mail->isHTML(true); // Set email format to HTML
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
I ran into this error message and wanted to post an answer in case it can help someone else. I have phpmailer installed with composer and am running php 7. The implementation of the phpmailer script that threw the error is in a php object method. The password variable, set in a config file elsewhere, needed to be declared as a global in the object context otherwise it did not contain the actual password and thus caused the authentication error..
global $password; // <- this added to make script work
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
// Set mailer to use SMTP
$mail->Host = '*******.com';
$mail->SMTPAuth = true;
$mail->Username = 'no-reply#*******.com';
$mail->Password = $password;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('no-reply#*******.com', '******* - DO NOT REPLY');
//$mail->isHTML(true);
$mail->AddAddress($user->email);
$mail->Subject = "*******";
$mail->Body = "*******";
$mail->send();
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
In my case it was because I didn't turn on the Less secure app access from here: https://myaccount.google.com/lesssecureapps
I have a PHPMailer script, and I've checked all the other threads that are similar to this, and tried all the solutions that follow the question. But here's the error I'm receiving
2014-01-03 02:25:16 SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed. Mailer Error: SMTP connect() failed.
And here's my script:
$body = "test";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // I have also tried tls
$mail->SMTPKeepAlive = true;
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // I have also tried 25
$mail->Username = "xxxx#gmail.com"; // GMAIL username
$mail->Password = "xxxxx"; // GMAIL password
$mail->SetFrom("johndoe#gmail.com","John Doe");
$mail->AddReplyTo("johndoe#gmail.com","John Doe");
$mail->Subject = "Booking from " . $_POST['person'] . ' at ' . $_POST['name'];
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "johndoe#hotmail.co.uk";
$mail->AddAddress($address, "John Doe");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
However I just cannot get it to work. I've tried to many different solutions. I'm even happy to use the default php mail however I just need it to send, the php mail wouldn't even get past the hotmail spam filters (by that I mean it didn't even send it to spam).
Any solutions or suggestions here? Thanks a lot!
Just comment this line:
$mail->IsSMTP();
Did you make sure your php.ini mail settings are the same on both your live server and your localhost server?
Install PHP Mailer From
https://github.com/Synchro/PHPMailer
And Read "A Simple Example"
<?php
require_once('PHPMailer/PHPMailerAutoload.php'); // Load your PHPMailerAutoload.php Correctly Here
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;// debugging: 1 = errors and messages, 2 = messages only // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'yourmail#gmail.com'; // SMTP username
$mail->Password = '**********'; // SMTP password
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // or 587 // TCP port to connect to
$mail->setFrom('fromemail#gmail.com', 'Vijay Rami');
$mail->addAddress('toemail#gmail.com', 'Vijay Rami'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//$mail->isHTML(true); // Set email format to HTML
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}