gmail disabled my account for sending too many emails through PHPMailer library - php

I'm using PHPMailer library to send 100 emails to my customers after every 5 mints. It was sending successfully. but when i had sent about 800 emails, gmail disabled my account because spamming or sending unwanted content. I was sending different email to every customer. Now i'm worried is there anything else to send too many emails for email marketing. here is my code
$sql="SELECT * FROM send_user_link LIMIT 100";
$result=$conn->query($sql);
while($row=$result->fetch_assoc()){
$email=$row['email'];
$name=$row['name'];
$id=$row['id'];
$code=$row['code'];
$e_subject="Support";
$msg="<h5>how are you doing??</h5>";
send_email_to_users($conn, $id, $email, $e_subject, $msg, $name);
}
function send_email_to_users($conn, $user_id, $to_email, $subject, $msg, $name){
$mail = new PHPMailer(); // Passing `true` enables exceptions
//Server settings
$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 = 'myacount#gmail.com'; // SMTP username
$mail->Password = '*******'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('myaccount#gmail.com', 'Support');
$mail->addAddress($to_email, $name); // 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');
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->AltBody = 'how are you doing??';
$user_id = mysqli_real_escape_string ($conn, $user_id );
if ($mail->send()) {
echo 'Message has been sent';
}
else{
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
Please help me to know is there anything to send to many emails without getting disabled from email service providers. Thank you

Related

Sending mails with different address as compared to the one used for smtp

I am trying to use phpmailer for sending mails. The mail is being sent but I am unable to change the from address.
try {
//Server settings
$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 = 'abc#gmail.com'; // SMTP username
$mail->Password = '****'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('xyz#yahoo.com');
$mail->addAddress('lmn#gmail.com' ); // 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');
// Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// Content
$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';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
When the mail is being sent the from address happens to be abc#gmail.com i.e. the one used in the smtp settings and not the one I have entered in mail->setFrom().How do i fix this?
This is a well-known issue that's covered in the PHPMailer docs - gmail does not allow you to send from arbitrary addresses because it's forgery. On top of that, Yahoo's DMARC policy is set to 'reject', so even if gmail allowed you to send from a yahoo address, any receiving server would reject the message anyway.
So the answer here is, no, you can't do this. If you want to send from a yahoo address, you must send through a Yahoo server.

PHPMailer, SMTP Error: Could not authenticate

I'm pretty sure that there are many questions out there regarding this problem.I don't seem to get my solution.
I'm using PHPMailer to mail through my php script.It was totally working few days back but today i tried doing it again,it didn't work.
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
$message="Hello there";
$mysqli->autocommit(FALSE);
$statement=$mysqli->prepare("INSERT INTO usertable VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
$zero=0;
$null=null;
$state='Delhi';
$statement->bind_param("sssssiiissssi",$email,$firstname,$lastname,$name,$password,$zero,$confirmcode,$zero,$null,$null,$null,$state,$zero);
$statement->execute();
try
{
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'ssl://smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'support#mydomain.com'; // SMTP username
$mail->Password = 'mvdctadwzkiibtot'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom('From #mydomain', 'mydomain');
$mail->addAddress($email); // Name is optional
$mail->addReplyTo('support#mydomain.com', 'For any queries mail back to support#foodquo.com');
// $mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50;
// $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 = 'Confirm your email ID';
$mail->Body = $message;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
}
catch(Exception $e)
{
$mysqli->rollback();
$response=array(
'status'=>'mailFailed',
'errorer'=>$e->getMessage(),
);
}
echo json_encode($response);
NOTE
I have my domain registered on Hostgator.
The email is present inside the cpanel and i can configure it from there.It doesn't have any password.So $mail->Password should not be an issue.
Given Below is the error that i have been getting , what could be the possible solution? Thanks!
SMPT Error :Could not authenticate.
if u are using gmail smptp service use:
$mail->Username = 'emailid#gmail.com'; // Gmail username
$mail->Password = 'mvdctadwzkiibtot'; //gmail password

Is sending email from yahoo's email address possible using gmails's smtp in phpmailer

<?php
require ("PHPMailer-master/PHPMailerAutoload.php");
$mail = new PHPMailer;
if(isset($_POST['submit']))
{
$email_query = "select email1,email2 from tbl_contacts where id = '1'";
$query_result = mysql_query($email_query);
$value = mysql_fetch_array($query_result);
$primary_email = $value['email1'];
$CC = $value['email2'];
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mail->IsSendmail(); // Set mailer to use SMTP
$mail->Host = "smtp.google.com"; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $email; // SMTP username
//$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom($email, $name);
//$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress($primary_email); // Name is optional
$mail->addReplyTo($email, '');
$mail->addCC($CC);
//$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 = $subject;
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
redirect('/contact.php?message=Your+Message+Has+Been+Sent!', 'location');
}
}
?>
I am using gmail's smtp and it works fine if I send an email with a gmail account but when I send an email with an yahoo account it displays a success message but the message does not get delivered. Is it possible to send email from yahoo's email using gmail' smtp? If yes, how can I do it? And if not, how can I solve the problem?
In general,
avoid declaring incorrect identity.
This might be possible from technical aspect; however in 2016 most email systems tend to protect themself by using multip. techniques - like SPF, DKIM and DMARC and that is both for sending and evaluating received emails.
If this is true for more and more email systems and ESP's it's not hard to imagine what ESP giants like Yahoo, GMAIL and others are doing to express their hate on incorrectly declared identities (mark as spam, reject at SMTP handshake level, silently discard...).
In your case displaying success message only mean that your mail is received on MTA side for further processing, not anything about it's future.

PHPMailer send the messages with my server email

as written on the subject, PHPMailer send the messages with my server email, not the email of the actual sender.
for example:
from: sendername myservername#server.com
the sender email doesn't show up on the email from section
here is my code
$result = mysql_query($insert_query, $connection) or die(mysql_error());
if($result)
{
require_once('../se482/class.phpmailer.php');
//$mail->SMTPDebug = 3; // Enable verbose debug output
//$mail->IsSMTP(true);
$mail = new PHPMailer; // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myemail#gmail.com'; // SMTP username
$mail->Password = 'mypassowrd'; // SMTP password
$mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->CharSet = 'UTF-8';
$mail->Encoding = '8bit';
$mail->addAddress('myemail#gmail.com'); // Add a recipient
$mail->setFrom($email, $name);
//$mail->addAddress('joe#example.net', 'Joe User'); // 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->isHTML(true); // Set email format to HTML
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');
$mail->From = $email;
$mail->Subject = 'Here is the subject';
$mail->Body = $comment;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->Send();
$mail->SmtpClose();
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
You have to include class.smtp.php as well to use smtp. Also uncomment //$mail->isSMTP(true);
As mentioned in documentation to avoid this you can include only autoloader class that is PHPMailerAutoload.php
updated code
$result = mysql_query($insert_query, $connection) or die(mysql_error());
if($result)
{
//require_once('../se482/class.phpmailer.php');
// require_once('../se482/class.smtp.php');
//or
require_once('../se482/PHPMailerAutoload.php');
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(true);
$mail = new PHPMailer; // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myemail#gmail.com'; // SMTP username
$mail->Password = 'mypassowrd'; // SMTP password
$mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->CharSet = 'UTF-8';
$mail->Encoding = '8bit';
$mail->addAddress('myemail#gmail.com'); // Add a recipient
$mail->setFrom($email, $name);
//$mail->addAddress('joe#example.net', 'Joe User'); // 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->isHTML(true); // Set email format to HTML
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');
$mail->From = $email;
$mail->Subject = 'Here is the subject';
$mail->Body = $comment;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->Send();
$mail->SmtpClose();
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
Update
You have also add your app credential in get_auth_token.php as mentioned in documentation for use gmail. Here is link https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2
$mail->from is set to $email which isn't defined anywhere.

How to send email with SMTP in php

I want to send email with SMTP in my project, previously i write php mail() in my project but now my client want that i should use SMTP. I search about this but i get nothing any proper solution for this.
In my php mail() i send name, subject and comment, so how can i do this in SMTP.
Here is my code:
$payer_email = "Your Email";
$subject = "Your Subject";
$message = 'Dear '.$name.',
Thank you for your purchase from '.$site_url.'. The details of your purchase are below.
Transaction ID: '.$txn_id.'
Item Name: '.$item_name.'
Payment Amount: '.$payment_amount.'
Payment Amount: '.$payment_status.'
Paid to: '.$receiver_email.'
Thanks and Enjoy!';
$headers .= 'From: ' .$from. "\r\n" .'Reply-To: ' .$from . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1 ";
//mail to buyer
mail( $payer_email , $subject, $message, $headers );
Please give me some suggestions or simple and nice tutorials.
Take a look at PHP Mailer:
https://github.com/PHPMailer/PHPMailer
Example from that page:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe#example.net', 'Joe User'); // 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->WordWrap = 50; // Set word wrap to 50 characters
$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';
}
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "mail.example.com"; // SMTP server example
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "username"; // SMTP account username example
$mail->Password = "password"; // SMTP account password example
Please try this
Create library file for the SMTP Settings 'library.php':
<?php
error_reporting(0);
define("SMTP_HOST", "SMTP_HOST_NAME"); //Hostname of the mail server
define("SMTP_PORT", "SMTP_PORT"); //Port of the SMTP like to be 25, 80, 465 or 587
define("SMTP_UNAME", "VALID_EMAIL_ACCOUNT"); //Username for SMTP authentication any valid email created in your domain
define("SMTP_PWORD", "VALID_EMAIL_ACCOUNTS_PASSWORD"); //Password for SMTP authentication
?>
Make the form post and do the below actions:
<?php
include 'library.php';
include "classes/class.phpmailer.php"; // include the class file name
if(isset($_POST["send"])){
$email = $_POST["email"];
$mail = new PHPMailer; // call the class
$mail->IsSMTP();
$mail->Host = SMTP_HOST; //Hostname of the mail server
$mail->Port = SMTP_PORT; //Port of the SMTP like to be 25, 80, 465 or 587
$mail->SMTPAuth = true; //Whether to use SMTP authentication
$mail->Username = SMTP_UNAME; //Username for SMTP authentication any valid email created in your domain
$mail->Password = SMTP_PWORD; //Password for SMTP authentication
$mail->AddReplyTo("reply#yourdomain.com", "Reply name"); //reply-to address
$mail->SetFrom("from#yourdomain.com", "Asif18 SMTP Mailer"); //From address of the mail
// put your while loop here like below,
$mail->Subject = "Your SMTP Mail"; //Subject od your mail
$mail->AddAddress($email, "Asif18"); //To address who will receive this email
$mail->MsgHTML("<b>Hi, your first SMTP mail has been received. Great Job!.. <br/><br/>by <a href='http://asif18.com'>Asif18</a></b>"); //Put your body of the message you can place html code here
$mail->AddAttachment("images/asif18-logo.png"); //Attach a file here if any or comment this line,
$send = $mail->Send(); //Send the mails
if($send){
echo '<center><h3 style="color:#009933;">Mail sent successfully</h3></center>';
}
else{
echo '<center><h3 style="color:#FF3300;">Mail error: </h3></center>'.$mail->ErrorInfo;
}
}
?>
Please edit your email and password correctly.
You may see the demo and source code on Click here
you can employ the use of the phpmailer library since the mail() function has limitations in its use. so I prefer using PHPmailer it is easier to work with than the mail() function.
I also used the mail() function it requires a lot of settings here and there and cannot be used to send remote mail messages.
so in short, the mail() function is only suitable when working with the local server but not suitable when you want to work with a remote server.

Categories