# PHP mail() setup



## c00kie (May 5, 2013)

Hi,

I'm trying to configure my FAMP server to send emails through PHP scripts using PHP's mail() function. I have Sendmail set up and working with it successfully sending/recieving emails from the shell using mail().

Taking a closer look at my PHP configuration (php.ini):

```
sendmail_from	no value	no value
sendmail_path	/usr/sbin/sendmail	/usr/sbin/sendmail
serialize_precision	17	17
short_open_tag	Off	Off
SMTP 	localhost	localhost
smtp_port	**	**
```
I try sending an email from 1cookie@myfreebsddomain.org to myaccount@yahoo.com using PHP's mail() method only the mail doesn't find its way there. It doesn't bounce, so I'm assuming it gets lost in transition somewhere.

Could it be my firewall blocking my SMTP port, or is this irrelevant?

Does anyone have knowledge of setting up PHP's mail() function to work with Sendmail?


----------



## Savagedlight (May 5, 2013)

Do check /var/log/maillog for related messages. It might be a good idea to `tail -f [FILE]/var/log/maillog[/FILE]` while trying to send mail via PHP.


----------



## J65nko (May 5, 2013)

What does /var/log/maillog say about that particular email that you tried to send to your Yahoo account? You can easily monitor that file if you use `#  tail -f /var/log/maillog` in an extra xterm.

If the mail is undeliverable for some reason, or rejected by Yahoo, you should read the mails that you can find in /var/mail/root.


----------



## ShelLuser (May 5, 2013)

c00kie said:
			
		

> I have Sendmail set up and working with it successfully sending/recieving emails from the shell using mail().


I'm confused here: are you using the mail(1) command or using a commandline PHP script which uses the mail() _function_? I'll assume the mail command, but its a bit confusing.


```
sendmail_from	no value	no value
sendmail_path	/usr/sbin/sendmail	/usr/sbin/sendmail
serialize_precision	17	17
short_open_tag	Off	Off
SMTP 	localhost	localhost
smtp_port	**	**
```
I'm also quite puzzled by all this. What's up with the double values? You've also added information which isn't relevant to the problem at hand; the short_open_tag is only meaningful for determining how you can set up your PHP code blocks. The reason I'm mentioning this is because there's a big difference in providing specific and relevant information, or simply "dumping" whatever you have in here.

It seems you changed some options manually yourself, I'd recommend keeping the sendmail_path to its default value which includes the -t -i command line parameters.

Needless to say but you'd really want to specify the right SMTP port (25) as well. Normally that's 25, it should obviously be different if you setup something else. Either way it should not be an asterisk. At best it should be commented out if you're not sure what value to use.

Apart from all this you might also want to look into the mail.add_x_header option, check out this web page, it lists all the relevant options for handling SMTP using PHP.


----------



## c00kie (May 6, 2013)

Savagedlight said:
			
		

> Do check /var/log/maillog for related messages. It might be a good idea to `tail -f [FILE]/var/log/maillog[/FILE]` while trying to send mail via PHP.



Hi,

I resend the message from my FreeBSD machine (PHP script) and tail: 
`tail -f /var/log/maillog` reveals:

```
May  6 02:47:39 web sendmail[12064]: r466ld6N012064: from=www, size=670, class=0, nrcpts=0, msgid=<201305060647.r466ld6N012064@web.myfreebsddomain.org>, relay=www@localhost
```
Do any of these parameters look faulty?


----------



## c00kie (May 6, 2013)

ShelLuser said:
			
		

> I'm confused here: are you using the mail(1) command or using a commandline PHP script which uses the mail() _function_? I'll assume the mail command, but its a bit confusing.



I meant the mail(1) command in this context.




> ```
> sendmail_from	no value	no value
> sendmail_path	/usr/sbin/sendmail	/usr/sbin/sendmail
> serialize_precision	17	17
> ...



The smtp_port is set to 25; that was me being overly paranoid regarding port numbers and yes the short_open_tag has nothing to do with the problem at hand.




> It seems you changed some options manually yourself, I'd recommend keeping the sendmail_path to its default value which includes the -t -i command line parameters.



I'm going to try using the sendmail_path with -t -i flags and let you know if this makes a difference.





> Apart from all this you might also want to look into the mail.add_x_header option, check out this web page, it lists all the relevant options for handling SMTP using PHP.




I'll certainly look into that.


----------



## kpa (May 6, 2013)

The relay used should be a plain hostname, localhost instead of www@localhost.


----------



## Savagedlight (May 6, 2013)

Is the from address using a valid domain? Is your server sending mail via an approved e-mail server for the domain? Cross-reference with SPF records for the domain.


----------



## c00kie (May 6, 2013)

kpa said:
			
		

> The relay used should be a plain hostname, localhost instead of www@localhost.



Hmm...

Where do I find/edit the relay parameter?


----------



## c00kie (May 6, 2013)

Savagedlight said:
			
		

> Is the from address using a valid domain?  Cross-reference with SPF records for the domain.



I'm not too sure about this. For example I have a user 1cookie on my FreeBSD machine. I send an email from the shell to myaccount@yahoo.com. The email get's there fine but the from address reads:


```
from: 1cookie@host.myfreebsddomain.org
```
 where host and myfreebsddomain are ficticional.

My point is, it's not of the form: user@domain.org It appears that emails sent from my FreeBSD machine are using my FQDN after the @ symbol?



> Is your server sending mail via an approved e-mail server for the domain?



I'm using Sendmail as my MTA and mail(1) from the shell.


----------



## fonz (May 6, 2013)

c00kie said:
			
		

> The email gets there fine but the from address reads:
> 
> ```
> from: 1cookie@host.myfreebsddomain.org
> ...


In that case you'll want to set up _masquerading_. For example, put something like the following in the .mc file of the mailhost that does the actual sending:

```
MASQUERADE_AS(`yourdomain.org')
FEATURE(masquerade_envelope)
FEATURE(allmasquerade)
```

Edit: this is not a PHP solution, it's a Sendmail solution. So, be sure to run `# make install` in /etc/mail and restart Sendmail after editing your .mc file.


----------



## ShelLuser (May 6, 2013)

c00kie said:
			
		

> I'm not too sure about this. For example I have a user 1cookie on my FreeBSD machine. I send an email from the shell to myaccount@yahoo.com. The email get's there fine but the from address reads:
> 
> 
> ```
> ...


You can either follow Fonz his example, but personally I wouldn't worry about it. This is normally something you'd settle in your mail user agent (or "mail program"). With PHP you can resort to using sendmail_from which will define the 'From' address which will be used.

The 'relay' above looks indeed quite odd to me, but I can't comment on it since I haven't been using Sendmail for a long time.


----------



## c00kie (May 6, 2013)

Thanks for the tips, but to get back on topic, where would I alter how my my mail is relayed? Do I edit hostname.mc? Can you provide some examples? Is this the solution? PHP mail() should work out of the box no?


----------



## c00kie (May 7, 2013)

Editing php.ini with 
	
	



```
sendmail_path	/usr/sbin/sendmail -t	/usr/sbin/sendmail -t
```
 solved it. The -t flag was missing.


----------



## c00kie (May 7, 2013)

ShelLuser said:
			
		

> I'd recommend keeping the sendmail_path to its default value which includes the -t -i command line parameters.



Yep, that'll be it!


----------

