LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 01-08-2016, 01:14 PM   #1
SignorX
LQ Newbie
 
Registered: Jun 2011
Posts: 20

Rep: Reputation: Disabled
send a mail with php mail() using postfix and nginx


Hi,

I have a webserver on my rpi with nginx. I would like to send emails with the php mail() function. I installed postfix and mailutils.
I have the following script:
Code:
    <?php
    $to      = 'XXX@gmail.com';
    $subject = 'the subject';
    $message = 'hello';
    $from   =  'From: pi@home';

            if (mail($to, $subject, $message, $from)) {
                echo '<h2>Thank you for your email!</h2>';
            } else {
                echo '<h2>Oops! An error occurred. Try sending your message again.</h2>';
            }
    ?>
If I execute it directly:
Code:
    pi@home ~ $ php -f script.php
    <h2>Thank you for your email!</h2>
But if I load the script in a webpage I obtain:
Code:
    <h2>Oops! An error occurred. Try sending your message again.</h2>
In /var/log/mail.log, php -f generates:
Code:
   Jan  7 21:34:11 home postfix/pickup[27836]: A0A65A1E5A: uid=1000 from=<www-data>
    Jan  7 21:34:11 home postfix/cleanup[27867]: A0A65A1E5A: message-id=<20160107203411.A0A65A1E5A@home.YYYY>
    Jan  7 21:34:11 home postfix/qmgr[27837]: A0A65A1E5A: from=<www-data@home.YYYY>, size=332, nrcpt=1 (queue active)
    Jan  7 21:34:12 home postfix/smtp[27869]: A0A65A1E5A: to=<XXX@gmail.com>, relay=gmail-smtp-in.l.google.com[74.125.195.26]:25, delay=1.3, delays=0.07/0.02/0.79/0.44, dsn=2.0.0, status=sent (250 2.0.0 OK 1452198852 y130si13070628wmy.93 - gsmtp)
    Jan  7 21:34:12 home postfix/qmgr[27837]: A0A65A1E5A: removed
but when I load a page:
Code:
   Jan  7 20:40:01 home postfix/pickup[25500]: D50C2A1E54: uid=33 from=<www-data>
    Jan  7 20:40:01 home postfix/cleanup[25681]: D50C2A1E54: message-id=<20160107194001.D50C2A1E54@home.YYYY>
    Jan  7 20:40:01 home postfix/qmgr[25501]: D50C2A1E54: from=<www-data@home.YYYY>, size=601, nrcpt=1 (queue active)
    Jan  7 20:40:02 home postfix/smtpd[25684]: connect from home[ZZZZ]
    Jan  7 20:40:02 home postfix/smtp[25683]: warning: host home.YYYY[ZZZZ]:25 greeted me with my own hostname home.YYYY
    Jan  7 20:40:02 home postfix/smtp[25683]: warning: host home.YYYY[ZZZZ]:25 replied to HELO/EHLO with my own hostname home.YYYY
    Jan  7 20:40:02 home postfix/smtp[25683]: D50C2A1E54: to=<www-data@home.YYYY>, orig_to=<www-data>, relay=home.YYYY[ZZZZ]:25, delay=0.51, delays=0.07/0.04/0.4/0, dsn=5.4.6, status=bounced (mail for home.YYYY loops back to myself)
    Jan  7 20:40:02 home postfix/smtpd[25684]: disconnect from home[ZZZZ]
    Jan  7 20:40:02 home postfix/cleanup[25681]: 5AF1EA1E56: message-id=<20160107194002.5AF1EA1E56@home.YYYY>
    Jan  7 20:40:02 home postfix/bounce[25688]: D50C2A1E54: sender non-delivery notification: 5AF1EA1E56
    Jan  7 20:40:02 home postfix/qmgr[25501]: 5AF1EA1E56: from=<>, size=2524, nrcpt=1 (queue active)
    Jan  7 20:40:02 home postfix/qmgr[25501]: D50C2A1E54: removed
    Jan  7 20:40:02 home postfix/smtpd[25684]: connect from home[ZZZZ]
    Jan  7 20:40:02 home postfix/smtp[25683]: warning: host home.YYYY[ZZZZ]:25 greeted me with my own hostname home.YYYY
    Jan  7 20:40:02 home postfix/smtp[25683]: warning: host home.YYYY[ZZZZ]:25 replied to HELO/EHLO with my own hostname home.YYYY
    Jan  7 20:40:02 home postfix/smtp[25683]: 5AF1EA1E56: to=<www-data@home.YYYY>, relay=home.YYYY[ZZZZ]:25, delay=0.18, delays=0.01/0/0.17/0, dsn=5.4.6, status=bounced (mail for home.YYYY loops back to myself)
    Jan  7 20:40:02 home postfix/smtpd[25684]: disconnect from home[ZZZZ]
    Jan  7 20:40:02 home postfix/qmgr[25501]: 5AF1EA1E56: removed
