How to change email sender email in PHP Mailer - php

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...";
}
?>

Related

Php mailer Error: SMTP connect() failed

I had not any problems before, but i have checked today and i get the following error: Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting. I run my website on Heroku app. I use the latest version of php Mailer with google smtp servers. Are there any alternative smtp servers i can use? Here is my Script:
<?php
require 'PHPMailer-master/PHPMailerAutoload.php';
if(empty($_POST['Email'])){
$_POST['Email']= "";
}
if(empty($_POST['Name'])){
$_POST['Name']= "";
}
if(empty($_POST['Subject'])){
$_POST['Subject']= "";
}
if(empty($_POST['message'])){
$_POST['message']= "";
}
if (isset($_POST["message"]) && !empty($_POST["message"])) {
$mymail=smtpmailer("webdominar1#gmail.com",$_POST['Email'],
$_POST['Name'], $_POST['Subject'], $_POST['message']);
}else{
header('Location: http://webdominar.xyz'); exit();
}
function smtpmailer($to, $from, $from_name, $subject, $body) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = 'test#gmail.com';
$mail->Password = '1234';
$mail->SetFrom($from, $from_name);
$mail->Subject = "Webdominar Contact form ~Name: $from_name ~ subject: $subject
on http://webdominar.xyz ";
$mail->CharSet = 'UTF-8';
$mail->isHTML(true); // Set email format to HTML
$mail->Body = "blabla" ; //end body
$mail->AddAddress($to);
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Well done $from_name, your message has been sent!\nWe will reply to the following email: $from"
. "<br>Your Message: $body";
}
} //end function smtpmailer
?>

Cant set sender name phpmailer

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");

Internal error for PHPmailer on server

$msg = 'God Bless You';
$subj = 'Ryan D\'souza Photgraphy Invoice';
$to = $_POST['email'];
$from = 'emailaddres';
$name = $_POST['client'];
smtpmailer($to,$from, $name ,$subj, $msg);
function smtpmailer($to, $from, $from_name = 'Ryan D\'souza Photgraphy', $subject, $body)
{
global $error;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.sendgrid.net';
$mail->Port = 465;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->IsHTML(true);
$mail->From="emailaddres";
$mail->FromName="Ryan D'souza Photgraphy";
$mail->Sender=$from; // indicates ReturnPath header
$mail->AddReplyTo($from, $from_name); // indicates ReplyTo headers
$mail->AddCC('cc#site.com.com', 'CC: to site.com');
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAttachment('pdfs/'.$_POST['client'].'.pdf');
$mail->AddAddress($to);
$mail->AddAddress("emailaddres");
if(!$mail->Send())
{
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
}
else
{
$error = 'Message sent!';
return true;
}
}
I'm using PHP mailer to attach a file and send mail. the code works on local system but when I upload it on the server that time it gives me Internal server error. I tried commenting and print each line of the code and found out that i get at $mail->Send().

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

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.

Categories