1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP mail() setup

Discussion in 'Web and Network Services' started by c00kie, May 5, 2013.

  1. c00kie

    c00kie New Member

    Messages:
    163
    Thanks Received:
    0
    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):
    Code:
    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?
     
  2. Savagedlight

    Savagedlight Member

    Messages:
    360
    Thanks Received:
    76
    Do check /var/log/maillog for related messages. It might be a good idea to tail -f /var/log/maillog while trying to send mail via PHP.
     
  3. J65nko

    J65nko Member

    Messages:
    443
    Thanks Received:
    119
    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.
     
  4. ShelLuser

    ShelLuser Active Member

    Messages:
    1,068
    Thanks Received:
    268
    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.

    Code:
    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 thanks for this.
  5. c00kie

    c00kie New Member

    Messages:
    163
    Thanks Received:
    0
    Hi,

    I resend the message from my FreeBSD machine (PHP script) and tail:
    tail -f /var/log/maillog reveals:
    Code:
    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?
     
  6. c00kie

    c00kie New Member

    Messages:
    163
    Thanks Received:
    0
    I meant the mail(1) command in this context.


    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.


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




    I'll certainly look into that.
     
  7. kpa

    kpa Well-Known Member

    Messages:
    4,109
    Thanks Received:
    812
    The relay used should be a plain hostname, localhost instead of www@localhost.
     
  8. Savagedlight

    Savagedlight Member

    Messages:
    360
    Thanks Received:
    76
    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.
     
  9. c00kie

    c00kie New Member

    Messages:
    163
    Thanks Received:
    0
    Hmm...

    Where do I find/edit the relay parameter?
     
  10. c00kie

    c00kie New Member

    Messages:
    163
    Thanks Received:
    0
    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:

    Code:
    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?

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

    fonz Active Member

    Messages:
    2,564
    Thanks Received:
    355
    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:
    Code:
    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.
     
  12. ShelLuser

    ShelLuser Active Member

    Messages:
    1,068
    Thanks Received:
    268
    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.
     
  13. c00kie

    c00kie New Member

    Messages:
    163
    Thanks Received:
    0
    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?
     
  14. c00kie

    c00kie New Member

    Messages:
    163
    Thanks Received:
    0
    Editing php.ini with
    Code:
    sendmail_path	/usr/sbin/sendmail -t	/usr/sbin/sendmail -t
    
    solved it. The -t flag was missing.
     
  15. c00kie

    c00kie New Member

    Messages:
    163
    Thanks Received:
    0
    Yep, that'll be it!