Cant set sender name phpmailer - php

So I'm setting up a script to send emails from a contact us form. Everything is working great besides the fact that I can't set sender's name properly. I get an error:
Warning: Creating default object from empty value
My code looks like this:
<?php $eemail = $_REQUEST['email'] ;
$message = $_REQUEST['message']; $subject = $_REQUEST['subject']; $fname =
$_REQUEST['name'];
require("class.phpmailer.php"); $mail = new PHPMailer();
$mail->Username = "email"; // SMTP username
$mail->Password = "password"; // SMTP password $mail->IsSMTP();
$mail->SMTPAuth = true; $mail->Host = 'aspmx.l.google.com';
$mail->Port = 25;
$mail->From = $eemail; $name->FromName = $fname; $mail->Subject =
$subject;
$mail->AddAddress("email", "name");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Body = $message; $mail->AltBody = $message;
if(!$mail->Send()) { echo "Message could not be sent. <p>"; echo
"Mailer Error: " . $mail->ErrorInfo; exit; }
echo "Message has been sent"; ?>

add this code
$mail->SetFrom("$eemail ", "$fname");

Related

Which PHPMailer prop controls the sender email in the email header?

I'm getting an error from jQuery with a 404 to my mail.php (I checked the path and it seems correct)
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require("../includes/libraries/phpmailer/PHPMailer-master/src/Exception.php");
require("../includes/libraries/phpmailer/PHPMailer-master/src/PHPMailer.php");
require("../includes/libraries/phpmailer/PHPMailer-master/src/SMTP.php");
$server_email = 'myemail#outlook.com';
$pass = '123456';
$full_name = $_POST['full_name'];
$email = $_POST['email'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsHTML(true);
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp-mail.outlook.com';
$mail->Port = 587;
$mail->Username = $server_email;
$mail->Password = $pass;
$mail->FromName = $full_name;
$mail->From = $email;
$mail->setFrom($server_email, $full_name );
$mail->addAddress($server_email);
$mail->addReplyTo($email); //email that it will be replied to
$mail->Subject = 'Subscription submitted by ' . $full_name . '.';
$mail->Body = "
Name: $full_name<br><br>
Email: $email<br>
";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
?>
Right now, my email (the outlook one) comes up in the header of the email next to the Sender's Name. The addReplyTo actually does give me the gmail address to reply to, so that's great. But having my email show on the sender header is confusing. See the image below
Thanks in advance

How to change email sender email in PHP Mailer

Currentlt it shows the mail on which phpmailer SMTP is registered.
$email='abc#email.com';
$subjectRsc="Any";
$message='Welcome';
phpmail($email, $subjectRsc, $message);
My phpmailer function:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->Debugoutput = 'html';
$mail->Host = 'smptp';
$mail->Port = 465; // or 587
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->SMTPAuth = true;
$mail->Username = PHP_MAILER_EMAIL;
$mail->Password = PHP_MAILER_PASSWORD;
$mail->AddReplyTo(REPLY_EMAIL, 'ABC');
$mail->SetFrom(FROM_EMAIL, 'ABC');
$mail->Subject = $subject;
$address = $to;
$mail->AddAddress($address);
$mail->MsgHTML($message);
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
return true;
}
This is how I am sending mails to the users I want to show the specific mail of my website to be displayed in the mails not that on which smtp server is registered.
You should try this :
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->Debugoutput = 'html';
$mail->Host = 'smptp';
$mail->Port = 465; // or 587
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->SMTPAuth = true;
$mail->Username = PHP_MAILER_EMAIL;
$mail->Password = PHP_MAILER_PASSWORD;
$mail->addReplyTo('myemail#example.com', 'ABC'); //<-- this is the line i changed
$mail->From= "myemail#example.com"; //<-- this is the line i changed
$mail->Subject = $subject;
$address = $to;
$mail->AddAddress($address);
$mail->MsgHTML($message);
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
return true;
}
According to phpmailer documentation this is the field to add the sender's email.
please try using this code :
<?php
require_once "vendor/autoload.php";
//PHPMailer Object
$mail = new PHPMailer;
//From email address and name
$mail->From = "from#yourdomain.com";
$mail->FromName = "Full Name";
//To address and name
$mail->addAddress("test#gmail.com", "Test");
$mail->addAddress("test1#gmail.com");
//Address to which recipient will reply
//if you want to add CC and BCC
$mail->addCC("cc#example.com");
$mail->addBCC("bcc#example.com");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
?>
Please try using this code:
<?php
$to = "test#gmail.com";
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:test1#gmail.com \r\n";
$header .= "Cc:test2#gmail \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
?>

