I am facing this problem where the mail is getting sent as a different email rather than the one specified.
$from_name = 'send';
$from = 'send#test.com';
$to = 'receive#test.com';
$to_name = 'receive';
$also_to = 'cc#test.com';
$also_to_name = 'cc';
$message = 'Dear receive Thank you for the booking';
$message .= '<br><br>'.$homepage;
$mail = new PHPMailer();
$mail->SMTPDebug = 0;
$mail->IsSMTP();
$mail->Port = 465;
$mail->Host = "ssl://smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = 'send1#test.com';
$mail->Password = ******;
$mail->SetFrom($from,$from_name );
$mail->AddReplyTo($from,$from_name );
$mail->Subject = 'Booking Details';
$mail->MsgHTML($message);
$mail->AddAddress($to,$to_name);
$mail->AddCC($also_to,$also_to_name);
When I sent it using this code the mail goes with name as 'send' but not the 'send#test.com' email address it gets sent from "send -> send1#test.com" rather than send -> send#test.com.
Above I have mentioned the smtp details that i have used. Also when i change:
$mail->Username = 'send#test.com';
$mail->Password = ******;
The mail does not go through to any email address. I hope this is clear enough for you guys and I would appreciate any help. Thank you
$mail->Username = send1#test.com;
this need to be in quotes like :
$mail->Username = "send1#test.com";
Check whether this resolves you issue ?
If not try setting debug to true $mail->SMTPDebug = 1;
and see what's being actually sent to gmail server
By default gmail will use the account's email address
Ref : How to change from-address when using gmail smtp server
Look at this: http://phpmailer.worxware.com/index.php?pg=properties
$From public root#localhost Sets the From email address for the message
and
$FromName public Root User Sets the From name of the message
So you want specifically add the following:
$mail->From = $from;
$mail->FromName= $from_name;
You have to call setFrom method to set the sender's mail address and name:
$mail->SetFrom('send#test.com', 'Your Name');
Related
There is a form in my webpage, message from the contact form goes to an email of a smtp server. I have used this codes for sending the message:
require_once("PHPMailer-master/PHPMailerAutoload.php");
$fromName = $_POST['username'];
$fromEmail = $_POST['email'];
$theMessage = $_POST['message'];
$theSubject = $_POST['subject'];
$theCompany = $_POST['company'];
$thePhone = $_POST['phone'];
$isSuccess = 0;
$notificationMsg = "";
$mail = new PHPMailer;
// $mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 465;
// Authentication
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
// Compose
$mail->SetFrom($fromEmail, $fromName);
$mail->addReplyTo($fromEmail, $fromName);
// Send To
$mail->addAddress('info#mycompany.com', 'My Company');
$mail->WordWrap = 50;
$mail->isHTML(true);
if ($mail->send()) {
$isSuccess = 1;
$notificationMsg = "Thank you for your message";
} else {
$isSuccess = 0;
$notificationMsg .= "Sorry, there is something wrong. Please, try again letter.";
exit ;
}
echo $notificationMsg;
But, it didn't work. If I add these line for Compose section:
// Compose
$mail->From = 'user#mycompany.com'; // any email address from our own
it will work then! And it shows at our email box:
from: Root User <user#mycompany.com>
reply-to: Sender Name <sender#gmail.com>
to: My Company <info#mycompany.com>
Message body at the email box is okay. But, instead of sender's email address, our email address is showed at form field. Also, Root User is showed instead of sender's name. If I add one more line at compose section:
// Compose
$mail->From = 'user#mycompany.com'; // any email address from our own
$mail->FromName = 'Anything';
It shows then:
from: Anything <user#mycompany.com>
reply-to: Sender Name <sender#gmail.com>
to: My Company <info#mycompany.com>
Even, I tried with this:
// Compose
$mail->From = $fromEmail;
$mail->FromName = $fromName;
But, message won't sent then form my contact form.
So, for compose section,
// Compose
$mail->From = 'user#mycompany.com'; // any email address from our own
$mail->FromName = 'Anything';
$mail->SetFrom($fromEmail, $fromName);
$mail->addReplyTo($fromEmail, $fromName);
3rd line doesn't seem working. But, that line should be working instead of first two lines and at our email box, it should be shown:
from: Sender Name <sender#gmail.com>
reply-to: Sender Name <sender#gmail.com>
to: My Company <info#mycompany.com>
How to solve that problem? Thanks in advance.
Don't try to use the submitter's address as the from address; it's forgery and even if you can get away with sending with it (which it looks like you can't anyway), it will cause your messages to fail SPF checks and be spam-filtered or bounced. Put your own address in the from address and the submitter's address in a reply-to, as the contact form example provided with PHPMailer shows you.
The combination of $mail->SMTPSecure = 'tls'; and $mail->Port = 465; will not work; change Port to 587 or SMTPSecure to ssl.
Read the docs, base your code on the examples provided with PHPMailer, and update to the latest version.
Need help!!!
I am using php mailer in my current project. It works successfully, after sending a mail successfully to the other end it says the mail comes from the smtp server account not from the $from what i use here-
$to ="muradautorun#gmail.com";
$from ="tanvir_cse0906#yahoo.com";
My smtp server code here..
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = "muradautorun";
$mail->Password = ""; //my passsword here
Here is my complete code-
$to ="muradautorun#gmail.com";
$from ="tanvir_cse0906#yahoo.com";
$from_name="S.M. Murad Hasan Tanvir";
$subject="A sending mail code";
$body ="This is a sending email code, the file attach here is about the coding.";
//here i use gmail as a smtp server
$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 = "muradautorun";
$mail->Password = ""; //my passsword here
$mail->SetFrom($from, $from_name);
$mail->addAttachment('emailSend.php'); // Add attachment(just the file url)
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
echo 'Mail error: '.$mail->ErrorInfo;
}
else {
echo 'Message sent!';
}
?>
Where i doing wrong i can't find out please help me...
Thanks in advance.
You need to allow sending mail as "tanvir_cse0906#yahoo.com" on your "muradautorun" gmail account first.
You can do this by following the following steps:
Login with "muradautorun"
Click the gear in the top right and choose "Settings"
Click on the "Accounts and Import" tab
Click on link "Add another email address you own" in the "Send mail as" section, and follow the instructions.
I am trying to setup PHPMailer for a customer. He has his own mail server located at a certain IP address. When asked to give me the information to send email through the system, he gave the following:
Host: xx.xxx.x.x
Port: 25
Domain: mydomain.local
Username: myemail#mydomain.local
Password: <myemailpassword>
From: myemail#anotherdomain.xx
(Which he confirmed is being used for external email sending)
I tried to setup PHPMailer by setting the parameters to the exact namings above.
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "xx.xxx.x.x";
$mail->Port = 25;
$mail->Username = "myemail#mydomain.local";
$mail->Password = <myemailpassword>;
$mail->SetFrom('myemail#anotherdomain.xx', 'Webname');
$mail->[...]
I got the following error:
Failed to connect to server (0)
So I try to send an email through telnet to check if it's the customer's email server or the PHPMailer settings:
telnet xx.xxx.x.x 25
It goes through, I'm connected to the server.
helo mydomain.local
I'm getting 'Hello' as a reply. This leads me to believe it might be the PHPMailer settings that are wrong here.
I also try not using SMTP:
$mail->Host = "ssl://xx.xxx.x.x";
$mail->Port = 25;
$mail->Username = "myemail#mydomain.local";
$mail->Password = "password";
$mail->SetFrom('myemail#anotherdomain.xx', 'Webname');
$mail->[...]
Again no go. Am I going about this wrong? I'm only familiar with setting up PHPMailer to use Gmail before so I'm at a loss as to what could be the issue because I'm using a 'personal' email server.
Thanks Loadparts for your assistance.
I'm still not sure what the issue was but it seems it has resolved itself. It might have been from the email server side because coding wise, I didn't change anything. This is the final code I used.
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Port = 25;
$mail->Host = "xx.xxx.x.x"; // SMTP server
$mail->Username = "myemail#mydomain.local";
$mail->Password = <myemailpassword>;
$mail->From = "myemail#anotherdomain.xx";
$mail->FromName = <Web_Name>;
$mail->AddAddress("email#domain.com");
$mail->Subject = <Subject>;
$mail->AltBody = <Alt_Body>
$mail->WordWrap = 80;
$body = "test message";
$mail->MsgHTML($body);
$mail->IsHTML(true);
$mail->Send();
I use a test function that I know works 100% to test the email servers when using PHPMailer.
I'm not sure why you are having your problem, but try to use the function I have ( I know it's messy but it does the trick). Just replace all the XXXX with your info and make sure you have both class.phpmailer.php and class.smtp.php in the same folder.
<?php
error_reporting(E_ALL);
$toemail = 'XXXX';
$toname = 'XXXX';
$subject = 'Testing Email Sending...';
$bodyhtml = '<H1>yeah</h1>';
$bodytext = 'heres Hoping it works';
$fromemail = 'XXXX';
$fromname = 'XXXX';
var_dump(sendemail($toemail,$toname,$subject,$bodyhtml,$bodytext,$fromemail,$fromname));
function sendemail($toemail,$toname,$subject,$bodyhtml,$bodytext,$fromemail,$fromname)
{
require_once("class.phpmailer.php");
$mail = new phpmailer();
$mail->IsSMTP();
$mail->From = $fromemail;
$mail->FromName = $fromname;
$mail->Host = "XXXX";
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "XXXX"; // SMTP username
$mail->Password = "XXXX"; // SMTP password
$mail->Port="25";
$mail->SMTPDebug=true;
if(strlen($bodyhtml)>0) {
$mail->Body = $bodyhtml;
$mail->IsHTML(true);
}
else if(strlen($bodytext)>0){
$mail->Body = $bodytext;
}
if(strlen($bodytext)>0 && strlen($bodyhtml)>0){
$mail->AltBody = $bodytext;
}
$mail->AddReplyto($fromemail,$fromname);
$mail->Subject = $subject;
// Check if multiple recipients
if(preg_match("/;/",$toemail))
{
$tmp_email=preg_split("/;/",$toemail);
$tmp_contact=preg_split("/;/",$toname);
$mail->AddAddress($tmp_email[0], $tmp_contact[0]);
// echo "<!-- multi email:".$tmp_email[0]." contact:".$tmp_contact[0]." -->\n";
for($j=1;$j<count($tmp_email);$j++)
{
if(preg_match("/\#/",$tmp_email[$j]))
{ $mail->AddCC($tmp_email[$j], $tmp_contact[$j]);
// echo "<!-- multi email cc:".$tmp_email[$j]." contact:".$tmp_contact[$j]." -->\n";
}
}
}
else{
$mail->AddAddress($toemail, $toname);
}
$error= false;
if($mail->Send()){
$error =true;
}
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
return $error;
}
If this doesn't work, my first try would be using port 80 - which usually isn't blocked, then you can work on getting SSL to work.
PS: because it's a local domain, you may want to consider adding the domain to your /etc/hosts just to be sure.
Best of Luck!
I'm using the following code to send a mail after a form submission with the PHP mailer class https://github.com/Synchro/PHPMailer. The mail sends and is received successfully. The only thing that isn't wokring is the following:
$mail->From = $email;
$email is the email that a user will enter on the form (it is set with a $_POST variable). I would like the email to appear that it's from the user who filled out the form, so I can hit reply and have it go to their email address.
However, the "from" email address is being set as $mail->Username, i.e. the username from the gmail account that the PHPMailer script is sending from.
What am I doing wrong here, and how do I get the From email header to work?
Also, I am using Gmail to receive the mail-- maybe there's a gmail security setting that won't allow the "From" email to be "faked"???
Thanks!
$email = $_POST['email'];
$name = $_POST['moveName'];
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'example#gmail.com';
$mail->Password = 'password';
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress('me#gmail.com');
$mail->AddReplyTo($email, $name);
$mail->IsHTML(true);
$mail->Subject = 'Quote Request';
$mail->Body = 'hey';
$mail->Send();
With gmail you have to configure an email address as allowed "$mail->from" first. This FAQ entry explains how to do it: https://support.google.com/mail/answer/22370?hl=en
I am trying to add a reply to address to my php mailer and it just puts from "me" and replies to my address.
Any ideas what I am doing wrong? I have added the $mail->AddReplyTo. I want it to reply to the sender of the web form.
$name = $_POST['name'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = file_get_contents('phpmailer/contents.html');
$body = eregi_replace("[\]",'',$body);
$body = eregi_replace("<name>", $name,$body);
$body = eregi_replace("<telephone>", $telephone, $body);
$body = eregi_replace("<email>", $email, $body);
$body = eregi_replace("<message>", $message, $body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
// 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 = 465; // set the SMTP port for the GMAIL server
$mail->Username = "xxx#xxx.net"; // GMAIL username
$mail->Password = "xxxxx";
$mail->AddReplyTo($email, $name);
$address = "xxxx.net";
$mail->AddAddress($address, "Contact form");
$mail->Subject = " Contact Form";
Something to try is to make sure your $email and $name variables are being passed in correctly (add some debugging statements to echo them out). Not sure if you have done that or if you are checking if the form has posted or not. But that would be step one.
From my workings with PHPMailer and GMail, they do not work to well. Instead I would suggest trying the phpGMailer script. It works great for GMail. See if that does not fix your issues.
UPDATE
Thinking about it, I do not think GMail permits the changing of the ReplyTo address unless the GMail account has activated authorization for that account. I am not 100% sure on this, but I know through the web interface that is not possible.
Off Topic
I would avoid using eregi_replace it is depreciated. I would use preg_replace instead. Here is an updated version so you can update your code:
$body = file_get_contents('phpmailer/contents.html');
$body = preg_replace("~[\]~",'',$body);
$body = preg_replace("~<name>~i", $name,$body);
$body = preg_replace("~<telephone>~i", $telephone, $body);
$body = preg_replace("~<email>~i", $email, $body);
$body = preg_replace("~<message>~i", $message, $body);