I have just uploaded a page and find out the phpmailer isn't sending emails though it was sending in localhost. Getting 405 Not Allowed nginx. Any help? following is the code...
if (isset($_POST['msg_submit'])) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = $smtp_server_w;
$mail->Username = $username_w;
$mail->Password = $password_w;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->setFrom("test#gmail.com", "test");
$sql_mail = "SELECT * FROM order_details ORDER BY id DESC LIMIT 1";
$run_mail = mysqli_query($conn, $sql_mail);
while ($row_mail = mysqli_fetch_assoc($run_mail)) {
$mail->addAddress($row_mail['user_email'], $row_mail['user_name']);
$mail->isHTML(true);
$bodyContent = message;
$mail->Subject = test message ;
$mail->Body = $bodyContent;
$mail->AltBody = $bodyContent;
if(!$mail->send()) {
echo "Message could not be sent.";
$error = '<div class="alert alert-danger"><strong>Mailer Error: '. $mail->ErrorInfo.'</strong></div>';
} else {
$error = '<div class="alert alert-default"><strong>Message has been sent</strong></div>';
}
}
}
Related
I am getting the following error while sending the email using PHPMailer.
Error:
SMTP Error: Could not authenticate.
I am explaining my code below.
<?php
require_once('/var/www/oditek.in/subhra/phpmailer/class.phpmailer.php');
function SentMail($to,$from,$subject,$msg_body,$reply_to='',$cc='',$files=''){
$mail = new PHPMailer();
$body = $msg_body;
$mail->IsSMTP();
$mail->Host = "smtp.sendgrid.net";
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = "tuurbus#gmail.com";
$mail->Password = "abcd_bs#123";
$mail->SetFrom($from,'tuurbus');
if($reply_to!=''){
$mail->AddReplyTo($reply_to,'tuurbus');
}
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address);
if(count($files) > 0 && $files!=''){
for($i=0;$i<=count($files);$i++){
if(is_file($files[$i])){
$mail->AddAttachment($files[$i]);
}
}
}
if($cc!=''){
$addrcc = explode(',',$cc);
foreach ($addrcc as $addresscc) {
$mail->AddCC(trim($addresscc));
}
}
if($mail->Send()){
return 1;
}else{
return 0;
}
}
$to="tuurbus#gmail.com";
$from="subhrajyotipradhan#gmail.com";
$subject="Test email";
$msg_body="Hi, This is customize request";
$ret = SentMail($to,$from,$subject,$msg_body);
echo $ret;exit;
?>
I have also turned on the the less secured app option in gmail but still same error is coming. This is the implementation like contact us form in website. User will send the email request to admin(here tuurbus). Please help me to resolve this issue.
Download Phpmailer package from https://github.com/PHPMailer/PHPMailer/ link & copy into your project and extract zip folder into your project and change the your code as following also check your password ,email id. Its works by my side.
<?php
include_once('PHPMailerAutoload.php');
function SentMail($to,$from,$subject,$msg_body,$reply_to='',$cc='',$files=''){
$mail = new PHPMailer();
$body = $msg_body;
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = "tuurbus#gmail.com";
$mail->Password = "abcd_bs#123";
$mail->SetFrom($from,'tuurbus');
if($reply_to!=''){
$mail->AddReplyTo($reply_to,'tuurbus');
}
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address);
if(count($files) > 0 && $files!=''){
for($i=0;$i<=count($files);$i++){
if(is_file($files[$i])){
$mail->AddAttachment($files[$i]);
}
}
}
if($cc!=''){
$addrcc = explode(',',$cc);
foreach ($addrcc as $addresscc) {
$mail->AddCC(trim($addresscc));
}
}
if($mail->Send()){
return 1;
}else{
return 0;
}
}
$to="tuurbus#gmail.com";
$from="subhrajyotipradhan#gmail.com";
$subject="Test email";
$msg_body="Hi, This is customize request";
$ret = SentMail($to,$from,$subject,$msg_body);
echo $ret;exit;
?>
Try:
$mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
I'm frustrated to fix this.
One time I made it work. The next day, an error annoys me:
Warning: Attempt to assign property of non-object in C:\XAMPP\htdocs\HAF\includes\sendmail.php on line 356
Help me fix this :(
Here's my code:
sendmail.php
class SendMail {
function notification($recipient, $name, $subject, $message) {
global $email;
$email->Host = "smtp.gmail.com";
$email->SMTPAuth = true;
$email->Username = "******#gmail.com";
$email->Password = "**********";
$email->SMTPSecure = "tls";
$email->Port = 465;
$email->setFrom('admin#gmail.com', 'My WebApp');
$email->addAddress($recipient);
$email->isHTML(true);
$email->Subject = $subject;
$email->Body = $message;
if(!$email->send()) {
return false;
} else {
return true;
}
}
}
index.php
$email = "jaydenjames#gmail.com";
$name = "Jayden James";
$message = "Welcome {$name}!";
$SendMail->notification($email, $name, 'Welcome guest!', $message);
update answer with gmail configuration....
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = 'MyUsername#gmail.com';
$mail->Password = 'valid password';
$mail->SMTPAuth = true;
$mail->From = 'MyUsername#gmail.com';
$mail->FromName = 'Mohammad Masoudian';
$mail->AddAddress('anotherValidGmail#gmail.com');
$mail->AddReplyTo('phoenixd110#gmail.com', 'Information');
$mail->IsHTML(true);
$mail->Subject = "PHPMailer Test Subject via Sendmail, basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->Body = "Hello";
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message sent!";
}
I am using this code to send email to everyone in the database in a loop. Problem occurred when I find out that user 1 in database also getting all emails sent to other users in the database. User 2 gets all email sent to other users after him and so on. I want to send a single email to everyone customized with their name and emails. Did check and applied couple of solutions from this forum but they are not working.
#SEND EMAILS
require '../includes/PHPMailer/PHPMailerAutoload.php';
require '../includes/PHPMailer/config.php';
if(isset($_POST['submit'])){
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = $smtp_server_w;
$mail->Username = $username_w;
$mail->Password = $password_w;
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->setFrom("test#test.com", "Test Name");
$sql_mail = "SELECT * FROM users";
$run_mail = mysqli_query($conn, $sql_mail);
while ($row_mail = mysqli_fetch_assoc($run_mail)) {
$name = $row_mail["user_name"];
$email = $row_mail["user_email"];
$mail->ClearAddresses();
$mail->addAddress('test#test.com', 'Test Name');
$mail->addBCC($email, $name);
$mail->isHTML(true); // Set email format to HTML
$bodyContent = "<p>Multiple Messages</p>";
$mail->Subject = $row_mail['user_name'].", New job is available " ;
$mail->Body = $bodyContent;
$mail->AltBody = $bodyContent;
if(!$mail->send()) {
echo "Message could not be sent.";
$error = '<div class="alert alert-danger"><strong>Mailer Error: '. $mail->ErrorInfo.'</strong></div>';
} else {
$error = '<div class="alert alert-success"><strong>Message has been sent</strong></div>';
}
}
}
From $list i am getting all the emails that are posted by form after using explode
$list = explode(',', $guest_email);
foreach ($list as $k => $v) {
$mail = new PHPmailer();
$mail->From = $smtp_user;
$mail->FromName = $fromname;
$mail->Mailer = "mail";
$mail->Subject = "";
$mail->IsHTML(true);
//$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = ;
$mail->SMTPSecure = '';
$mail->Host = $smtp;
$mail->Port = $port;
$mail->Password = $sm;
$mail->Username = $sm;
$mail->Body = $bodyContent;
in below line the mail are going to all posted email address, in next line i want to add all the emails in cc, but it is not working
$mail->AddAddress($v);
$mail->Addcc($v)
$mail->AddReplyTo($smtp_user);
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "SUCCESS";
}
}
how to send all email in $mail->Addcc($v)
Iam using PHP mailer to send an Email the message sent but email body is Empty
here is my code
foreach($results as $result)
{
$email = $result['email'];
$body = 'Hello'.$result['username'].' we remind you to return the book to library after' .$result['days'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.myhost.com;';
$mail->SMTPAuth = true;
$mail->Username = 'send#myhost.com';
$mail->Password = 'mypassword';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->From = 'send#myhost.com';
$mail->FromName ='Bookstore';
$mail->addAddress($email, 'username');
$mail->Subject = 'Book returned Time';
$mail->Body = $body;
$mail->IsHTML(true);
if(!$mail->send()) {
echo 'Message could not be sent.';
//echo 'Mailer Error: ' . $mail->ErrorInfo; return $user ;
} else {
echo 'done';
}
}