PHP mailer Error receiving an email

I am using the code below for the mailing list of the website I am working on, and it's working fine on my localhost using my credentials and on some other server using some other credentials were given to me.
<?php
if(isset($_POST['submit']))
{
require("class.phpmailer.php");
$mail = new PHPMailer();
$query = "SELECT * FROM mailing_list";
$result = mysqli_query($connect, $query)
or die('Error querying the database!');
$mail->IsSMTP();
$mail->Host= "smtp";
$mail->Username= "some email address was given to me";
$mail->Password= "password";
$mail->SMTPAuth= true;
$mail->SMTPSecure = 'tls';
$mail->From ="some email address was given to me";
$mail->FromName ="CSCA";
while($row = mysqli_fetch_array($result))
{
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$to = $row['email'];
$mail->ClearAddresses();
$mail->AddAddress($to);
$mail->Subject= $_POST['subject'];
$mail->Body = "Dear $first_name $last_name,\n" . $_POST['body'];
$mail->WordWrap = 50;
if(!$mail->Send())
{
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
Now when I use the following code for the contact us page of the same website it works fine one my localhost using my own credentials, but it doesn't work on the other server I used above. I got a message in the spam 'mail delivery failed'. I don't know how with the same credentials you can send mail but you can't receive! I mean with the same server and same credentials you the mailing list works fine but the contact us form gives an error. Is there a way to fix that??
Thanks in advance!
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$name = $_REQUEST['name'];
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$mail->IsSMTP();
$mail->Host="smtp";
$mail->Username="some email address was given to me";
$mail->Password="password";
$mail->SMTPAuth=true;
$mail->SMTPSecure = 'tls';
$mail->From =$email;
$mail->FromName =$name;
$mail->AddAddress("some email address was given to me", "Some name");
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

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

Not Able to Send Mail Using PHP?

I am trying to send a mail through core Php, I have created a file email.php and included PHPMailer_v5.1 in it.. here is my php code,
<?php if($_POST){
$email = $_REQUEST['email'] ;
$comments = $_REQUEST['comments'] ;
$phone = $_POST['telephone'] ;
$name = $_REQUEST['name'] ;
$mail = new PHPMailer();
$mail->Host = "localhost";
$mail->From = "scorpion.schizo#gmail.com";
$mail->FromName = "Showket";
$mail->AddAddress("manusurya9139#gmail.com");
$mail->Subject = "Feedback form results";
$mail->Body="
Name: $name
Email: $email
Telephone: $telephone
Comments: $comments";
$mail->WordWrap = 50;
if(!$mail->Send())
{
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
echo 'Thank you for your feedback.';
}
}
?>
when i post this form it gives me the right message Thank you for your feedback. but i didnt recieve the message..do i need to configure anything else ?
Try setting Gmail as your SMTP
<?php if($_POST){
$email = $_REQUEST['email'] ;
$comments = $_REQUEST['comments'] ;
$phone = $_POST['telephone'] ;
$name = $_REQUEST['name'] ;
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "ssl://smtp.gmail.com:465"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "scorpion.schizo#gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->Host = "localhost";
$mail->From = "scorpion.schizo#gmail.com";
$mail->FromName = "Showket";
$mail->AddAddress("manusurya9139#gmail.com");
$mail->Subject = "Feedback form results";
$mail->Body="
Name: $name
Email: $email
Telephone: $telephone
Comments: $comments";
$mail->WordWrap = 50;
if(!$mail->Send())
{
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
echo 'Thank you for your feedback.';
}
}
?>
Run "sendmail -bp" to check if there is anything in the queue. Also, check the email folder for the user that your webserver is running under to see if you have had any undeliverable notifications.

Categories