Some of my configuration files:

/etc/hosts:
Code:
    127.0.0.1         localhost
    ::1               localhost ip6-localhost ip6-loopback
    fe00::0         ip6-localnet
    ff00::0         ip6-mcastprefix
    ff02::1         ip6-allnodes
    ff02::2         ip6-allrouters
    ZZZZ      home home.YYYY
/etc/hostname
Code:
home
/etc/mailname
Code:
    home.YYYY
/etc/postfix/main.cf
Code:
    smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
    biff = no

    append_dot_mydomain = no

    readme_directory = no

    # TLS parameters
    smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
    smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
    smtpd_use_tls=yes
    smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
    smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

    myhostname = home.YYYY
    alias_maps = hash:/etc/aliases
    alias_database = hash:/etc/aliases
    myorigin = /etc/mailname
    mydestination = localhost.localdomain, localhost
    relayhost =
    mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
    mailbox_size_limit = 0
    recipient_delimiter = +
    inet_interfaces = all
    inet_protocols = ipv4
/etc/nginx/sites-available/mysite
Code:
    [...]   
    location ~ \.php$ {
            fastcgi_pass  unix:/var/run/php5-fpm.sock;
            fastcgi_index  index.php;
            include       fastcgi_params;
        }
    [...]
I can't figure it out if it is a problem of php, postfix, nginx or networking (hostnames, domains, etc).

Can you help me?
 
Old 01-08-2016, 02:08 PM   #2
ceyx
Member
 
Registered: May 2009
Location: Fort Langley BC
Distribution: Kubuntu,Free BSD,OSX,Windows
Posts: 342

Rep: Reputation: 59
So, this works :
Quote:
Jan 7 21:34:12 home postfix/smtp[27869]: A0A65A1E5A: to=<XXX@gmail.com>, relay=gmail-smtp-in.l.google.com[74.125.195.26]:25, delay=1.3, delays=0.07/0.02/0.79/0.44, dsn=2.0.0, status=sent (250 2.0.0 OK 1452198852 y130si13070628wmy.93 - gsmtp)
This doesn't - from your loaded page
Quote:
Jan 7 20:40:02 home postfix/smtp[25683]: D50C2A1E54: to=<www-data@home.YYYY>, orig_to=<www-data>, relay=home.YYYY[ZZZZ]:25, delay=0.51, delays=0.07/0.04/0.4/0, dsn=5.4.6, status=bounced (mail for home.YYYY loops back to myself)
I'd look at the web page script - it is not sending to the google email address, it is sending it to www-data@home.YYYY
 
  


Reply

Tags
mail, nginx, php, postfix



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to send mail using PHP mail function On apache server kiran.ursal Linux - Server 2 04-29-2013 03:21 AM
Can send mail but Not able to receive mail. Postfixadmin, dovecot, postfix setup RajeshThakur Linux - Newbie 9 04-20-2012 08:31 AM
Postfix - allowing only certain users to send mail to a mail ID rahmathullakm Linux - Server 3 02-07-2009 11:05 AM
Want to send mail with php mail() command - minimum config required? FeathersMc Linux - Server 2 10-26-2008 12:21 AM
Cannot send mail with mail() command in PHP 4.3.4, Apache 2.0.48, Mandrake Linux 10 arcanum Linux - General 3 06-27-2004 06:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 12:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration