PHPMailer from is showing as Root User - php

I am using PHP Mailer to send emails and i am using SMTP
here is the code i am using:
$email = new PHPMailer();
$email->IsSMTP(); // telling the class to use SMTP
$email->Host = "mail.integradigital.co.uk"; // SMTP server
$email->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$email->SMTPAuth = true; // enable SMTP authentication
$email->Host = "mail.integradigital.co.uk"; // sets the SMTP server
$email->Port = 26; // set the SMTP port for the GMAIL server
$email->Username = "sending#************.co.uk"; // SMTP account username
$email->Password = "********"; // SMTP account password
$email->SetFrom($result["emailfrom"]);
$email->FromName($result["emailfrom"]);
$email->Subject = $result["subject"];
$email->Body = $result["message"];
but its sending and showing as from Root User - root#localhost
i know i can change this in the class.phpmailer.php file but i want to be able to set it in the code above
is this possible?

Okay, in short:
$email->From = "email.address#gmail.com";
$email->FromName = "Support Team";
This worked out for me.

Wrap the whole thing in a try catch. And initialize phpmailer with exceptions enabled
$email = new PHPMailer(true);
It looks like the default values are used. So something goes wrong when calling setFrom(). And it's failing silently because it returns false without exceptions enabled.
try {
// your mail code here
} catch (phpmailerException $e) {
echo $e->getMessage();
}
And change the setting of email and from name to:
$email->setFrom($result["emailfrom"], $result["emailfrom"]);
FromName is a property not a method.

class.phpmailer.php file contains the "var $FromName = 'Root User';" section which can be changed to "Desired Name" from "Root User" and have your job done.

Inside your form when you are getting the 1. email and 2. name it should look like this
<input type="text" name="emailfrom">
<input type="text" name="namefrom">
Inside your php code it should look like this.
$mail->setFrom($_POST['emailfrom'],$_POST['namefrom']);
I was having the same issue when I was using these settings on infinityhost I hope this helps.
$mail->Port = 587; // Which port to use, 587 is the default port for TLS security.
$mail->SMTPSecure = 'tls'; // Which security method to use. TLS is most secure.```

I had this problem when I was sending only the first parameter to setFrom(), as name <email>.
setFrom() did not know how to parse that construction.
Changing to two separate parameters solved the issue.

Use:
$mail->setFrom('here email add of sender','here name of the sender');
First parameter is the email addres if it is empty or not a valid email address it will show as ROOT user while sending mail using php mailer

For PHPMailer Version: 5.1
This worked for me:
$mail->SetFrom('no-reply#example.com', 'Name From');
Hope it works.

