I have a problem on a server for which the additional headers get ignored.
Code to reproduce:
$toEmail="customer#domain.com";
$subject="subject";
$message="this is a message";
$fromEmail="Customer Service <customerservice#domain.net>";
$replyToEmail="Customer Service <customerservice#domain.net>";
mail("$toEmail", $subject, $message, "FROM: $fromEmail","-f$replyToEmail");
echo "mail sent";
The from address ends up being:
customerservice#domain.net.prod3.domain.net
prod3.domain.net is the name of the server, so it concatenates the from address and the machine name.
Server is LAMP running Centos5.
Thanks!
Cam
1) Please enclosed the email name with double quote, e.g. $fromEmail = "\"Customer Service\" "
2) Please change the "FROM" to "From". For example, "From: $fromEmail"
Related
I'm building a website that sends and email to a user when he registers.
My code (the gist of it):
<?php
$to = "helloworld#gmail.com";
$subject = "Test mail";
$message = "Hello! \nThis is a simple email message.";
$headers = "From: munged#gmail.com";
$headers .= "\r\nReply-To: munged#gmail.com";
$headers .= "\r\nX-Mailer: PHP/".phpversion();
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
the problem is that when the mail is delivered, the from header remains munged#box123.bluehost.com, while reply-to gets changed to the specified value.
box123.bluehost.com is the hostname of the server on which the website is hosted.
So what am I doing wrong? What can I do to get the "From" address the same as the reply-to address?
Is it something I'm doing wrong, or is the web host playing foul?
Edit: I just noted that you are trying to use a gmail address as the from value. This is not going to work, and the ISP is right in overwriting it. If you want to redirect the replies to your outgoing messages, use reply-to.
A workaround for valid addresses that works with many ISPs:
try adding a fifth parameter to your mail() command:
mail($to,$subject,$message,$headers,"-f your#email.here");
It turns out the original poster's server (blueHost) has a FAQ concerning this very question.
Article 206.
This is because our servers require you (or your script) to use a properly formatted, valid From: field in the email's header. If the From: field is not formatted correctly, empty or the email address does not exist in the cPanel, the From: address will be changed to username#box###.bluehost.com.
You must change the script you are using to correctly use a valid From: header.
Examples of headers that should work would be:
From: user#domain.com
From: "user" <user#domain.com>
Examples of headers that will NOT work:
From: "user#domain.com"
From: user # domain.com
From: user#domain.com <user#domain.com>
Our servers will not accept the name for the email address and the email address to be the same. It will not accept a double declaration of the email address.
For scripts such as Joomla and Wordpress, you will need to follow their documentation for formatting the from fields properly. Wordpress will require the Mail From plugin.
Note: The email address you use must be a valid created account in the
cPanel.
I had the same Issue, I checked the php.net site. And found the right format.
This is my updated code.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $fromName . ' <' . $fromEmail .'>' . " \r\n" .
'Reply-To: '. $fromEmail . "\r\n" .
'X-Mailer: PHP/' . phpversion();
The \r\n should be in double quotes(") itself, the single quotes(') will not work.
In order to prevent phishing, some mail servers prevent the From from being rewritten.
I realize this is an old thread, but i had the same problem since i moved to bluehost yesterday. It may not have been the selected answer but i support the bluehost article 206 reply.
I created a valid email in control panel and used it as my From address and it worked.
I solved this by adding email accounts in Cpanel and also adding that same email to the header from field like this
$header = 'From: XXXXXXXX <test#test.org>' . "\r\n";
The web host is not really playing foul. It's not strictly according to the rules - but compared with some some of the amazing inventions intended to prevent spam, its not a particularly bad one.
If you really do want to send mail from '#gmail.com' why not just use the gmail SMTP service? If you can't reconfigure the server where PHP is running, then there are lots of email wrapper tools out there which allow you to specify a custom SMTP relay phpmailer springs to mind.
C.
headers were not working for me on my shared hosting, reason was i was using my hotmail email address in header.
i created a email on my cpanel and i set that same email in the header yeah it worked like a charm!
$header = 'From: ShopFive <site#mysite.org>' . "\r\n";
I know this seems like it is a duplicate but please read first:
I have the following php code:
<?php
$to = 'myemail#yahoo.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$headers = "From: ".$email." \r\n";
$headers .= "Reply-To: ".$email."\r\n";
mail($to, $subject, $message, $headers);
?>
I think it's the standard email sending script. However, I face an interesting bug. My website is florin-pop.com and the emails are only sending when in the email input field I put something like this: blahblah#florin-pop.com or mama#florin-pop.com or anything before #florin-pop.com.
If I try to put a something different like test#yahoo.com or even a real yahoo email address I don't get the email. Why? It's something wrong with my code? It may be from the hosting company? ( I use hostgator ).
EDIT:
If I change the Reply-To to the domains email address then it is working, but it is still not the perfect way to do it. If you press the reply button and forget about this trick, you will email yourself.
Code:
<?php
$to = 'myemail#yahoo.com';
$my_domain_email = 'myemail#mydomain.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$headers = "From: ".$email." \r\n";
$headers .= "Reply-To: ".$my_domain_email."\r\n";
mail($to, $subject, $message, $headers);
?>
In this case, delivery failure may be caused by Yahoo's adoption of the Domain-based Message Authentication, Reporting, and Conformance (DMARC) policy.
This means all DMARC compliant mail receivers (including Yahoo,
Hotmail, and Gmail) are now bouncing emails sent as "#yahoo.com"
addresses that aren't sent through Yahoo servers. [Yahoo]
Twitter, Facebook, Linked In, Paypal, AOL, Comcast and others have also adopted this policy. [Venture Beat]
A solution: Change the "From" header to an address at the server from which you are sending the email. This (correctly) indicates that the mail was sent from your server, and not from Yahoo. You can still use a user-submitted address in the "Reply-To" header so that the recipient can reply to the sender.
As a best practice, you should ... be using a domain you control in ... the "From:" header... [For example,] the site visitor's name is shown in the descriptive part of the "From:" header, and the "Reply-To:" header is set to the website visitor's address, but the actual address used in the "From:" header clearly indicates that your website is the origin of the message. [DMARC]
I'm tring to send an email via php
Here is the code:
<?php
//define the receiver of the email
$to = 'xxx#example.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. Each line should be separated with \n
$message = "Hello World!\n\nThis is my first mail.";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: xxx#example.com";
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
But the message is not received in my email.
It is not a good idea to suppress error messages using #, you need to see these errors.
If you are testing this locally you need a mail server to be running. I use Test Mail Server Tool, it's dead simple.
Also note that mail() returning true does not indicate that the mail was successfully sent, only that execution of the command didn't generate an error:
Returns TRUE if the mail was successfully accepted for delivery, FALSE
otherwise.
It is important to note that just because the mail was accepted for
delivery, it does NOT mean the mail will actually reach the intended
destination.
Are you executing this script on your local machine, or on a webserver with SMTP availability? If it's on your own computer, you may not have a mail server it can use.
Consider using PHPMailer instead, it is far more capable than the native mail() function, and includes integrated SMTP support: https://github.com/PHPMailer/PHPMailer
This is my working code:
mail($address, $subject, $message, "From: Service <service#service.net>\n".
"MIME-Version: 1.0\n".
"Content-type: text/plain; charset=\"UTF-8\"\n");
Maybe the the correct carrige return in the headers is important
Hi guys i am using the mail() from the contact form and for some reason it is not working.
The php coding i have setup is as follows:
// sending email to sales#xxx.com
$to = "hello#xxx.com";
$subject = 'Email From {$name} from your website';
$message = "$name has contacted you from the website and says:
$mcontent
$name 's contact email address is: $email";
$headers = $email . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
mail($to, $subject, $message, $headers) or die ("Failure");
// end of sending mail
returnheader("/xxx/email-sent");
i get no errors at all, it even goes to the success page when completed.
Any ideas why this would not work.
I have checked your code and it is working fine on my server , i am getting e-mail.
Here , it looks like some problem with your SMTP server settings.
There is nothing wrong with your PHP script.
You can find your solution here.
php.ini & SMTP= - how do you pass username & password
Also in windows environment ,
http://www.ruhanirabin.com/php-sendmail-setup-with-smtp-iis-and-windows-servers/
If you are on WIndows, make sure you have an SMTP server in your
php.ini
If you are una Unix, make sure the MTA is running: If it is (at least partly) installed but not runnng, you will get exactly this effect
Edit
If your MTA is not running, and you start it, the mails sent with PHP will go out! They have beein queued, but not processed.
This is probably related to the mail setup. Do you have a mailserver running on the machine? Check sendmail / smtp_server settings in php.ini.
mail() uses the sendmail, by default: sendmail -t -i
It returns TRUE if the mail has been accepted, not if it has been sent:
Return Values
Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
I would suggest using http://swiftmailer.org/ with SMTP rather than mail().
The first line of $headers is invalid if $email is just an email address. Presumably you would want something like:
$headers = "Cc: " . $email . "\r\n";
or
$headers = "From: " . $email . "\r\n";
The cause can be for example, you don't have setup a mail server, or configuration of firewall if you has it.
You should use double quotes instead of single quotes in the variable $subject
$subject = "Email From {$name} from your website";
Send email using mail function working fine. but my question is how to auto save the mail in sent items folder when mail sent
i m using own smtp server and port no is 25
is any config needed on php ini file or i may use imap
my code is as follows
<?php
function send_email($from, $to, $subject, $message){
$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "Return-Path: ".$from."\r\n";
$headers .= "Content-type: text/html\r\n";
if (mail($to,$subject,$message,$headers) ) {
echo "email sent";
} else {
echo "email couldn't be sent";
}
}
$subject = "Helloooo!";
$message .= "<html><body>";
$message .= "<b>Hey! How are you today?</b>";
$message .= "<br>Regards";
$message .= "</body></html>";
send_email("frm addr", "MYEMAILADDRESS#gmail.com",
$subject ,
$message);
?>
"frm addr" is my own web address which is provided by my mail server
how to store or automatically save the mails to my sent item folder whenever i send a mail using the above function
with thanks in adv
R.Saravanakumar
You have to understand the mail() function works on a remote server. The function you send the E-Mail with does not know the concept of a "Sent mail" folder.
That said, it would probably be possible to have the mail server place a copy of the message in the "sent" folder of an IMAP mailbox running on the same server. That includes a lot of hassle, though, and requires root access to the server.
A much easier solution is having the mail() function send you a copy of every sent mail, and to set up an Outlook rule to copy those mails into your "sent" folder.
For that, you could for example add a specific string (like "mail sent from form 1234567", some random number that is unlikely to be repeated in a normal mail) to the subject when sending your copy. Your Outlook rule would then look out for mails containing that subject, and move them into the "Sent" folder (or any other folder, for that matter).
Storing a sent mail in a "sent item"-folder is done by the client which sends out emails, not the mailserver (which is used by mail()).
The PHP-mail()-function will "just" send out email, communicating with the server, without storing it anywhere, so to have your send messages stored anywhere you'll have to create your own "mail-client" which handles mails and folders. I doubt that's worth the effort, so why not just send your mails to a second mail-account and store them there?