phpmailer - email not send in gmail - php

I am having problem with sending mail using phpmailer. I am a beginner programmer. I am trying to make contact form. My code is as follow(submit.php). Please suggest me.. Thanks in advance .
session_start();
require_once 'libs/phpmail/PHPMailerAutoload.php';
$errors = array();
if(isset($_POST['name'], $_POST['phone'],$_POST['mail'],$_POST['message'])){
$fields = array(
'name' => $_POST['name'],
'phone' => $_POST['phone'],
'email' => $_POST['mail'],
'message' => $_POST['message']
);
foreach ($fields as $field => $data) {
if(empty($data)){
$errors[] = 'The '. $field . ' field is required';
}
}
if(empty($errors)){
$m = new PHPMailer;
$m -> isSMTP();
$m -> SMTPAuth = true;
//$m -> SMTPDebug = 2;
$m -> Host = 'smtp.gmail.com';
$m -> Username = 'xxxx#gmail.com';
$m -> Password = 'xxxx';
$m -> SMTPSecure = 'ssl';
$m -> Port = 465;
$m -> isHTML();
$m -> Subject = 'Contact form submitted';
$m -> Body = 'From: ' . $fields['name']. '('. $fields['phone'] . $fields['email']. ')'.'<p>' .$fields['message'] .'</p> ';
$m -> FromName = 'Contact';
// $m ->addReplyTo($fields['email'], $fields['name']);
$m -> addAddress('ssss#gmail.com', 'xxxxxxxx');
if($m->send()){
header('Location: thanks.php');
die();
}else{
$errors[] = 'Sorry could not send email. Please try again';
}
}
}else{
$errors[] = 'some thing went wrong';
}
$_SESSION['error'] = $errors;
$_SESSION['field'] = $fields;
header('Location: form.php');

My setting phpmailer, everything works
function __construct ($to, $subject, $body) {
date_default_timezone_set('Etc/UTC');
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
$mail->CharSet = 'UTF-8';
//Set the hostname of the mail server
$mail->Host = "mail.xxxxxx.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
$mail->AuthType = 'PLAIN';
//Username to use for SMTP authentication
$mail->Username = "xxxx";
//Password to use for SMTP authentication
$mail->Password = "xxxx";
//Set who the message is to be sent from
$mail->setFrom('erp#xxxxxx.com');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress($to);
//Set the subject line
$mail->Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML($body);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
$this->mail = $mail;
}
function SendMail () {
//send the message, check for errors
if (!$this->mail->send()) {
return "Mailer Error: " . $this->mail->ErrorInfo;
} else {
return true;
}
}
// using
$email = $this->request->getPost('email');
$smtp = new \SmtpClient($email, 'Test', $template);
$result = $smtp->SendMail();

Remove
$m -> Subject = 'Contact form submitted';
And try again.
When I remove the subject it worked.

Related

PHPMailer Stopping After So Many Sends?

