Using SMTP to send emails in PHP getting blank pge - php

I am trying to send emails using SMTP Server in Core HP. I am getting a blank page not displaying any errors. Can any one help me out? what i am doing wrong? Unable to check the issue as well!
<?php ob_start();
if(isset($_POST['submit_contact'])) {
require 'PHPMailer/PHPMailerAutoload.php';
$to = "abc#gmail.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$txt = $_POST['comment'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "abc#gmail.com";
$mail->Password = "password1#3";
$mail->SetFrom('$email');
$mail->Subject = $subject;
$mail->Body = $txt;
$mail->AddAddress($to);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
}
?>

is your source code located at public_html folder?
In that case you have to include like below
require 'aaa/PHPMailer/PHPMailerAutoload.php';

Related

phpmailer says invalid email when parameter passing

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer-master/src/Exception.php';
require 'PHPMailer-master/src/PHPMailer.php';
require 'PHPMailer-master/src/SMTP.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = "smtp";
$mail->SMTPDebug = 1;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->Host = "smtp.gmail.com";
$mail->Username = "myemail";
$mail->Password = "myemailpassword";
if(isset($_POST['submit'])){
$from = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mail->IsHTML(true);
$mail->AddAddress("myemail", "name");
$mail->SetFrom($from);
$mail->AddReplyTo($from);
$mail->Subject = $subject;
$content = $message;
$mail->MsgHTML($content);
if(!$mail->Send()) {
echo "Error while sending Email.";
var_dump($mail);
}
else {
echo "Email sent successfully";
}
}
?>
the code works if I enter the details myself rather than getting them from my html form. for example for $mail->SetFrom("myemail", "my name")
tried defining the variable when I call it in the mail function that didn't work either

Connection with Gmail SMTP cannot be established whith my php script

I want to make just a basic email sending script. Because I want to run it on localhost I use Gmail SMTP. I have both enabled IMAP settings and I allowed access from less secure apps. Surprisingly, I still cannot connect. Everytime I try, I get the error SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Why is that happening? I am using this code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
if(isset($_POST['submit'])){
$to = $_POST['email'];
$subject = "First email";
$txt = "Do you want our service?";
//require_once('PHPMailer/PHPMailer.php');
require_once "PHPMailer\PHPMailer.php";
require_once "PHPMailer\SMTP.php";
require_once "PHPMailer\Exception.php";
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->isHTML();
$mail->Username = "myemailnotimportant#gmail.com";
$mail->Password = "password123";
$mail->SetFrom('myemailnotimportant#gmail.com', 'Sir Walter Scott');
$mail->Subject = $subject;
$mail->Body = $txt;
$mail->AddAddress($to);
if ($mail->Send()) {
echo "email sent";
} else {
echo "Something is wrong: <br><br>" . $mail->ErrorInfo;
}
}
?>

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

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!";
}

how to make phpmailer code more simple it use to send 2 mails

I have php code using phpmailer to send two different messages to two users. I have duplicate the code twice to send both mails, but that's makes the process takes long tome to complete the task. is there any solution to make my code more simple
//// -------------------- send email. to student adviser ----------------------------------------------------------
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->Username = "rms#gmail.com";
$mail->Password = "12121212";
$mail->AddAddress($advisoremail);
$mail->FromName = "RMS-NCT";
$mail->Subject = "New Request from: ".$_SESSION['UID'];
$mail->Body = "Dear Mr. Adviser you have got new request from 26s12115 ... click here to access it. http://localhost/rms/";
//-----------------------------------------------------------------------
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->From = $mail->Username;
if(!$mail->Send())
echo "Mailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
// ------------send email to student ----------------------
$mail = new PHPMailer();
$mail->Username = "rms#gmail.com"; // your GMail user name
$mail->Password = "12121212";
$mail->AddAddress($_SESSION['UEMAIL']);
$mail->FromName = "RMS-NCT";
$mail->Subject = "Receipt for your new Request";
$mail->Body = "Dear Student .. Your request has been sent.. you will get response as soon as possible.";
//-----------------------------------------------------------------------
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->From = $mail->Username;
if(!$mail->Send())
echo "Mailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
As long as you have two different subjects and bodies, I would say no. There is no way to simplify this task.
But you could put the task in one function, that gets the different parameters. So you just have one function to call.
require("phpmailer/class.phpmailer.php");
function send_mail($email, $subject, $body) {
$mail = new PHPMailer();
$mail->Username = "rms#gmail.com";
$mail->Password = "12121212";
$mail->AddAddress($email);
$mail->FromName = "RMS-NCT";
$mail->Subject = $subject;
$mail->Body = $body;
//-----------------------------------------------------------------------
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->From = $mail->Username;
if(!$mail->Send())
echo "Mailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
}
require("phpmailer/class.phpmailer.php");
function sendMail($to = "trash#domain.com", $subject = "", $body = "", $from = "RMS-NCT")
{
$mail = new PHPMailer();
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Username = "rms#gmail.com";
$mail->Password = "12121212";
$mail->From = $mail->Username;
$mail->FromName = $from;
$mail->AddAddress($to);
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->Send()) {
return false;
}
return true;
}
In code, smth like this:
$Address = $advisoremail;
$Subject = "New Request from: " . $_SESSION['UID'];
$Body = "Dear Mr. Adviser ...";
if (sendMail($Address, $Subject, $Body)) {
echo "Message has been sent";
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}

Categories