ISPConfig3 - Sending emails using PHP and Pear

Discussion in 'Installation/Configuration' started by DKLeader, Oct 14, 2010.

  1. DKLeader

    DKLeader New Member

    First : Not sure if this goes in this part of the forum - sorry if it is misplaced.

    On my multiserver setup (Debian) I am trying to get emails sent from PHP on the sites. Since all normal mail sending from PHP does not work on a multiserver system I decided to try using Pear.

    My PHP file looks like this :
    Code:
    <?php 
       require_once "Mail.php"; 
       require_once 'Mail/mime.php'; 
    
    
       // Get user information entered on form. 
       $Kontakt_FuldtNavn = $_REQUEST['Kontakt_FuldtNavn'] ; 
       $baadnavn = $_REQUEST['baadnavn'] ; 
       $foraar = $_REQUEST['foraar'] ; 
       $efteraar = $_REQUEST['efteraar'] ; 
       $starthavn = $_REQUEST['starthavn'] ; 
       $tidligere = $_REQUEST['tidligere'] ; 
       $senestaar = $_REQUEST['senestaar'] ; 
       $Adresse = $_REQUEST['Adresse'] ; 
       $postnr = $_REQUEST['postnr'] ; 
       $by = $_REQUEST['by'] ; 
       $email = $_REQUEST['email'] ; 
       $dsklub = $_REQUEST['dsklub'] ; 
       $baadtype = $_REQUEST['baadtype'] ; 
       $skrogfarve = $_REQUEST['skrogfarve'] ; 
       $sejlnr = $_REQUEST['sejlnr'] ; 
       $lystal = $_REQUEST['lystal'] ; 
       $lgd = $_REQUEST['lgd'] ; 
       $brd = $_REQUEST['brd'] ; 
       $depl = $_REQUEST['depl'] ; 
    
       // Some info on who is sending. 
       $from = "[email protected]"; 
       $to = "[email protected], $email"; 
       $subject = "24Timer - Tilmelding"; 
        
       // Some info on what smtp we are going to use. 
       $host = "mail.24-timerssejlads.dk"; 
       $username = "[email protected]"; 
       $password = "somethingiknow"; 
       $crlf = "\n"; 
        
    // Message start 
    $html = "<html> 
    <body>HTML TEST MESSAGE</body> 
    </html>"; 
    
    $hdrs = array ('From' => $from, 
    'To' => $to, 
    'Subject' => $subject); 
    
    $mime = new Mail_Mime($crlf); 
    $mime->setHTMLBody($html); 
    print_r($html); 
    
    $body = $mime->get(); 
    $headers = $mime->headers($hdrs); 
    
    $smtp =& Mail::factory('smtp', 
    array ('host' => $host, 
    'auth' => true, 
    'username' => $username, 
    'password' => $password)); 
    
    $mail = $smtp->send($to, $headers, $body); 
        
       if (PEAR::isError($mail)) { 
       echo("<p>" . $mail->getMessage() . "</p>"); 
       } 
    ?>
    
    I have changed the message part to above during testing. I have it running on http://24-timerssejlads.dk/sendtilmeld.php

    First of it seems to work fine - execpt it doesn't send any emails.
    Anuone got a solution or maybe another way to send emails from within PHP using SMTP on another server.
     
  2. Nicram

    Nicram New Member

    What shows Your log files?
    Like /var/log/maillog or /var/logmessages ?
    It should say what is wrong.
     
  3. DKLeader

    DKLeader New Member

    Forgot something in my first part :

    Did check mail.err - no errors since I fixed my problems with mysql.

    Also forgot to mention that the emails are received but they are empty.

    //DKLeader
     
  4. till

    till Super Moderator Staff Member ISPConfig Developer

    Normal sending of mails works fine on multiserver systems. So if it does not work on yours, then there must be a misconfiguration of the email system and this should be fixed instaed of switching to pear.

    So which exact errors do you get in the mail.log when you use the php mail() function?
     
  5. itanium

    itanium New Member

    Do you have a email server on the web server like postfix or sendmail?

    In the multiserver setup, i cannot find any install of a sendmail or postfix for webserver.

    You can also check the php.ini for sendmail_path = sendmail -t -i =
     
  6. DKLeader

    DKLeader New Member

    Ok, did some changes after reading the replies.
    First of postfix was also running on my web server (192.168.1.90 - web.superweb.dk) - must have been from when I ran all as single server trying out ISPConfig3.

    The PHP I have used for testing now looks like this :
    Code:
    <?php
    $reciever = "[email protected]";
    $subject = "Et emnefelt";
    
    $massage = "<h1 style='background-color: #006699'>
                This is a test mail
              </h1>";
    
    $header  = "MIME-Version: 1.0" . "\r\n";
    $header .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
    $header .= "from:[email protected]";
    
    mail($reciever, $subject, $message, $header);
    ?> 
    In the log there was some errors regarding connection to the mysql.
    Changed all mysql-*.cf files to have the same user and pasword as the mail server (192.168.1.60 - mail.superweb.dk)
    Also changed the host in same files to 192.168.1.60

    My main.cf looks like this (after my changes) :
    Code:
    # See /usr/share/postfix/main.cf.dist for a commented, more complete version
    
    
    # Debian specific:  Specifying a file name will cause the first
    # line of that file to be used as the name.  The Debian default
    # is /etc/mailname.
    #myorigin = /etc/mailname
    
    smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
    biff = no
    
    # appending .domain is the MUA's job.
    append_dot_mydomain = no
    
    # Uncomment the next line to generate "delayed mail" warnings
    #delay_warning_time = 4h
    
    readme_directory = /usr/share/doc/postfix
    
    # TLS parameters
    smtpd_tls_cert_file = /etc/postfix/smtpd.cert
    smtpd_tls_key_file = /etc/postfix/smtpd.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
    
    # See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
    # information on enabling SSL in the smtp client.
    
    myhostname = mail.superweb.dk
    alias_maps = hash:/etc/aliases
    alias_database = hash:/etc/aliases
    myorigin = /etc/mailname
    mydestination = mail.superweb.dk
    relayhost =
    mynetwork = mail.superweb.dk
    # mynetworks = 127.0.0.0/8 [::1]/128
    mailbox_command = procmail -a "$EXTENSION"
    mailbox_size_limit = 0
    recipient_delimiter = +
    inet_interfaces = all
    html_directory = /usr/share/doc/postfix/html
    virtual_alias_domains =
    virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-virtual_forwardings.cf, mysql:/etc/postfix/mysql-virtual_email2email.cf
    virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-virtual_domains.cf
    virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf
    virtual_mailbox_base = /var/vmail
    virtual_uid_maps = static:5000
    virtual_gid_maps = static:5000
    smtpd_sasl_auth_enable = yes
    virtual_gid_maps = static:5000
    smtpd_sasl_auth_enable = yes
    broken_sasl_auth_clients = yes
    smtpd_sasl_authenticated_header = yes
    smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, check_recipient_access mysql:/etc/postfix/mysql-virtual_recipient.cf, reject_unauth_destination
    smtpd_tls_security_level = may
    transport_maps = proxy:mysql:/etc/postfix/mysql-virtual_transports.cf
    relay_domains = mysql:/etc/postfix/mysql-virtual_relaydomains.cf
    relay_recipient_maps = mysql:/etc/postfix/mysql-virtual_relayrecipientmaps.cf
    virtual_create_maildirsize = yes
    virtual_maildir_extended = yes
    virtual_mailbox_limit_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailbox_limit_maps.cf
    virtual_mailbox_limit_override = yes
    virtual_maildir_limit_message = "The user you are trying to reach is over quota."
    virtual_overquota_bounce = yes
    proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_alias_domains $virtual_mailbox_maps $virtual_mailbox_domains $relay_recipient_maps $relay_dom$
    smtpd_sender_restrictions = check_sender_access mysql:/etc/postfix/mysql-virtual_sender.cf
    smtpd_client_restrictions = check_client_access mysql:/etc/postfix/mysql-virtual_client.cf
    maildrop_destination_concurrency_limit = 1
    maildrop_destination_recipient_limit = 1
    virtual_transport = maildrop
    header_checks = regexp:/etc/postfix/header_checks
    mime_header_checks = regexp:/etc/postfix/mime_header_checks
    nested_header_checks = regexp:/etc/postfix/nested_header_checks
    body_checks = regexp:/etc/postfix/body_checks
    content_filter = amavis:[192.168.1.60]:10024
    receive_override_options = no_address_mappings
    
    And from the mail.log :
    Code:
    Oct 14 17:01:28 SuperWebServer10 postfix/master[6261]: daemon started -- version 2.5.5, configuration /etc/postfix
    Oct 14 17:01:41 SuperWebServer10 postfix/pickup[6262]: 50C5D3EC13E: uid=33 from=<www-data>
    Oct 14 17:01:41 SuperWebServer10 postfix/cleanup[6271]: 50C5D3EC13E: message-id=<[email protected]>
    Oct 14 17:01:41 SuperWebServer10 postfix/qmgr[6263]: 50C5D3EC13E: from=<[email protected]>, size=378, nrcpt=1 (queue active)
    Oct 14 17:01:41 SuperWebServer10 postfix/smtp[6274]: connect to 192.168.1.60[192.168.1.60]:10024: Connection refused
    Oct 14 17:01:41 SuperWebServer10 postfix/smtp[6274]: 50C5D3EC13E: to=<[email protected]>, relay=none, delay=0.07, delays=0.06/0.01/0/0, dsn=4.4.1, status=deferred (connect to 192.168.1.60[192.168.1.60]:10024: Connection refused)
    Oct 14 17:02:10 SuperWebServer10 postfix/pickup[6262]: 0732F3EC13F: uid=33 from=<www-data>
    Oct 14 17:02:10 SuperWebServer10 postfix/cleanup[6271]: 0732F3EC13F: message-id=<[email protected]>
    Oct 14 17:02:10 SuperWebServer10 postfix/qmgr[6263]: 0732F3EC13F: from=<[email protected]>, size=378, nrcpt=1 (queue active)
    Oct 14 17:02:10 SuperWebServer10 postfix/smtp[6274]: connect to 192.168.1.60[192.168.1.60]:10024: Connection refused
    Oct 14 17:02:10 SuperWebServer10 postfix/smtp[6274]: 0732F3EC13F: to=<[email protected]>, relay=none, delay=0.06, delays=0.06/0/0/0, dsn=4.4.1, status=deferred (connect to 192.168.1.60[192.168.1.60]:10024: Connection refused)
    change IP in
    Code:
    content_filter = amavis:[192.168.1.60]:10024
    to 127.0.0.1 in the main.cf

    And mail.log :
    Code:
    Oct 14 17:11:08 SuperWebServer10 postfix/pickup[6501]: 441983EC140: uid=33 from=<www-data>
    Oct 14 17:11:08 SuperWebServer10 postfix/cleanup[6520]: 441983EC140: message-id=<[email protected]>
    Oct 14 17:11:08 SuperWebServer10 postfix/qmgr[6502]: 441983EC140: from=<[email protected]>, size=378, nrcpt=1 (queue active)
    Oct 14 17:11:08 SuperWebServer10 postfix/smtp[6504]: connect to 127.0.0.1[127.0.0.1]:10024: Connection refused
    Oct 14 17:11:08 SuperWebServer10 postfix/smtp[6504]: 441983EC140: to=<[email protected]>, relay=none, delay=0.06, delays=0.06/0/0/0, dsn=4.4.1, status=deferred (connect to 127.0.0.1[127.0.0.1]:10024: Connection refused)
    
    //DKLeader
     
  7. DKLeader

    DKLeader New Member

    Extra to my port :

    Just found out that amavis wasn't running.

    mail.log after amavis start and restart of postfix :

    Code:
    Oct 14 17:25:26 SuperWebServer10 postfix/pickup[6821]: B27333EC141: uid=33 from=<www-data>
    Oct 14 17:25:26 SuperWebServer10 postfix/cleanup[6830]: B27333EC141: message-id=<[email protected]>
    Oct 14 17:25:26 SuperWebServer10 postfix/qmgr[6822]: B27333EC141: from=<[email protected]>, size=378, nrcpt=1 (queue active)
    Oct 14 17:25:26 SuperWebServer10 amavis[6698]: (06698-01) (!)connect_to_sql: unable to connect to DSN 'DBI:mysql:database=dbispconfig;host=127.0.0.1;port=3306': Can't connect to MySQL server on '127.0.0.1' (111)
    Oct 14 17:25:26 SuperWebServer10 amavis[6698]: (06698-01) (!!)TROUBLE in process_request: connect_to_sql: unable to connect to any dataset at (eval 86) line 241, <GEN28> line 3.
    Oct 14 17:25:26 SuperWebServer10 amavis[6698]: (06698-01) (!)Requesting process rundown after fatal error
    Oct 14 17:25:26 SuperWebServer10 postfix/smtp[6833]: B27333EC141: to=<[email protected]>, relay=127.0.0.1[127.0.0.1]:10024, delay=0.14, delays=0.08/0.01/0.01/0.04, dsn=4.3.2, status=deferred (host 127.0.0.1[127.0.0.1] said: 421 4.3.2 Service shutting down, closing channel (in reply to RCPT TO command))
    Oct 14 17:25:48 SuperWebServer10 postfix/pickup[6821]: 792DD3EC142: uid=33 from=<www-data>
    Oct 14 17:25:48 SuperWebServer10 postfix/cleanup[6830]: 792DD3EC142: message-id=<[email protected]>
    Oct 14 17:25:48 SuperWebServer10 postfix/qmgr[6822]: 792DD3EC142: from=<[email protected]>, size=378, nrcpt=1 (queue active)
    Oct 14 17:25:48 SuperWebServer10 amavis[6699]: (06699-01) (!)connect_to_sql: unable to connect to DSN 'DBI:mysql:database=dbispconfig;host=127.0.0.1;port=3306': Can't connect to MySQL server on '127.0.0.1' (111)
    Oct 14 17:25:48 SuperWebServer10 amavis[6699]: (06699-01) (!!)TROUBLE in process_request: connect_to_sql: unable to connect to any dataset at (eval 86) line 241, <GEN28> line 3.
    Oct 14 17:25:48 SuperWebServer10 amavis[6699]: (06699-01) (!)Requesting process rundown after fatal error
    Oct 14 17:25:48 SuperWebServer10 postfix/smtp[6833]: 792DD3EC142: to=<[email protected]>, relay=127.0.0.1[127.0.0.1]:10024, delay=0.07, delays=0.03/0/0.01/0.03, dsn=4.3.2, status=deferred (host 127.0.0.1[127.0.0.1] said: 421 4.3.2 Service shutting down, closing channel (in reply to RCPT TO command))
    
     
  8. falko

    falko Super Moderator ISPConfig Developer

    What's the output of
    Code:
    netstat -tap
    ?
     
  9. DKLeader

    DKLeader New Member

    From both web and mail server :

    Code:
    web:~# netstat -tap
    Active Internet connections (servers and established)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
    tcp        0      0 localhost:10024         *:*                     LISTEN      7162/amavisd (maste
    tcp        0      0 localhost:10025         *:*                     LISTEN      8383/master
    tcp        0      0 web.superweb.dk:mysql   *:*                     LISTEN      11268/mysqld
    tcp        0      0 *:submission            *:*                     LISTEN      8383/master
    tcp        0      0 *:sunrpc                *:*                     LISTEN      1763/portmap
    tcp        0      0 *:ftp                   *:*                     LISTEN      11087/pure-ftpd (SE
    tcp        0      0 web.superweb.dk:domain  *:*                     LISTEN      30034/mydns
    tcp        0      0 localhost:domain        *:*                     LISTEN      30034/mydns
    tcp        0      0 *:smtp                  *:*                     LISTEN      8383/master
    tcp        0      0 *:27                    *:*                     LISTEN      14672/sshd
    tcp        0      0 *:57182                 *:*                     LISTEN      1774/rpc.statd
    tcp        0    152 web.superweb.dk:27      148.122.180.7:56569     ESTABLISHED 29560/0
    tcp        0      0 web.superweb.dk:mysql   db2.superweb.dk:44699   TIME_WAIT   -
    tcp        0      0 web.superweb.dk:33478   192.168.1.40:mysql      TIME_WAIT   -
    tcp        0      0 web.superweb.dk:49745   192.168.1.60:mysql      TIME_WAIT   -
    tcp        0      0 web.superweb.dk:34342   192.168.1.40:mysql      ESTABLISHED 23984/php-cgi
    tcp        0      0 web.superweb.dk:mysql   db1.superweb.dk:47513   TIME_WAIT   -
    tcp6       0      0 [::]:www                [::]:*                  LISTEN      6656/apache2
    tcp6       0      0 [::]:tproxy             [::]:*                  LISTEN      6656/apache2
    tcp6       0      0 [::]:ftp                [::]:*                  LISTEN      11087/pure-ftpd (SE
    tcp6       0      0 localhost:domain        [::]:*                  LISTEN      30034/mydns
    tcp6       0      0 [::]:https              [::]:*                  LISTEN      6656/apache2
    tcp6       0      0 [::]:27                 [::]:*                  LISTEN      14672/sshd
    tcp6       0      0 web.superweb.dk:www     b3091085.crawl.ya:35293 TIME_WAIT   -
    tcp6       0      0 web.superweb.dk:www     b3091085.crawl.ya:35354 TIME_WAIT   -
    Code:
    mail:~# netstat -tap
    Active Internet connections (servers and established)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
    tcp        0      0 *:51584                 *:*                     LISTEN      2195/rpc.statd
    tcp        0      0 *:imaps                 *:*                     LISTEN      3363/dovecot
    tcp        0      0 *:pop3s                 *:*                     LISTEN      3363/dovecot
    tcp        0      0 :10024                  *:*                     LISTEN      2044/amavisd (ch16-
    tcp        0      0 :10025                  *:*                     LISTEN      31909/master
    tcp        0      0 *:mysql                 *:*                     LISTEN      2632/mysqld
    tcp        0      0 *:submission            *:*                     LISTEN      31909/master
    tcp        0      0 *:pop3                  *:*                     LISTEN      3363/dovecot
    tcp        0      0 *:imap2                 *:*                     LISTEN      3363/dovecot
    tcp        0      0 :spamd                  *:*                     LISTEN      2718/spamd.pid
    tcp        0      0 *:sunrpc                *:*                     LISTEN      2184/portmap
    tcp        0      0 *:ftp                   *:*                     LISTEN      3260/pure-ftpd (SER
    tcp        0      0 *:24                    *:*                     LISTEN      2541/sshd
    tcp        0      0 *:smtp                  *:*                     LISTEN      31909/master
    tcp        0    248 mail.superweb.dk:24     148.122.180.7:56613     ESTABLISHED 28385/0
    tcp        0      0 mail.superweb.dk:57673  web.superweb.dk:mysql   TIME_WAIT   -
    tcp6       0      0 [::]:www                [::]:*                  LISTEN      3542/apache2
    tcp6       0      0 [::]:ftp                [::]:*                  LISTEN      3260/pure-ftpd (SER
    tcp6       0      0 localhost:ipp           [::]:*                  LISTEN      3158/cupsd
    tcp6       0      0 [::]:24                 [::]:*                  LISTEN      2541/sshd
    tcp6       0      0 [::]:https              [::]:*                  LISTEN      3542/apache2
    mail:~#
    
     
  10. falko

    falko Super Moderator ISPConfig Developer

    I think the problem is that MySQL isn't listening on 127.0.0.1.
     
  11. DKLeader

    DKLeader New Member

    Ok - solved the part with the mysql error on amavis.
    Emails are still not passed through :

    Code:
    Oct 16 20:09:52 SuperWebServer10 postfix/smtpd[17554]: connect from localhost[127.0.0.1]
    Oct 16 20:09:52 SuperWebServer10 postfix/smtpd[17554]: 07A513EC12B: client=localhost[127.0.0.1]
    Oct 16 20:09:52 SuperWebServer10 postfix/cleanup[17548]: 07A513EC12B: message-id=<[email protected]>
    Oct 16 20:09:52 SuperWebServer10 postfix/qmgr[8385]: 07A513EC12B: from=<[email protected]>, size=956, nrcpt=1 (queue active)
    Oct 16 20:09:52 SuperWebServer10 postfix/smtpd[17554]: disconnect from localhost[127.0.0.1]
    Oct 16 20:09:52 SuperWebServer10 amavis[7164]: (07164-09) Passed BAD-HEADER, <[email protected]> -> <[email protected]>, quarantine: T/badh-TKqw8n8pMIdq, Message-ID: <[email protected]>, mail_id: TKqw8n8pMIdq, Hits: 6.814, size: 376, queued_as: 07A513EC12B, 368 ms
    Oct 16 20:09:52 SuperWebServer10 postfix/smtp[17551]: A08C43EC12F: to=<[email protected]>, relay=127.0.0.1[127.0.0.1]:10024, delay=0.41, delays=0.04/0/0/0.37, dsn=2.0.0, status=sent (250 2.0.0 Ok, id=07164-09, from MTA([127.0.0.1]:10025): 250 2.0.0 Ok: queued as 07A513EC12B)
    Oct 16 20:09:52 SuperWebServer10 postfix/qmgr[8385]: A08C43EC12F: removed
    Oct 16 20:09:52 SuperWebServer10 postfix/pipe[17555]: 07A513EC12B: to=<[email protected]>, relay=maildrop, delay=0.05, delays=0.01/0.01/0/0.04, dsn=2.0.0, status=sent (delivered via maildrop service)
    Oct 16 20:09:52 SuperWebServer10 postfix/qmgr[8385]: 07A513EC12B: removed
    
     
  12. falko

    falko Super Moderator ISPConfig Developer

    Looks ok. What's the output of
    Code:
    grep 24timer /etc/passwd
    ?
     
  13. DKLeader

    DKLeader New Member

    From web server output is :
    Code:
    web:~# grep 24timer /etc/passwd
    web:~#
    
    and the mail server :
    Code:
    mail:~# grep 24timer /etc/passwd
    mail:~#
    
    Checked the passwd file and none of the ispconfig users on web and mail server are mentioned by name in these.
    Users can log in and can use ftp - does not know if that info helps :)

    On the web server I forund these :
    Code:
    web4:x:5006:5006::/var/www/clients/client1/web4:/bin/false
    .
    .
    web5:x:5027:5006::/var/www/clients/client1/web5:/bin/false
    
    That must be for each domain
     
  14. till

    till Super Moderator Staff Member ISPConfig Developer

    The email has been delivered correctly to the mailbox [email protected] accortding to the logfile. Have you checked this mailbox?

    Additionally there seem to be a problem with the mail headers in the email that you create from php. You should disable header checks in the amavisd policy that you selected for the account [email protected] to ensure that you get the emails and then check the header and correct the problem as other external servers might reject the email too when the headers are incorrect.
     

Share This Page