I am trying to invoke sendmail via PHP's mail function by the following code:
$to = 'blah#email.state.edu';
$subject = 'test';
$message = 'test';
$headers = 'From: mail#smartrek.blah.me' . "\r\n" .
'Reply-To: mail#smartrek.blah.me' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
However in my mail.log I am getting a message that the from is not the address I specified in the header:
<www-data#Name>: Sender address rejected: Domain not found
Why is this?? I am running PHP's fast-cgi on ubuntu
Why doesn't sendmail use the header that I have specified via the PHP code?
It looks like www-data#Name is your envelope "from" address. The envelope "from" address is different from the address that appears in your "From:" header of the email. It is what sendmail uses in its "MAIL FROM/RCPT TO" exchange with the receiving mail server.The main reason it is called an "envelope" address is that appears outside of the message header and body, in the raw SMTP exchange between mail servers.
The default envelope "from" address on unix depends on what sendmail implementation you are using. But typically it will be set to the username of the running process followed by "#" and the hostname of the machine. In a typical configuration this will look something like username#example.com.
If your emails are being rejected by receiving mail servers, or if you need to change what address bounce emails are sent to, you can change the envelope "from" address to solve your problems.
To change the envelope "from" address on unix, you specify an "-r" option to your sendmail binary. You can do this globally in php.ini by adding the "-r" option to the "sendmail_path" command line. You can also do it programmatically from within PHP by passing -r mail#smartrek.blah.me as the additional parameter argument to the mail() function (the 5th argument). If you specify an address in both places, the sendmail binary will be called with two "-r" options, which may have undefined behavior depending on your sendmail implementation. With the Postfix MTA, later "-r" options silently override earlier options, making it possible to set a global default and still get sensible behavior when you try to override it locally.
EDIT
About optional flags that can be passed to sendmail: -f will set the From address, -r will override the default Return-path that sendmail generates (typically the From address gets used). If you want your bounce-backs to go to a different address than the from address, try using both flags at once: -f mail#smartrek.blah.me -r bounced-mail#smartrek.blah.me
my php.ini
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me#example.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =
; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =
; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = On
; Log all mail() calls including the full path of the script, line #, to address and headers
;mail.log =
Although this is an old question, I'm adding this answer in case it is of help to someone:
I had the same problem with the From: header being re-written to www-data#host... I eventually tracked it down to the ssmtp bridge service that was piping mail from our web server into our mailserver. I added the line FromLineOverride=YES in the file /etc/ssmtp/ssmtp.conf and the problem disappeared.
In my case, I've got a hosted server so I needed to edit this file :
/etc/ssmtp/ssmtp.conf
Then uncomment this line :
FromLineOverride=YES
Once done, personals headers are working.
I was having similar problem with www-data when all my mails were sent and received with this header:
From: www-data <www-data#example.com>
I used the -f info#example.com flag as 5th argument with the PHP email() function (as mentioned in accepted answer), but i was still receiving my emails as:
From: www-data <info#example.com>
So i added one more flag -f info#example.com -F info to set the full name of the email and finally i was getting emails as i wanted:
From: info <info#example.com>
I'm posting this answer because nobody mentions it here and i got a little stuck on it.
This worked for me:
$mail->Sendmail = $mail->Sendmail.' -f '.$mail_errorsto;
I had this issue using exim4 with smarthost. The mails were sent with
Return-path: <www-data#servername>
which was rejected by the ISP. I needed to change it to at least www-data#example.com (assuming 'example.com' is the servers public domain name). I could achieve that by changing /etc/mailname from
servername
to
example.com
This has already worked for me, having www-data#example.com as Return-path.
However - If you want to completely change the email address, you can configure it in /etc/email-addresses as
www-data: notifications#example.com
After that the emails have been sent by default with
Return-path: <notifications#example.com>
Related
I'm trying to send emails from my localhost, but I'm running across an issue that keeps coming up. I found this answer which explained how to set up XAMPP to send email, and the emails are sent, but when I try changing the From header, nothing happens. The email gets sent, but it's sent from my personal email.
I tried removing both the sendmail_from in php.ini and force_sender in sendmail.ini, but neither worked. I tried adding the -f parameter to the mail function, and it didn't work. I've even tried restarting XAMPP several times, but still nothing. Is there something I've overlooked or is there no way to do this?
PHP
<?php
$headers = "From: Joe Smith <joe#joesmith.com>" . "\r\n";
mail("recipient#gmail.com", "This is a test message", "Yup, it's a test message all right.", $headers, "-f joe#joesmith.com");
?>
php.ini
[mail function]
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from =
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
sendmail.ini
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=mypersonalemail#gmail.com
auth_password=mypersonalpassword
It is because you are using Google's SMTP servers and they don't allow changing the 'From' header.
This was answered here:
How to change from-address when using gmail smtp server
I just did a project similar to this. Depending on your needs, you can use PHPMailer to change your "reply to" address. Although it's not the same as changing the from address, it achieves a similar goal, is easy to use, and has a good community. Also, it works perfectly with Google Mail.
I am new to PHP. I am running XAMPP on Windows 7 PC. I am attempting to create an e-mail contact form using the PHP mail() function. I am uncertain how the configuration of my sendmail.ini and php.ini files affects my code. Does the configuration of the files only affect how the mail function will work on my localhost? or does it affect how the code will run when I upload my content to my web server?
From my php.ini:
[mail function]
; For Win32 only.
SMTP = smtp.gmail.com
smtp_port = 995
; For Win32 only.
sendmail_from = myemail#gmail.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = "\"\xampp\sendmail\sendmail.exe\" -t"
; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =
; Add X-PHP-Originating-Script: that will include uid of the script followed by the
filename
;mail.add_x_header = Off
; Log all mail() calls including the full path of the script, line #, to address and headers
;mail.log = "\xampp\php\logs\php_mail.log"
From my sendmail.ini:
smtp_server=smtp.gmail.com
; smtp port (normally 25)
smtp_port=995
; SMTPS (SSL) support
; auto = use SSL for port 465, otherwise try to use TLS
; ssl = alway use SSL
; tls = always use TLS
; none = never try to use SSL
;smtp_ssl=auto
; the default domain for this server will be read from the registry
; this will be appended to email addresses when one isn't provided
; if you want to override the value in the registry, uncomment and modify
;default_domain=mydomain.com
; log smtp errors to error.log (defaults to same directory as sendmail.exe)
; uncomment to enable logging
error_logfile=error.log
; create debug log as debug.log (defaults to same directory as sendmail.exe)
; uncomment to enable debugging
debug_logfile=debug.log
; if your smtp server requires authentication, modify the following two lines
auth_username=myemail#gmail.com
auth_password=mypassword
; if your smtp server uses pop3 before smtp authentication, modify the
; following three lines. do not enable unless it is required.
;pop3_server=
;pop3_username=
;pop3_password=
; force the sender to always be the following email address
; this will only affect the "MAIL FROM" command, it won't modify
; the "From: " header of the message content
force_sender=myemail#gmail.com
; force the sender to always be the following email address
; this will only affect the "RCTP TO" command, it won't modify
; the "To: " header of the message content
;force_recipient=
My code looks like:
$name = $_POST['name1'];
$email = $_POST['email'];
$message = $_POST['body'];
$body = $message . '\n My email is ' . $email;
$header1 = "From: $email \n";
mail("myemail#gmail.com", $_POST['subject'], $body, $header1);
header('location:Contact.php');
In any case I have been getting two errors: one is that the "connection timed out" and the other is that the "connection closed gracefully.' I googled "connection closed gracefully" and found that I had to run my sendmail.exe file as an administrator and since I am using gmail I had to set up application-specific passwords.
On the post it indicated that I needed to run a server (or IDE) as an administrator as well. Anyone familiar with XAMPP who knows what/where the server/IDE is please respond.
Making sure that php and sendmail are run as administrator seems a good idea but this did not solved my problem.
Solution in sendmail.ini
smtp_server = smtp.yourisp.com
; smtp port (normally 25)
smtp_port=25
; SMTPS (SSL) support
; auto = use SSL for port 465, otherwise try to use TLS
; ssl = alway use SSL
; tls = always use TLS
; none = never try to use SSL
smtp_ssl=none
do not use smtp_ssl = auto that was what was causing my bug just set it to ssl tls or none according to specification of your server
Try these and I'll be back when I have more time to update this:
1) Never install XAMPP in your Programs folder in Windows. This will cause privilege issues. If you have then re-install it elsewhere.
2) All settings in your sendmail.ini must be configured with what your Apache (Xampp) installation has. The php.ini part you provided is not the full file or the part that has these settings. For example I don;t think your SMTP port is really 995 (I could be wrong).
3) Look at this post: How to configure XAMPP to send mail from localhost?
4) SMTP should work now. Your using another mail server (Gmail in this case) to send off your mail. You actually could use your computer as the server too but that is a whole nether issue.
I am trying to send an email using mail function in php. I have read the documentation and followed the examples but in vain. I don't know if I am doing anything wrong.
Can anyone please help me. I also followed other examples from the community without success.
And here also;
Send email with PHP from html form on submit with the same script
Here is my code:
$name =array($_POST['name'],$_POST['email'],$_POST['phone'],$_POST['comments']);
$to = "fasdjgasgd#yahoo.com";
$subject = "Form submission";
$message = "$name[0] wrote the following: <br/> $name[3]";
$header = "FROM:".$name[1];
mail($to, $subject, $message, $header);
Your server does not have local mailserver.
There are few solutions:
Install local mail server if you have sufficient rights
Change your PHP settings to use other mail server (other open mailserver or auth-based ones like Gmail, Yahoo etc)
Use one of available mail libraries which supports IMAP / POP3 to handle mail sending.
SwiftMailer or Pear Mail are one of most commonly used.
PHP must be configured correctly in the php.ini file with the details of how your system sends email. Open php.ini file available in /etc/ directory and find the section headed [mail function].
Windows users should ensure that two directives are supplied. The first is called SMTP that defines your email server address. The second is called sendmail_from which defines your own email address.
The configuration for Windows should look something like this:
[mail function]
SMTP = localhost
sendmail_from = email#domain.com
This link will help you.
This my problem,
I have a simple PHP script to send email to localhost with mercury mail server and read it with Mozilla Thunderbird. And it's work.
And then, I upgarde XAMPP version to 1.8.1 and my function return true but no email delivered.
<?php
$to = "root#localhost.com";
$subject = "Hi!";
$body="test";
$headers = "From: root#localhost.com";
if (mail($to, $subject, $body, $headers)) {
echo "Message successfully sent!";
} else {
echo "Message delivery failed...";
}
?>
I tried to send email with Thunderbird to root#localhost.com and it work.
So iI try to change mail function in php.ini to this but not work
[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
SMTP = localhost
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = root#localhost.com
; XAMPP IMPORTANT NOTE (1): If XAMPP is installed in a base directory with spaces (e.g. c:\program filesC:\xampp) fakemail and mailtodisk do not work correctly.
; XAMPP IMPORTANT NOTE (2): In this case please copy the sendmail or mailtodisk folder in your root folder (e.g. C:\sendmail) and use this for sendmail_path.
; XAMPP: Comment out this if you want to work with fakemail for forwarding to your mailbox (sendmail.exe in the sendmail folder)
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
; XAMPP: Comment out this if you want to work with mailToDisk, It writes all mails in the C:\xampp\mailoutput folder
sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"
; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =
; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = Off
; Log all mail() calls including the full path of the script, line #, to address and headers
;mail.log = "C:\xampp\php\logs\php_mail.log"
I try to change sendmail.ini to this but not work
; configuration for fake sendmail
; if this file doesn't exist, sendmail.exe will look for the settings in
; the registry, under HKLM\Software\Sendmail
[sendmail]
; you must change mail.mydomain.com to your smtp server,
; or to IIS's "pickup" directory. (generally C:\Inetpub\mailroot\Pickup)
; emails delivered via IIS's pickup directory cause sendmail to
; run quicker, but you won't get error messages back to the calling
; application.
smtp_server=localhost
; smtp port (normally 25)
smtp_port=25
; SMTPS (SSL) support
; auto = use SSL for port 465, otherwise try to use TLS
; ssl = alway use SSL
; tls = always use TLS
; none = never try to use SSL
smtp_ssl=auto
; the default domain for this server will be read from the registry
; this will be appended to email addresses when one isn't provided
; if you want to override the value in the registry, uncomment and modify
;default_domain=mydomain.com
; log smtp errors to error.log (defaults to same directory as sendmail.exe)
; uncomment to enable logging
error_logfile=error.log
; create debug log as debug.log (defaults to same directory as sendmail.exe)
; uncomment to enable debugging
;debug_logfile=debug.log
; if your smtp server requires authentication, modify the following two lines
auth_username=
auth_password=
; if your smtp server uses pop3 before smtp authentication, modify the
; following three lines. do not enable unless it is required.
pop3_server=
pop3_username=
pop3_password=
; force the sender to always be the following email address
; this will only affect the "MAIL FROM" command, it won't modify
; the "From: " header of the message content
force_sender=
; force the sender to always be the following email address
; this will only affect the "RCTP TO" command, it won't modify
; the "To: " header of the message content
force_recipient=
; sendmail will use your hostname and your default_domain in the ehlo/helo
; smtp greeting. you can manually set the ehlo/helo name if required
hostname=
Any idea?
If mail returns true, then php's job is done. it's handed the email over to your outgoing mail server. You need to check that server's logs to see what happens from that point forward.
e.g. PHP's role in sending an email is basically the equivalent of taking an envelope you hand it, walking down to the street corner, and dropping it into the mailbox. After that, PHP's done and it can report success.
If that mailbox gets nailed by a drunk driver and the mail gets destroyed, that's not PHP's fault. If the mail gets picked up and sent to a postal sorting facility and then gets lost, that's also not PHP's problem.
So go check your mail server's logs. If it can't deliver the mail, it'll report why. If it DID deliver the mail to the destination server, it'll report so, and then you need to find out why the receiving server isn't doing its job - maybe your email is getting flagged as spam and dump in the trash.
Check this directory: C:\xampp\apache\mailoutput
I recently experienced this problem and after many frustrating hours figured out what was causing it, and how to fix it. Hopefully I can save someone else all that trouble.
My php mail() function was returning true, but I wasn't receiving the messages. In my case, it turned out to have nothing to do with PHP after all.
I use Plesk to manage the sites on my server. This particular version of Plesk doesn't allow me to turn mail services on or off for a given website/domain. I host my email elsewhere.
So here's what was happening: PHP mail() was sending messages, the way it was supposed to. But my server was under the impression that it was supposed to receive messages for the particular domain those messages were addressed to. But there was no such mailbox for that domain, because again I host my email elsewhere.
So the messages were never leaving my server.
The solution was for me to go into var/qmail/control and remove the domain names from the "virtualdomains" file, then restart qmail.
Again, I hope this can save someone else all the time and frustration.
Gregg
Case Solved
I downgrade XAMPP version to 1.7 and it works :)
I had a similar problem. In my case was that I modified the "From" field in the headers. In your example you have
$headers = "From: root#localhost.com";
Maybe this is the problem. See this answer for more information
While troubleshooting a contact form with an e-mail host they told me to use '-f' in the from address of the php mail function. What does the "-f" flag do and why would that be a fix for allowing an e-mail to be delivered? I read some of the documentation but I'm not quite clear on it.
Example code:
mail($emailAddress, $mailSubject, $mailBody, $headers, '-f ' . $mailFrom);
PS: without the "-f" it works just fine for the big e-mail hosts (hotmail, gmail, etc, but for whatever reason not for the smaller host I'm working with)
Thanks
-f is a parameter to the mailer (usually sendmail). From the docs:
The additional_parameters parameter can be used to pass additional
flags as command line options to the program configured to be used
when sending mail, as defined by the sendmail_path configuration
setting. For example, this can be used to set the envelope sender
address when using sendmail with the -f sendmail option.
Here is the man page for sendmail, you can see what the -f option does:
-fname Sets the name of the ``from'' person (i.e., the sender of the
mail). -f can only be used by ``trusted'' users (normally
root, daemon, and network) or if the person you are trying to
become is the same as the person you are.
The -f option is to set the bounce mail address. Sending a message without one can negatively influence the spam-score that is being calculated over the message. Messages with low scores sometimes get filtered out for certain hosts.
You can use https://www.mail-tester.com/ to test the score of your message. You can expirement with or without the -f flag and see the score change.
It is a flag to mark the following text ($mailFrom) to be used as "from" address of the mail.
Have a look at: http://www.php.net/manual/en/function.mail.php