php mail() sendmail, masquerade as any domain? - php

I'm running a lamp server on which i'm using the php mail() function to send mail from the current domain (using $username#$_SERVER['HTTP_HOST'] as the source address). I'm using many domains on the same server, and I need the email to come from the correct domain.
Right now I'm using masquerade for a single domain:
dnl # Masquerading options
FEATURE(`always_add_domain')dnl
MASQUERADE_AS(`srv.domain.org')dnl
FEATURE(`allmasquerade')dnl
FEATURE(`masquerade_envelope')dnl
This works fine for this domain, but no other.
How can I set the from-field in php to whatever I want? Right now all email are sent from the same domain, no matter what domain i set in php.
EDIT:
All domain names are CNAME records, pointing to one A record, which points to the server. Every email is using that A record domain name.

You could set the from address in the headers, however, beware that a lot of email clients will mark the emails as spam if the from address and the server domain don't match up.
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com'; // other headers
mail($to, $subject, $message, $headers);
As shown in the PHPDocs for mail()

Gave up, installed postfix, configured it to use a smtp relay, and everything works as expected.
5 minutes, "plug'n'play".

Related

PHP email issue only working within my domain

I have a PHP code hosted on godaddy linux shared hosting
My script is sending email to my domain but not external domain.
I can send an email from hotmail to my domain but cannot send email from my domain to hotmail.
My domain is not listed as spam
Here is the PHP code I am using and working when sending to my domain.
headers = "From: webmaster#mydomain.com \n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
mail($email, $subject, $txt, $headers)
So why is it not getting delivered outside my domain?
I am able to send an email using the script but only on my own adress. me#myserver.com. When I receive it the from adress is wrong. Its not the one i have included in the headers
I'm guessing that your not authenticated your emails before sending (using SMTP). This is commonly blocked by mail servers as it cannot verify where the actual email is coming from. Try authenticating your emails using your SMTP credentials through your Godaddy server and see if that fixes it.
From their documentation,
Two non-CGI form mailers are included in Linux shared hosting account
default files: webformmailer.php and gdform.php. They reside in the
root directory of your hosting account. Incorporating either of these
scripts into your website creates a form to capture user information
and email it to a specified address
SPECIFYING AN EMAIL ADDRESS FOR THE PHP FORM MAILER
To use their PHP form-mailer, you must specify an email address where you want the contents of your form sent to.
Form-mailers cannot send from #aol.com, #gmail.com, #hotmail.com,
#msn.com, or #yahoo.com addresses.
To Specify the Email Address for the Form-Mailer
Log in to your GoDaddy account.
Click Web Hosting.
Next to the hosting account you want to use, click Manage.
In the Tools section of the Hosting Control Panel, click the Form
Mail icon.
Under the Forms Email Address section, type the email address you
want to use with your form-mailer.
Click Continue.
Verify that this is the address you want to use with your
form-mailer, and click Update.
For more information, Please read this page
UPDATE::
The best thing to do is check what PEAR extensions GoDaddy have on there hosting, if they have PEAR mail, you can use that. If not, then you should try using authenticated mail sending.
<?php
include('Mail.php');
$recipients = 'joe#example.com';
$headers['From'] = 'richard#example.com';
$headers['To'] = 'joe#example.com';
$headers['Subject'] = 'Test message';
$body = 'Test message';
$params['sendmail_path'] = '/usr/lib/sendmail';
// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory('sendmail', $params);
$mail_object->send($recipients, $headers, $body);
?>
Mail::send() – sends a mail

How does Mail PHP work?

I stumbled on the following script today for sending an e-mail using PHPMail.
<?php
$to = "some_address#domain.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "my_address#domain.com";
$headers = "From:" . $from;
mail($to, $subject, $message, $headers);
echo "Mail Sent.";
?>
Above can be runnable through php mail.php and instantly you'll get an e-mail sent to $to from $from despite not needing to set outgoing/ingoing servers out.
It really intrigued me, since my CMS uses an SMTP outgoing server (well, same way Mail PHP does), which I need to set up with my Outlook SMTP username and password - some sort of verification.
However, about Mail PHP just.. sends an e-mail. To the address you set it as. From the address you set it as.
Looking at PHP docs it does not really reveal how it works. Does Mail PHP not have any issues with spamming since anyone can send anyone anything anytime programmatically without verification of the from identity?
EDIT:
It's rather funny the people in the comments were talking about the POTUS, since I had the exact thing in mind:
It did land in my junk folder, but I'm sure it isn't hard to make this look convincing enough and still be considered "oh damn spam filter lost my e-mail!"
The mail function uses the settings from php.ini. The details of this configuration can be found in Mail Runtime Configuration.
The defaults can be set in php.ini, although you can override them using ini_set.
I bet you sent the mail from a PHP script on a hosted server. That server probably has SMTP settings configured beforehand. If you would try this locally on a WAMP/LAMP server, you would have to do this configuration yourself, since PHP cannot read your Outlook/WhateverMailclient settings.
As stated in the comments, you can specify the sender/from address yourself. SMTP doesn't require this to be the actual sender domain, so that's why this works. The missing link is the pre-configured SMTP server of your host.
Some relay servers do check for this, and your mail might be blocked or sent to a junk mail folder. You can however configure this in your DNS to indicate that <Your server's IP> is indeed allowed to send email for <yourdomain>. For more information about that subject, you might want to read this question on ServerFault.
It uses the smtp protocol or send_mail, you can even configure what php should use to send mails in php.ini. It can send e-mail but the e-mail will end-up in your spam filter take a look to DKIM and SPF records for more information

PHP mail is not sending from mydomain.com to the google apps email myname#mydomain.com

When i am trying to send a mail from a contact form in my website using the
PHP mail function its not sending from mydomain.com to the google apps email myname#mydomain.com.
Please suggest a solution for the issue.
After a very long research, I found the answer to this problem.
For me, the problem is in my host settings.
You host treats those domain addresses as they were registered with itself not with Google Apps. So it routes the emails to hosts mail server.
To route it through Google Apps (Gmail), you need to change the email routing settings.
Find "MX Entry" settings in your cpanel and add a new MX Record with priority
0
and destination
gmail.com
and add it. You should also change the existing MX Record priority to 1.
These settings will let your receive emails from php mail() to your Google Apps gmail account.
To send email from PHP you must have an SMTP server installed and have PHP configured to use the server. If you are using shared hosting or have it set up, you should check that you are calling the mail function correctly by checking the documentation.
Here is an example of how it is used:
$to = "name#example.com";
$subject = "Test Message";
$message = "Hello!";
$headers = "From: me#example.org";
mail($to, $subject, $message, $headers);
Just modify the /etc/hosts file and add your IP Address mydomain.com mydomain in the next line, this should work by routing emails to google's servers .I did the same for my site.
So my /etc/hosts file looks like this :
127.0.0.1 localhost
IP mysite.com mysite(this is an alias and you can call it anything i suppose)

BATV; What is it and how is it configured?

I have tested my email server on allaboutspam.com to see why the emails are beeing considered spam by hotmail and gmail servers.
The results was amongst other faults, the BATV.
This is the complete result from allaboutspam.com on my BATV:
BATV is a mechanism wherein an outgoing Email server adds a tag to the Envelope From address of all outgoing Emails. For example, if an Email address goes out with From address as <info#allaboutspam.com>, the Envelope From is changed to <prvs=SBDGAUJ=info#allaboutspam.com>, where 'SBDGAUJ' is the added tag. This tag is generated using an internal mechanism and is different for each email sent.
If any bounce is received by the Incoming email servers, they are checked to see if the Bounce address has the proper tag (in above case 'SBDGAUJ'). If not, the email is rejected.
Could somebody explain this in simpler words... How is it configured?
currently I have this setup when sending email with php:
$mail_message="text_text_text_text";
$headers="MIME-Version: 1.0"."\n";
$headers.="Content-type: text/plain; charset=UTF-8"."\n";
$headers.="From: Skuffen <no-reply#domain.se>"."\n";
$subject="SUBJECT HERE";
mail($email, '=?UTF-8?B?'.base64_encode($subject).'?=', $mail_message, $headers, '-fno-reply#domain.se');
This is a swedish language so you know (utf-8)...
Thanks
I think that you need to set this up in your Mail Transfer Agent. The PHP mail() command sends mail through the local sendmail (or compatible, like Exim, Postfix or Qmail) installation. That's where BATV needs to be configured.
If you are on a simple shared hosting, contact your hosting provider.

why is php mail script not sending mail

<?php
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail("example#gmail.com", "hello", "nothing",$headers);
echo "mail sent";
?>
but it does not actually send the mail please help me out with this
The mail function is just an interface to the local mail server. The mail functionality in PHP relies on the machine PHP is running on to be correctly configured and able to dispatch e-mail. Check the mail system configuration on the machine.
check the following -
check if your smtp server is running (you could either check through command line tools or try ftp'ing to port 25).
If your smtp server is running. Then try to send a mail manually (without script). Use the command-line mail command (I am assuming you have unix here).
Also, when you run your script, what happens? It could be possible that your mail is in a Queue. From your terminal type 'mailq'. This shows the current emails in Queue & why they are there. Also there is a corresponding log to this. You could also check that out for info.
My guess is if all the above are running, you are good to go.
please check following ** Please verify your servers sender domain policy**
Emails sent through your servers (mail servers and shared web servers) should use a from address that is hosted here at your server. Emails that are sent with a from address hosted somewhere else (like Hotmail or Google) may be blocked.
ie use
$header = "From:example#/*yourhostname.domain name*/ \r\n";

Categories