PHPMailer for Gmail action not being taken - php

Does anyone knows what is wrong with this code ? I am pretty new to PHP and I am having problems when trying to send contact mails from the web page.
The action seems to be executed but the mail in fact is not sent.
<?php
$Nombre = $_POST['Nombre'];
$Apellido = $_POST['Apellido'];
$Correo = $_POST['Correo'];
$Numero = $_POST['Numero'];
require_once('PHPMailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth= true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = '587';
$mail->isHTML(true);
$mail->setFrom($Correo, $Nombre);
$mail->AddAddress('');
$mail->AddReplyTo($Correo, $Nombre);
$mail->Username = '';
$mail->Password = '';
$mail->Subject = '';
$mail->Body = 'l';
$mail->Send();
?>

Related

PHPMailer is not working on godaddy server

I have developed a site in PHP and added a PHPMailer function for my contact us page but the mailer functionality is not working. Contacted with support team but they didn't help me tried a lot with that.
<?php
if(isset($_POST['submit_contact']))
{
require 'PHPMailer/PHPMailerAutoload.php';
$to = "gmail#gmail.com";
$name = $_POST['uname'];
$email = $_POST['email'];
$phone = $_POST['phonenumber'];
$companyname = $_POST['companyname'];
$country = $_POST['country2'];
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "relay-hosting.secureserver.net";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "xxxx#gmail.com";
$mail->Password = "PASSword1#3";
$message = array();
$message[] = 'Name : '.trim($name).' ';
$message[]='Phone : '.trim($phone).' ';
$message[]='Email : '.trim($email).' ';
$message[]='Company Name : '.trim($companyname).' ';
$message[]='Country : '.trim($country).' ';
$message = implode('<br/>', $message);
$mail->SetFrom($email);
$mail->Subject = 'Here is the subject';
$mail->Body = $message;
$mail->AddAddress($to);
if(!$mail->send()) {
$msg = "Error while sending email";
$msgclass = 'bg-danger';
header("Location: /");
die();
}
else {
$msg = 'A mail with recovery instruction has sent to your email.';
$msgclass = 'bg-success';
}
}
?>
u used gmail,u should set host to smtp.gmail.com, SMTPSecure to ssl,SMTPAuth to true,port to 465,u alse should use the app password,that u can see https://support.google.com/mail/answer/7126229
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = false;
$mail->SMTPSecure = 'none';
$mail->Host = "localhost";
$mail->Port = 25;
$mail->IsHTML(true);
Solved the issue with this update in SMTP

Could Not Access File using phpMailer

I'm Using phpmailer to send mail.
Recieving mail fine but the attachment not attached in it.
When I debug it. it gives me the error.
"Could not access file: upload_images/images.jpg"
i have a folder upload_images in which i have an image name images.
here is my code...
if (isset($_POST['btnsubmit'])) {
require "phpmailer/PHPMailerAutoload.php";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "mail.domain.com";
$mail->port = 465;
$mail->SMTPAuth=true;
$mail->SMTPDebug = 4;
$mail->SMTPSecure = 'tls';
$mail->Username = "info#example.com";
$mail->Password = '******';
$file_name = $_FILES["attc"]["name"];
$tmp_name = $_FILES["attc"]["tmp_name"];
$path = '/upload_images/';
//move_uploaded_file($tmp_name, $path.$file_name);
$mail->setFrom('abc#gmail.com','From_name');
$mail->addAddress('xyz#gmail.com','To_name');
$mail->addReplyTo('abc#gmail.com','From_name');
$mail->isHTML(true);
$mail->AddAttachment("upload_images/images.jpg","images.jpg");
$mail->Subject = "Testing well";
$mail->Body = 'This is Body Part';
if ($mail->send()) {
echo "<script>alert('Email Sent Success!')</script>";
}
else{
echo "<script>alert('".$mail->ErrorInfo."')</script>";
}
}
You should use an absolute path. For example, if upload_images is inside the document root, you may use:
$mail->AddAttachment($_SERVER['DOCUMENT_ROOT']."/upload_images/images.jpg","images.jpg");
Use the absolute path.
if (isset($_POST['btnsubmit'])) {
require "phpmailer/PHPMailerAutoload.php";
$my_path ="upload_images/images.jpg";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "mail.domain.com";
$mail->port = 465;
$mail->SMTPAuth=true;
$mail->SMTPDebug = 4;
$mail->SMTPSecure = 'tls';
$mail->Username = "info#example.com";
$mail->Password = '******';
$file_name = $_FILES["attc"]["name"];
$tmp_name = $_FILES["attc"]["tmp_name"];
$path = '/upload_images/';
//move_uploaded_file($tmp_name, $path.$file_name);
$mail->setFrom('abc#gmail.com','From_name');
$mail->addAddress('xyz#gmail.com','To_name');
$mail->addReplyTo('abc#gmail.com','From_name');
$mail->isHTML(true);
$mail->AddAttachment($my_path);
$mail->Subject = "Testing well";
$mail->Body = 'This is Body Part';
if ($mail->send()) {
echo "<script>alert('Email Sent Success!')</script>";
}
else{
echo "<script>alert('".$mail->ErrorInfo."')</script>";
}
}
Use the following and path for file in the variable $my_path
if (isset($_POST['btnsubmit'])) {
//use the path
$my_path = "/upload_images/images.jpg";
require "phpmailer/PHPMailerAutoload.php";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "mail.domain.com";
$mail->port = 465;
$mail->SMTPAuth=true;
$mail->SMTPDebug = 4;
$mail->SMTPSecure = 'tls';
$mail->Username = "info#example.com";
$mail->Password = '******';
$file_name = $_FILES["attc"]["name"];
$tmp_name = $_FILES["attc"]["tmp_name"];
$path = '/upload_images/';
//move_uploaded_file($tmp_name, $path.$file_name);
$mail->setFrom('abc#gmail.com','From_name');
$mail->addAddress('xyz#gmail.com','To_name');
$mail->addReplyTo('abc#gmail.com','From_name');
$mail->isHTML(true);
$mail->AddAttachment($my_path,'images.jpg');
$mail->Subject = "Testing well";
$mail->Body = 'This is Body Part';
if ($mail->send()) {
echo "<script>alert('Email Sent Success!')</script>";
}
else{
echo "<script>alert('".$mail->ErrorInfo."')</script>";
}
}

I used phpmailer() concept to receive mail from users from my bigrock server using php script, but It's not working

This is my code:
<?php
require_once "PHPMailer/PHPMailerAutoload.php";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com"; //gethostbyname('smtp.gmail.com');
$mail->SMTPAuth = true;
$mail->SMTPDebug = 3;
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->Username = "ku***wa***ar***#gmail.com";
$mail->Password = "t***r9***5******7";
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress($mail->Username);
$mail->Subject = 'Contact form query / feedback';
$mail->Body = "
<div><span>Name : </span><span>{$name}</span></div>
<div><span>Email : </span><span>{$email}</span></div>
<div><p>{$message}</p></div>
";
$mail->isHTML(true);
if ($mail->send()){
echo "Your feedback/query is sent!";
}else{
echo "Error! Unable to forward your request.<br> Pleas try again later!";
}
Note: I have used " GMail " as my SMTP server and SMTPSecure is " ssl " and port is "465" and username & passwords are my GMail username & password
The Error Message is:
Screen Shot : https://s16.postimg.org/4hgsh9d51/smtp.png
Here is your code modified
<?php
include "PHPMailer_5.2.4/class.phpmailer.php";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com"; //gethostbyname('smtp.gmail.com');
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->isHTML(true);
$mail->Username = "hari.andoidsaiss#gmail.com";
$mail->Password = "supreme#12#";
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress($mail->Username);
$mail->Subject = 'Contact form query / feedback';
$mail->Body = "
<div><span>Name : </span><span>{$name}</span></div>
<div><span>Email : </span><span>{$email}</span></div>
<div><span>Message : </span><p>{$message}</p></div>
";
if ($mail->send()){
echo "Your feedback/query is sent!";
}else{
echo "Error! Unable to forward your request.<br> Pleas try again later!";
}

Username and Password not accepted

I'm trying to send a email using gmail's smtp (see below code) but I'm getting "Username and Password not accepted" error.
I've tried:
this link
allow less secure apps
ssl is enabled
enable imap/pop on gmail's settings
a different login
none of them works.
Here's the PHP code:
function sendEmail($from, $fromName, $msg)
{
$mail = new PHPMailer();
$mail->SMTPDebug = 4; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line
$mail->IsSMTP();
//$mail->Host = 'tls://smtp.gmail.com:587';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; //465
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = 'xxxx#gmail.com';
$mail->Password = 'yyyyyyy';
$mail->From = $from;
$mail->FromName = $fromName;
$mail->AddAddress('foo#gmail.com', ' ');
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Priority = 1;
$mail->Timeout = 60;
$mail->SMTPKeepAlive = true;
$mail->Subject = "subject here";
$mail->Body = $msg;
$mail->AltBody = 'testing..';
$ok = $mail->Send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
return $ok;
}
Update: Here's the full error message (with DebugMode = 4)
After testing I got this to work by changing the host to include the tls prefix.
function sendEmail($from, $fromName, $msg)
{
$mail = new PHPMailer();
$mail->SMTPDebug = 4;
$mail->WordWrap = 900;
$mail->IsSMTP();
//$mail->Host = 'tls://smtp.gmail.com:587';
$mail->Host = "tls://smtp.gmail.com";
$mail->Port = 587; //465
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = 'xxxx#gmail.com';
$mail->Password = 'yyyyyyy';
// Define o remetente
$mail->From = $from;
$mail->FromName = $fromName;
$mail->AddAddress('foo#gmail.com', ' ');
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Priority = 1;
$mail->Timeout = 60;
$mail->SMTPKeepAlive = true;
$mail->Subject = "subject here";
$mail->Body = $msg;
$mail->AltBody = 'testing..';
$ok = $mail->Send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
return $ok;
}

PhpMailer Authentication Error

I'm trying to configure email by using phpmailer and smtp but Authentication error appears.
Code below
include_once("phpmailer.php");
$mail = new PHPMailer();
$tarih = date("d.m.Y - H:i:s");
$mail->IsSMTP();
$mail->Host = "mail.golcukdh.gov.tr";
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = "username";
$mail->Password = "password";
$mail->IsHTML(true);
$mail->From = "email";
$mail->AddAddress("email");
$mail->Subject = "subject";
$mail->Body = "Body Part";
$mail->Send()
?>
I tried to configure this by using asp.net with these authentication information and it was successfull. But I want to use php, what is wrong ?

Categories