Edit your phpmailer.php, changing:
if($mail->Send()){
to:
if (mail($_POST["email"],
$_POST["subject"],
$_POST["message"],
$_POST["sender"])) {
echo'Email Sended';
}
*HTML*
<form action="phpmailer.php" method="POST">
<input type="hidden" name="sender" value="From: Support Team<Support#email.com>"/>
</form>
if ($mail->Send()) { is set to Auto root, if you send email and From is not declared at the same time.

Related

PHPMailer | (mail from) Sender not allowed

I have a contact form made using PHPMailer. It has been working since today, then it suddenly stopped working. I tried to understand why and I discovered I get everytime error about the sender which is not allowed as it says. Here is the code:
try {
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtps.aruba.it';
$mail->SMTPAuth = true;
$mail->Username = 'info#mydomain.it';
$mail->Password = 'myPassword';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom("myemail#gmail.com", 'Sender'); // Here is where I put the sender email
$mail->addAddress('info#mydomain.it');
$mail->isHTML(true);
$mail->Subject = "Subject";
$mail->Body = "Body";
if (!$mail->send()) {
$result = array('status'=>"error", 'message'=>"Mailer Error: ".$mail->ErrorInfo);//
print_r(json_encode($result));
} else {
$result = array('status'=>"success", 'message'=>"Message sent.");
print_r(json_encode($result));
}
}
catch (Exception $e) {
echo "Error excpetion: ".$e;
}
Here is the error I get from the debug:
The following From address failed: myemail#gmail.com: MAIL FROM command
failed,<myemail#gmail.com> ik2dozQHmuz2mik2doPsAn - Mittente non
consentito / Sender not allowed ( mail from )
I tried to update PHPMailer, now I have the latest version. Also, I tried with an outlook.it email I have, but same error again.
It was perfectly working since today, can you help me?
Most likely the server admin has applied anti email spoofing policy today, so yesterday it works, but today it disallows you to send thru smtps.aruba.it as
sender 'xxxxx#gmail.com'
The proper way is to change your "from" address to info#mydomain.it, but if you want the recipient to reply your email but get it sent to xxxxx#gmail.com, then add the following
$mail->ClearReplyTos();
$mail->addReplyTo('xxxxx#gmail.com', 'your name in gmail');
Alternatively, use gmail smtp server to send out the email thru PHPMailer
(you may refer to the official link: https://support.google.com/a/answer/176600?hl=en for the smtp settings needed)
In case you have other problems in using gmail smtp, you may also wish to refer to this SO post (or other related ones)
PHPMailer does not work with Gmail SMTP

Send MULTIPLE Attachments from Input on Form via PHPMailer

I have searched all around but cannot find a specific answer. I have a form there the user can put in MULTIPLE attachments. I am using PHPMailer to send the submitted form. I have looked everywhere and it seems people who explain how to upload multiple files but NOT from user input. THANK YOU!.
My attachment upload button name is "attachments"
if (isset($_POST["submit"])) {
$optionOne = $_POST['optionOne'] ;
$tick = $_REQUEST['tick'] ;
$results = $_REQUEST['results'] ;
$option = $_POST['option'] ;
$option = $_POST['requirements'] ;
$mail = new PHPMailer;
//Server settings
$path = 'upload/' . $_FILES["attachments"]["name"];
move_uploaded_file($_FILES["attachments"]["tmp_name"], $path);
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->isSendmail(); // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'bonnie'; // SMTP username
$mail->Password = 'bonnie'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('bonniethompson12345#gmail.com', 'Mailer');
$mail->addAddress('bonniethompson12345#gmail.com'); // Add a recipient
$mail->isHTML(true);
$mail->AddAttachment($path); // Set email format to HTML
$mail->Subject = 'Form submission';
$mail->Body = 'This course is identified in my Work Plan and Learning Agreement: $optionOne \n \n I am attending this session because: $tick \n \n What would you like to achieve as a result of your attendance: $results \n \n Do you require adjustments or additions to the session delivery to support your participation: $option \n \n Please provide details of your requirments: $requirements</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
//$mail->AddAttachment('Logo.png');
if(!$mail->send()) {
echo "Failure to send";
} else {
echo "Message has been sent successfully";
}
}
<p>Please upload any supporting documentation to support your registration request </p>
<div class="browse-button">
<input type="file" name="attachments" multiple="multiple"></input>
</div>
The thing you're missing is the naming of the input element; you need to use array naming to make it handle multiple files correctly, so build it like this:
<input type="file" name="attachments[]" multiple="multiple">
The important bit is the [] at the end of the element name, which means that the multiple values will get submitted as an array. When you receive a submission from that, try a var_dump($_FILES) so you can see what you've got. You should also check the return value of move_uploaded_file() rather than assuming it works. Also you don't need a closing </input> tag if you're using HTML5.
The multiple file upload example provided with PHPMailer demonstrates exactly what you're asking, and includes the error checking I mentioned.
It's also covered in the PHP docs. Look harder next time!

customizing mail sending form in php using core php

Well i am in need of little help here.
I am doing a php custom mail sending form in which i need to make custom option for user to setup - port, host, smtp email, smtp pass, smtp username, also there is option to enable or disable smtp, Those all are working, but i have a situation here.
i also want to enable SSL Option for user, so if user select SSL is yes then it will send using SSL to prevent email going to SPAM. but i am unable to do this, can anyone help me with this.
here is code that i am using please check and assist anyone-
https://www.dropbox.com/s/m7ltrl8q0dxic4v/smtpmail-dummy.zip?dl=0
Here for this set the SMTPSecure property of PHPMailer class to 'ssl'. Then connect it using SmtpConnect() method.
So basically you can write a code like below. Not exactly, just for reference.
<?php
$mail = new PHPMailer; // call the class
$mail->IsSMTP();
$mail->Host = SMTP_HOST; //Hostname of the mail server
$mail->Port = SMTP_PORT; //Port of the SMTP like to be 25, 80, 465 or 587
$mail->SMTPAuth = true; //Whether to use SMTP authentication
$mail->Username = SMTP_UNAME; //Username for SMTP authentication any valid email created in your domain
$mail->Password = SMTP_PWORD; //Password for SMTP authentication
$mail->AddReplyTo("developer#vrstamphead.com", "Developer Rakesh");
//reply-to address
$mail->SetFrom("developer#vrstamphead.com", "Developer Rakesh"); //From address of the mail
// put your while loop here like below,
$mail->Subject = "Your SMTP Mail"; //Subject od your mail
$mail->AddAddress($email, "Asif18"); //To address who will receive this email
$mail->MsgHTML("<b>Hi, your first SMTP mail has been received. Great Job!.. <br/><br/>by <a href='http://www.asif18.com/7/php/send-mails-using-smtp-in-php-by-gmail-server-or-own-domain-server/'>Asif18</a></b>"); //Put your body of the message you can place html code here
if ($_SESSION['enable_ssl'] = 1) { // 1 for true and 0 for false
$mail->SMTPSecure = '';
$mail->SmtpConnect();
}
$send = $mail->Send(); //Send the mails
if($send){
echo '<center><h3 style="color:#009933;">Mail sent successfully</h3></center>';
}
else{
echo '<center><h3 style="color:#FF3300;">Mail error: </h3></center>'.$mail->ErrorInfo;
}
?>
Instead of $_SESSION['enable_ssl'], you can use $_POST['enable_ssl'] as well if you're providing it right a way.
Hope this helps...

php email, code reuse fails on second include

I posted earlier as an email and relocation issue, however after trying several things, and tracing (in production! yech!)
I have found the culprit - but have NO IDEA why or where to go from here.
you will see in the below code I include 'email.php' twice - because one receipt goes to the user, the other includes some meta data about the user and goes to support...
if i comment out the SECOND include, my redirect works, if i leave it in, it bombs...
the notify email is valid, and is the only thing that is different.
Im at a loss
PHP FORM PROCESSOR
...
// send two emails
$_emailTo = $email; // the email of the person requesting
$_emailBody = $text_body; // the stock response with things filled in
include ( 'email.php' );
$_emailTo = $notifyEmail; // the support email address
$_emailBody = $pretext.$text_body; // pretext added as meta data for support w/ same txt sent to user
//I make it this far in my trace - then nothing
include ( 'email.php' );
// relocate
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php" >';
exit;
PHP MAILER (email.php)
<?php
require 'phpmailer/class.phpmailer.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->IsSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Set the hostname of the mail server
$mail->Host = "mail.validmailserver.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 26;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "validusername";
//Password to use for SMTP authentication
$mail->Password = "pass1234";
//Set who the message is to be sent from
$mail->SetFrom('me#validmailserver.com', 'no-reply # this domain');
//Set an alternative reply-to address
//$mail->AddReplyTo('no-reply#validmailserver.com','Support');
//Set who the message is to be sent to
$mail->AddAddress( $_emailTo );
$mail->Subject = $_emailSubject;
$mail->MsgHTML( $_emailBody );
$_emailError = false;
//Send the message, check for errors
if( !$mail -> Send() ) {
$_emailError = true;
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
help - please
It's the require in email.php that is causing the problems. If you want to make the least change possible and make it work, change it to require_once. This will ensure "phpmailer/class.phpmailer.php" is only loaded once, therefore the class will only be defined once.
take the require() line from email.php and put it in the calling file

PHPMailer setting for individual emails is sending empty To: fields

Using PHPMailer to send individual emails to recipients I'm getting nothing in the To: field when I add $mail->SingleTo = TRUE; to my code.
When I remove $mail->SingleTo = TRUE; I receive emails with an email address in the To: field that is correct.
This is what I get:
reply-to xxxxxx <xxxx#xxxx.com>, No Reply <no-reply#no-reply.com>
to
date Mon, Mar 21, 2011 at 5:07 PM
subject Testing
mailed-by gmail.com
signed-by gmail.com
(where xxxxxxx represents my email address.)
Here's my code:
if(isset($_POST['submit']))
{
require_once('PHPMailer_v5.1/class.phpmailer.php');
$mail = new PHPMailer();
$subject = $_POST['subject'];
$body = $_POST['emailbody'];
$to = $_POST['to'];
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "localhost"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "SSL"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "xxxxxxxxxx#gmail.com"; // GMAIL username
$mail->Password = "*********"; // GMAIL password
$mail->SetFrom('xxx#xxx.com', 'XXXXXX');
$mail->AddReplyTo("no-reply#xxxxx.com","No Reply");
$mail->Subject = $subject;
// After adding this line I'm getting an empty To: field
$mail->SingleTo = TRUE;
$mail->AddAddress("address1#xxxxxx.com", 'xyz abc');
$mail->AddAddress("address2#xxxxxx.com", 'abc xyz');
//$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
if(!$mail->Send()) {
$message= "Mailer Error: " . $mail->ErrorInfo;
}
else
{
$message= "Message sent!";
}
}
You are using SMTP to send email. The phpmailer class is not using the SingleTo parameter when senting using Smtp.
More, if you see the CreateHeader function when the SingleTo == true the recipents are only aded to $this->SingleToArray and not to the header itself so basically it PHPmailer bug.
Looks like the only choice, unless you want to patch phpmailer, is to send email one-by-one without using SingleTo property
the solution will be
function & prepare_mailer($subject, $body) {
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "localhost"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "SSL"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "xxxxxxxxxx#gmail.com"; // GMAIL username
$mail->Password = "*********"; // GMAIL password
$mail->SetFrom('xxx#xxx.com', 'XXXXXX');
$mail->AddReplyTo("no-reply#xxxxx.com","No Reply");
$mail->Subject = $subject;
$mail->MsgHTML($body);
return $mail;
}
foreach( $_POST['to'] as $to ){
$mail = null;
$mail = & prepare_mailer($_POST['subject'],$_POST['body']);
$mail->AddAddress($to['address'], $to['name']);
$mail->Send();
}
Set up all the other parameters, then in a loop set the recipient, send the email, then use the ClearAllRecipients() function. Like so:
{ // loop start
$mail->AddAddress($person_email, $person_name);
$mail->Send();
$mail->ClearAllRecipients();
} // loop end
The problem is in the SmtpSend function (starting at line 701 in class.phpmailer.php). As you can see there, they don't take the singleTo setting into account, and just add all recipients and send the body of the message once.
So you should add some logic there to test if singleTo is true, and if it is modify the code so it sends these mails separately, prefixing the To: header to the body of the message. If singleTo is false you can call the code as-is (line 713 - 758).
Or alternatively, if you don't want to patch things, then you could use a transport method (ie. sendmail) that does seem to support the singleTo parameter.
$mail->AddAddress()
doesn't like CSV's
so if you have:
$Emails="addr1#host.com,addr2#host.net"; #etc;
do a for loop after a split:
$NewList = preg_split("/,/",$Emails);
foreach ($NewList as $k=>$email){
if ($k==0) $mail->AddAddress($email); # Add main to the "To" list.
else $mail->AddCC($email); # The REST to CC or BCC which ever you prefer.
}
-- BF.
SingleTo Is not a good idea. It only works with "sendmail" or "mail" transports, not with SMTP. If you use SingleTo with SMTP, this parameter is just ignored without any error or warning, and you may get duplicates.
Since you use both SingleTo an SMTP, as shown in your code, the SingleTo in your case is ignored.
The SMTP protocol is designed in a way that you cannot send one message to several different recipients, each having only its own address in the TO: field. To have each recipient have only its name in the TO:, the whole message have to be transmitted again. This explains why SingleTo is incompatible with SMTP.
According to the authors of the PHPMailer library, SingleTo is planned to be deprecated in the release of PHPMailer 6.0, and removed in 7.0. The authors have explained that it's better to control sending to multiple recipients at a higher level, since PHPMailer isn't a mailing list sender. They tell that the use of the PHP mail() function needs to be discouraged because it's extremely difficult to use safely; SMTP is faster, safer, and gives more control and feedback. And since SMTP is incompatible with SingleTo, the authors of PHPMailer will remove SingleTo, not SMTP.

Categories