I am attempting to send over 1,000 emails through phpmailer, however after roughly the 100 email sent, it stops sending them, do you know why this could be? Here is my code.
I am using PHPmailer and Google SMTP, with a g suite email as the sender.
It does execute every function perfectly, but for some reason it just randomly stops in the loop. Very lost.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
header('Content-Type: application/json');
require '/var/www/fakewebsite.com/vendor/autoload.php';
$newsletterid = $_POST['newsletterid'];
$allcontacts = Contacts::ReadAllNewsletter();
$allnewseletters = Newsletter::ReadAllNewsletter();
$mail = new PHPMailer(true);
//Server settings
//Enable verbose debug output
$mail->isSMTP();
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->SMTPKeepAlive = true; //Keep SMTP Connection Alive During Loop
$mail->Host = 'smtp.gmail.com'; //Set the SMTP server to send through
$mail->Username = 'test#example.com'; //SMTP usernames
$mail->Password = 'fakepassword'; //SMTP password
$mail->SMTPSecure = 'tls'; //Enable implicit TLS encryption
$mail->Port = 587;
$mail->SMTPDebug = 1;
$mail->addReplyTo('test#example.com', 'Fake Name');
foreach ($allcontacts as $eachcontact) {
foreach ($allnewseletters as $eachnewsletter) {
if ($eachnewsletter["id"] == $newsletterid) {
if ($eachnewsletter["contactlist"] == 1) {
if ($eachcontact["contacttype"] == 'Client') {
if ($eachcontact["subscribed"] == 1) {
$sendnewsletter = new Newsletter();
$sendnewsletter->contactid = $eachcontact["id"];
$sendnewsletter->newsletterid = $eachnewsletter["id"];
$newslettercounter = new Newsletter();
$newslettercounter->id = $eachnewsletter["id"];
//Recipients
$mail->setFrom('fakewebsite#example.com', $eachnewsletter["fromname"]);
$mail->addAddress($eachcontact["email"]);
$message1 = file_get_contents('/var/www/fakewebsite.com/public_html/admin/emailtemplates/newsletterunfilled.php');
$message1 = str_replace('{NEWSLETTERCONTENT}', $eachnewsletter["content"], $message1);
$message1 = str_replace('{First Name}', $eachcontact["name"], $message1);
$message1 = str_replace('..', "https://fakewebsite.com/admin", $message1);
$message1 = str_replace("api/unsubscribe.php?contactid={contactid}&newsletterid={newsletterid}", "https://fakewebsite.com/admin/newsletter/api/unsubscribe.php?contactid={contactid}&newsletterid={newsletterid}", $message1);
$message1 = str_replace('<span style="color: #ffffff;"><a style="color: #ffffff;" href="https://www.divinestatus.com" target="_blank" rel="noopener"><strong>SCHEDULE TRANPORT</strong></a></span>', '<span style="color: #ffffff;"><a style="color: #ffffff;" href="https://fakewebsite.com/admin/newsletter/api/linkclicked.php?url=https://www.fakewebsite.com&contactid={contactid}&newsletterid={newsletterid}" target="_blank" rel="noopener"><strong>SCHEDULE TRANPORT</strong></a></span>', $message1);
$message1 = str_replace('{First Name}', $eachcontact["name"], $message1);
$message1 = str_replace('{contactid}', $eachcontact["id"], $message1);
$message1 = str_replace('{newsletterid}', $eachnewsletter["id"], $message1);
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = $eachnewsletter["subject"];
$mail->Body = $message1;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$sendnewsletter->delivered = 1;
$sendnewsletter->SendNewsletter();
$newslettercounter->NewsletterDelivered();
$result = array('status' => "success", 'message' => "Message sent.");
echo json_encode($result);
if (!$mail->send()) {
$result = array('status' => "error", 'message' => "Message not sent!.");
echo json_encode($result);
echo "Mail Not Sent";
$sendnewsletter->delivered = 0;
$sendnewsletter->SendNewsletter();
}
$mail->ClearAllRecipients();
sleep(1);
}
}
}
}
}
}

Can solve my "SMTP Error: Could not authenticate" error

i have a problem with my PHPmailer function.
I want my registration form to send a mail to every new registered member.
I get this error code. SMTP Error: Could not authenticate.
I have contacted my webhost, and they say that i have the right username,password, smtp server etc.
include "class.smtp.php";
include "class.phpmailer.php";
$Host = "mail.smtp.com"; // SMTP servers
$Username = "support#mymail.com"; // SMTP password
$Password = "mymailpassword";
$From = "support#mymail.com";
$FromName = "My name";
$ToEmail = $mail;
$ToName = $username;
$header = str_replace('%homepage_url%', $hp_url, $_language->module['mail_subject']);
$Message = str_replace(Array('%nickname%', '%username%', '%password%', '%activationlink%', '%pagetitle%', '%homepage_url%'), Array(stripslashes($nickname), stripslashes($username), stripslashes($pwd1), stripslashes($validatelink), $hp_title, $hp_url), $_language->module['mail_text']);
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = $Host;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $Username;
$mail->Password = $Password;
$mail->From = $From;
$mail->FromName = $FromName;
$mail->AddAddress($ToEmail , $ToName);
$mail->WordWrap = 50; // set word wrap
$mail->Priority = 1;
$mail->IsHTML(true);
$mail->Subject = $header;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($Message);
$mail->CharSet="UTF-8";
if(!$mail->Send()){
redirect("index.php",$_language->module['register_successful'],10);
$show = false;
} else {
redirect("index.php",$_language->module['register_successful'],10);
$show = false;
}

how to variable pass mail function in php

