I'm using php mailer for mail triggering.
Its working fine. But I gave 2 to 5 recipients, it sends the mail to only one recipient. In future, I have to trigger a mail to nearly 100 recipients..
I've shared my code below.. Please check it..
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // 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 = 'karthick****#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
$mail->setFrom('karth*******#gmail.com', 'A**n');
$addresses = explode(',',$emailM);
foreach ($addresses as $address) {
$mail->AddAddress($address);
}
$mail->isHTML(true);
$mail->Subject = 'Need for '.$keyword.'';
$mail->Body = 'Hi,The Message';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Qoute has been sent to all the Manufacturers';
echo "$address";
}
You're code looks like it should be doing the trick. Make sure that $address doesn't contain any whitespace for the entries. For safe measure, add the trim()
function.
$mail->AddAddress(trim($address));
If that does not work, make sure that you're recipient addresses are real.
Additionally, in case the privacy of the recipients is of concern, I would recommend you use AddBCC() instead of AddAddress() so that their addresses are not revealed.
A basic idea is that make different connection (object) for each your mailing address like below if You don't have so much addresses in your array.
require 'phpmailer/PHPMailerAutoload.php';
$addresses = explode(',',$emailM);
foreach ($addresses as $address) {
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // 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 = 'karthick****#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
$mail->setFrom('karth*******#gmail.com', 'A**n');
$mail->AddAddress($address);
$mail->isHTML(true);
$mail->Subject = 'Need for '.$keyword.'';
$mail->Body = 'Hi,The Message';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Qoute has been sent to all the Manufacturers';
echo "$address";
}
UPDATE :
The second idea is that you can remove recepients each time and add new one and then send like below
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // 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 = 'karthick****#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
$mail->setFrom('karth*******#gmail.com', 'A**n');
$addresses = explode(',',$emailM);
foreach ($addresses as $address) {
// for clear last recipients
$mail->ClearAllRecipients( )
$mail->AddAddress($address);
$mail->isHTML(true);
$mail->Subject = 'Need for '.$keyword.'';
$mail->Body = 'Hi,The Message';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Qoute has been sent to all the Manufacturers';
echo "$address";
}
}
Related
I tried to create a mail-confirm signup system but when I signup, database save my data but there is a error that says:
SMTP Error: Could not authenticate.
note: I am using class.phpmailer.php
I could not understand the problem. Here is my code:
sendmail.php
<?php
require 'PHPMailerAutoload.php';
function send_mail($sendto, $subject, $body)
{
$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 = '**.****.****#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
$mail->setFrom('e.d***.c**#gmail.com', 'Restro | Restaurants in Mumbai');
$mail->addAddress($sendto); // Name is optional
$mail->addReplyTo('e.d****.c***#gmail.com', 'Restro | Restaurants in Mumbai');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = 'Please open this on Chrome';
if (!$mail->send()) {
//echo 'Message could not be sent.';
return 'Mailer Error: ' . $mail->ErrorInfo;
} else {
return 'Message has been sent';
}
}
Use port 465
port 587 if your client begins with plain text before issuing the STARTTLS command.
I am new to PHP. I've been trying to get this mailer to work and I have gotten most of it working.
Here is my PHP:
<?php
require __DIR__ . '/vendor/autoload.php';
$included_files = get_included_files();
foreach ($included_files as $filename) {
echo "$filename\n";
}
$mail = new PHPMailer;
$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 = 'rottenrobbie911#gmail.com'; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('yalnifcam2#gmail.com', 'Joe User');
$mail->Subject = 'GenericSubject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
When I run my code here without a password this comes up:
/var/www/html/mail.php /var/www/html/vendor/autoload.php /var/www/html/vendor/composer/autoload_real.php /var/www/html/vendor/composer/ClassLoader.php /var/www/html/vendor/composer/autoload_static.php /var/www/html/vendor/guzzlehttp/promises/src/functions_include.php /var/www/html/vendor/guzzlehttp/promises/src/functions.php /var/www/html/vendor/guzzlehttp/psr7/src/functions_include.php /var/www/html/vendor/guzzlehttp/psr7/src/functions.php /var/www/html/vendor/paragonie/random_compat/lib/random.php /var/www/html/vendor/guzzlehttp/guzzle/src/functions_include.php /var/www/html/vendor/guzzlehttp/guzzle/src/functions.php /var/www/html/vendor/sendgrid/sendgrid/lib/SendGrid.php /var/www/html/vendor/sendgrid/sendgrid/lib/helpers/mail/Mail.php /var/www/html/vendor/swiftmailer/swiftmailer/lib/swift_required.php /var/www/html/vendor/swiftmailer/swiftmailer/lib/classes/Swift.php
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
But put a password in and it doesn't like it. Instead prints the code to the webpage like so.
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 = 'rottenrobbie911#gmail.com'; // SMTP username $mail->Password = 'password';
// SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted $mail->Port = 465; $mail->setFrom('from#example.com', 'Mailer'); $mail->addAddress('yalnifcam2#gmail.com', 'Joe User'); $mail->Subject
= 'GenericSubject'; $mail->Body = 'This is the HTML message body in bold!'; if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; } ?>
It's just the password. When I enter a password it doesn't work but leave it bank and it just fails to authenticate.
Reference gmail smtp settings
Use port no 587 for TLS.
change this $mail->Port = 465; to $mail->Port = 587;
I am trying to configure SMTP server using 'phpmailer' class, i am using my email account email and password from my '1and1' email server, this is the code i have tried but i am getting error of 'SMTP' connect() failed, i don't know what i am doing wrong.
code source: 'github'
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.1and1.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myemail#server.com'; // SMTP username
$mail->Password = '*********'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 993; // TCP port to connect to
$mail->setFrom('myemail#server.com', 'Mailer');
$mail->addAddress('receiver#gmail.com', 'Shaur'); // Add a recipient
//$mail->addAddress('shaurshori#gmail.com'); // Name is optional
//$mail->addReplyTo('shaurshori#gmail.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';
}
?>
date_default_timezone_set('Etc/UTC');
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.mailgun.org';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "#email.com";
$mail->Password = "**************";
$mail->setFrom('emailTo','name' );
$mail->addAddress('emailFrom', 'name');
$mail->addAddress('');
$mail->Subject = 'Test';
$mail->msgHTML("<h1>Succes!</h1>");
$mail->AltBody = 'Succes';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
I have soomething that works
Change
$mail->SMTPSecure = 'ssl';
to
$mail->SMTPSecure = 'tls';
As it states on the 1and1 email outgoing server info "Outgoing port (TLS must be activated)"
I am trying to send different messages to different users. I made an array of email addresses and while iterating through it, I want to send message2 to user2.
While reusing the same mail instance, at the beginning of each iteration I declare $mail -> ClearAddresses(), but now user2 gets the message of user1, and user2... and so one.
What am I missing that the Address won't get cleared at the beginning of the iteration?
Thanks!
// settings
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'xxx'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxx'; // SMTP username
$mail->Password = 'xxx'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;
$mail->CharSet = "UTF-8"; // TCP port to connect to
function sendInvoice($mail, $addresses) {
foreach($addresses as $recipient) {
$mail->ClearAddresses();
$mail->setFrom('mail#domain.eu', 'My Server');
$mail->addAddress($recipient['email'], $recipient['name']); // Add a recipient
$mail->addReplyTo('mail#domain.eu', 'My Server');
$mail->isHTML(true);
$mail->Subject = $recipient[subject];
//$mail->Body = $message;
$mail->MsgHTML($recipient[message]);
if (! $mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
//echo 'Message has been sent';
}
}
}
In your code, change:
$mail->ClearAddresses();
to:
$mail->ClearAllRecipients();
This should fix the problem.
I'm trying to configure PHPMailer I've uploaded 1 file which is class.phpmailer.php and created another php file with this content:
<?php
require('class.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 = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "myemail#gmail.com";
$mail->Password = "mypassword";
$mail->SetFrom("the same email address");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("the same email address");
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
and i get nothing at all not a success message nor a failure message. http://www.mawk3y.net/mailtest/test.php
all answers are outdated now. Most current version (as of Feb 2018) does not have autoload anymore, and PHPMailer should be initialized as follows:
<?php
include_once(FCPATH.'PHPMailer/src/PHPMailer.php');
include_once(FCPATH.'PHPMailer/src/SMTP.php');
include_once(FCPATH.'PHPMailer/src/Exception.php');
$msj="My complete message";
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
//authentication SMTP enabled
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
//indico el puerto que usa Gmail 465 or 587
$mail->Port = 465;
$mail->Username = "xxxxxx";
$mail->Password = "xxxx";
$mail->SetFrom("xxxxxx#xxxxx.com","Name");
$mail->AddReplyTo("xxx#xxx.com","Name Replay");
$mail->Subject = "Test";
$mail->MsgHTML($msj);
$mail->AddAddress("xxxxxx#xxxxx.com");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
?>
The problem is with the require method.
You first have to extract all file of phpMailer repository.
instead of writing
require('class.phpmailer.php');
you need to include path where your PHPMailerAutoload.php file extracted.so you can replace it with.
require('path-of-extracted-folder/PHPMailerAutoload.php');
for more reference you can visit it's GitHub link
https://github.com/PHPMailer/PHPMailer
I had same problem but I solved.This is the way I code
<?php
require 'PHPMailer-master/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
// Set PHPMailer to use the sendmail transport
$mail->isSMTP();
$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 = 'abc'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Set who the message is to be sent from
$mail->setFrom('abc1#gmail.com', 'First Last');
//Set an alternative reply-to address
//Set who the message is to be sent to
$mail->addAddress('abc#gmail.com', 'Shehan');
//Set the subject line
$mail->Subject = 'Test Mail';
$mail->Body = 'This is Test Mail';
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>