I am new to php and i want solution for variable pass in mail function
I am using this way to variable pass mail function but getting issue Code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
function Send_Mail($to,$subject,$body,$cc = "",$bcc = "")
{
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPDebug = false;
$mail->Username = 'xyz#gmail.com'; // Your Gmail Address
$mail->Password = '123456'; // Your Gmail Password
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->From = 'xyz#gmail.com'; // From Email Address
$mail->FromName = 'gfgg'; // From Name
$mail->addAddress($to);
$mail->addReplyTo('xyz#gmail.com', 'dfg'); // Same as From Email and From Name.
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->send()) {
return array('status' => false, 'message' => $mail->ErrorInfo);
}
return array('status' => true, 'message' => 'Email Sent Successfully');
}
if (isset($_REQUEST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$contact=$_POST['contact'];
echo $name, $email, $contact;
$result = Send_Mail('$name','$email','$contact');
}
pls give me soltution
?>
The Send_Mail function requires minimum 3 parameters, which are $to, $subject and $bodyin this order.
So first of all you are calling your parameters within strings
$result = Send_Mail('$name','$email','$contact');
and in the wrong order.
You should probably try this
$result = Send_Mail($email, "Subject", "Message");
You will have to define $subject and $message variables and replace them with "Subject" and "Message"
Try with -
echo "$name, $email, $contact";
$result = Send_Mail($name,$email,$contact);

PHP not receiving email

This is my php code to send a confirmation email to a new user and the problem is, i am not receiving it(i have tried different ones, no luck)
function send_email($info)
{
//format each email
$body = format_email($info,'html');
$body_plain_txt = format_email($info,'txt');
//setup the mailer
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
$message ->setSubject('Welcome to Site Name');
$message ->setFrom(array('noreply#localhost' => 'localhost'));
$message ->setTo(array($info['email'] => $info['username']));
$message ->setBody($body_plain_txt);
$message ->addPart($body, 'text/html');
$result = $mailer->send($message);
return $result;
}
I get no errors
Install phpmailer.
Here is a sample on how to send mail with PHP mailer
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // 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;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}
If your destination address is #hotmail.com it may never income because they have a strong anti-spam management.

Problem with using PHPMailer for SMTP

I have used PHPMailer for SMTP and there is problem in sending mail with error "Mailer Error: The following From address failed: no-reply#mydomain.org.uk"
My code is as follows:
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "localhost;"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = ""; // SMTP username
$mail->Password = ""; // SMTP password
$mail->From = $email_address;
$mail->FromName = $email_address;
$mail->AddAddress($arrStudent[0]["email"]);
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Subject";
$theData = str_replace("\n", "<BR>", $stuff);
$mail->Body = $theData; // "This is the <b>HTML body</b>";
$mail->AltBody = $stuff;
if (!$mail->Send()) {
$sent = 0;
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
i researched everything and when i debug inside class.smtp.php i found error the function "get_lines()" is returning error value "550 Authentication failed"
The code was working fine previously, i am wondering how this problem came suddenly.
Desperate for some help.
Thanks,
Biplab
public function sendEmail ( $subject, $to, $body, $from = FALSE ) {
require_once('mailer.class.php');
$mailer = new PHPMailer();
//do we use SMTP?
if ( USE_SMTP ) {
$mailer->IsSMTP();
$mailer->SMTPAuth = true;
$mailer->Host = SMTP_HOST;
$mailer->Port = SMTP_PORT;
$mailer->Password = '';
$mailer->Username = '';
if(USE_SSL)
$mailer->SMTPSecure = "ssl";
}
$mailer->SetFrom($from?$from:ADMIN_EMAIL, ADMIN_NAME);
$mailer->AddReplyTo ( ADMIN_EMAIL, ADMIN_NAME );
$mailer->AddAddress($to);
$mailer->Subject = $subject;
//$mailer->WordWrap = 100;
$mailer->IsHTML ( TRUE );
$mailer->MsgHTML($body);
require_once('util.class.php');
$mailer->AltBody = Util::html2text ( $body );
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if ( ! $mailer->Send() ) {
return FALSE;
}
else {
$mailer->ClearAllRecipients ();
$mailer->ClearReplyTos ();
return TRUE;
}
}
I've used like that... SetFrom should be used in place of From... that's your error buddy... :))
try adding belowe line to php.ini
extension=php_openssl.dll
restart and try again
I am using YII's Mailer with PHPMailer, and this works for me:
$mail = Yii::createComponent('application.extensions.mailer.EMailer');
$mail->Username = $this->SMTP_USERNAME; // SMTP username
$mail->Password = $this->SMTP_PASSWORD; // SMTP password
$mail->SMTPAuth = true;
$mail->From = $this->fromAddress;
$mail->Host = $this->SMTP_SERVER_ADDRESS;
$mail->FromName = $this->fromName;
$mail->CharSet = 'UTF-8';
$mail->Subject = Yii::t('mailer', $this->subject);
$mail->Body = $this->message;
$mail->AddReplyTo($this->toAddress);
$mail->AddAddress($this->toAddress);
$mail->IsSMTP(true);
$mail->IsHTML(true);
$mail->Send();
Hope that helps?

Categories