Configurer l'envoi de mail avec PHP sous linux depuis une freebox
Afin de tester mes scripts, j'ai voulu récemment tester l'envoi de mail avec PHP, mais voila ... php utilise l'affreux SENDMAIL !!!
Je vous propose ici la méthode que j'ai utilisée pour résoudre ce petit pb et pouvoir envoyer mes mails via php ;)
Voila ce que j'ai derrière ma freeboxhd :
[root@srv1 mail]# dig MX free.fr
; <<>> DiG 9.2.4 <<>> MX free.fr
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 25771
;; flags: qr; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;free.fr. IN MX
;; ANSWER SECTION:
free.fr. 15777 IN MX 20 mx2.free.fr.
free.fr. 15777 IN MX 10 mx1.free.fr.
;; Query time: 65 msec
;; SERVER: 212.27.54.252#53(212.27.54.252)
;; WHEN: Sat Feb 17 15:40:06 2007
;; MSG SIZE rcvd: 65[root@srv1 mail]#
Je commence par modifier le fichier de template de sendmail (utiliser pour générer la configuration)
en y ajoutant ceci : (en bleu)
ivert(-1)dnl
include(`/usr/share/sendmail-cf/m4/cf.m4')dnl
VERSIONID(`setup for Red Hat Linux')dnl
OSTYPE(`linux')dnl
define(`confDEF_USER_ID',``8:12'')dnl
define(`confTO_CONNECT', `1m')dnl
define(`confTRY_NULL_MX_LIST',true)dnl
define(`confDONT_PROBE_INTERFACES',true)dnl
define(`PROCMAIL_MAILER_PATH',`/usr/bin/procmail')dnl
define(`ALIAS_FILE', `/etc/aliases')dnl
define(`STATUS_FILE', `/var/log/mail/statistics')dnl
define(`UUCP_MAILER_MAX', `2000000')dnl
define(`confUSERDB_SPEC', `/etc/mail/userdb.db')dnl
define(`confPRIVACY_FLAGS', `authwarnings,novrfy,noexpn,restrictqrun')dnl
define(`confAUTH_OPTIONS', `A')dnl
define(`confTO_IDENT', `0')dnl
FEATURE(`no_default_msa',`dnl')dnl
FEATURE(`smrsh',`/usr/sbin/smrsh')dnl
FEATURE(`mailertable',`hash -o /etc/mail/mailertable.db')dnl
FEATURE(`virtusertable',`hash -o /etc/mail/virtusertable.db')dnl
FEATURE(redirect)dnl
FEATURE(always_add_domain)dnl
FEATURE(use_cw_file)dnl
FEATURE(use_ct_file)dnl
FEATURE(local_procmail,`',`procmail -t -Y -a $h -d $u')dnl
FEATURE(`access_db',`hash -T<TMPF> -o /etc/mail/access.db')dnl
FEATURE(`blacklist_recipients')dnl
EXPOSED_USER(`root')dnl
DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl
FEATURE(`accept_unresolvable_domains')dnl
LOCAL_DOMAIN(`localhost.localdomain')dnl
FEATURE(always_add_domain)dnl
FEATURE(`masquerade_entire_domain')dnl
FEATURE(`allmasquerade')dnl
MASQUERADE_DOMAIN(`free.fr.')dnl
MASQUERADE_DOMAIN(localhost)dnl
MASQUERADE_DOMAIN(localhost.localdomain)dnl
FEATURE(`relay_based_on_MX')dnl
FEATURE(masquerade_envelope)dnl
define(RELAY_MAILER, TCP)
FEATURE(`accept_unresolvable_domains')
MAILER(smtp)dnl
MAILER(procmail)dnl
Dans un premier temps j'ai voulu regénérer un fichier de config pour sendmail, bien
sur cela n'a pas marché !
Cannot open /usr/share/sendmail-cf/m4/cf.m4: No such file or directory
Après une brève recherche sur google j'ai finnalement trouvé quand installant ceci tout rentré dans l'ordre :
[root@srv1 mail]# apt-get install sendmail-cf
Ensuite je regénère le fichier de configuration :
[root@srv1 mail]# m4 sendmail.mc > sendmail.cf
Voila mon fichier hosts :
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost
192.168.0.135 srv2
192.168.0.134 srv1
192.168.0.156 srv3
Ensuite sur mon serveur je lance sendmail et je contrôle qu'il écoute sur une socket ;)
[root@srv1 mail]# service sendmail start
[root@srv1 mail]# netstat -an | grep 25
Puis je redémarre le tout :
[root@srv1 mail]# service sendmail restart
Shutting down sendmail: [ OK ]
Shutting down sm-client: [ OK ]
Starting sendmail: [ OK ]
Starting sm-client: [ OK ]
[root@srv1 mail]#
Modification du PHP.INI
;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;
[Syslog]
; Whether or not to define the various syslog variables (e.g. $LOG_PID,
; $LOG_CRON, etc.). Turning it off is a good idea performance-wise. In
; runtime, you can define these variables by calling define_syslog_variables().
define_syslog_variables = Off
[mail function]
; For Win32 only.
;SMTP = localhost
; For Win32 only.
;sendmail_from = me@localhost.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =
sendmail_path = /usr/sbin/sendmail -t -i -f f4eyq@free.fr
Test d'envoi de mail
Pour cela j'utilise un petit script php : disponible : http://fr.php.net/manual/fr/ref.mail.php
( function send_mail )
En regardant sur le serveur qui héberge ici un apache/php/mysql) on voit bien l'adresse du serveur de free qui nous sert de relais :
relay=mx1.free.fr. [212.27.48.6]
[root@srv1 mail]# tail -f /var/log/maillog
Feb 17 16:03:24 srv1 sendmail[4911]: alias database /etc/aliases rebuilt by root
Feb 17 16:03:24 srv1 sendmail[4911]: /etc/aliases: 78 aliases, longest 10 bytes, 802 bytes total
Feb 17 16:03:27 srv1 sendmail[4936]: alias database /etc/aliases rebuilt by root
Feb 17 16:03:27 srv1 sendmail[4936]: /etc/aliases: 78 aliases, longest 10 bytes, 802 bytes total
Feb 17 16:03:27 srv1 sendmail[4941]: starting daemon (8.13.1): SMTP+queueing@01:00:00
Feb 17 16:03:27 srv1 sm-msp-queue[4949]: starting daemon (8.13.1): queueing@01:00:00
Feb 17 16:04:25 srv1 sendmail[4957]: l1HF4Ok6004957: Authentication-Warning: -: nobody set sender to f4eyq@free.fr using -f
Feb 17 16:04:25 srv1 sendmail[4957]: l1HF4Ok6004957: from=f4eyq@free.fr, size=693, class=0, nrcpts=1, msgid=< crxcluster@192.168.0.134>, relay=nobody@localhost
Feb 17 16:04:25 srv1 sendmail[4958]: l1HF4PHh004958: from=<f4eyq@free.fr>, size=801, class=0, nrcpts=1, msgid=< crxcluster@192.168.0.134>, proto=ESMTP, daemon=MTA, relay=localhost.localdomain [127.0.0.1]
Feb 17 16:04:25 srv1 sendmail[4957]: l1HF4Ok6004957: to=f4eyq@free.fr, ctladdr=f4eyq@free.fr (99/99), delay=00:00:01, xdelay=00:00:00, mailer=relay, pri=30693, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (l1HF4PHh004958 Message accepted for delivery)
Feb 17 16:04:25 srv1 sendmail[4960]: l1HF4PHh004958: to=<f4eyq@free.fr>, delay=00:00:00, xdelay=00:00:00, mailer=esmtp, pri=120801, relay=mx1.free.fr. [212.27.48.6], dsn=2.0.0, stat=Sent (ok 1171727296 qp 15302)
Sur mon deuxième serveur (crxcluster.homelinux.com)
[root@tuxsrv mail]# tail -f /var/log/maillog
Feb 17 17:08:17 tuxsrv sendmail[6493]: l1HG8GW6006493: Authentication-Warning: localhost.localdomain: nobody set sender to f4eyq@free.fr using -f
Feb 17 17:08:17 tuxsrv sendmail[6493]: l1HG8GW6006493: from=f4eyq@free.fr, size=693, class=0, nrcpts=1, msgid=< crxcluster@192.168.0.156>, relay=nobody@localhost
Feb 17 17:08:17 tuxsrv sendmail[6494]: l1HG8HxG006494: from=<f4eyq@free.fr>, size=841, class=0, nrcpts=1, msgid=< crxcluster@192.168.0.156>, proto=ESMTP, daemon=MTA, relay=tuxsrv [127.0.0.1]
Feb 17 17:08:17 tuxsrv sendmail[6493]: l1HG8GW6006493: to=f4eyq@free.fr, ctladdr=f4eyq@free.fr (99/99), delay=00:00:01, xdelay=00:00:00, mailer=relay, pri=30693, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (l1HG8HxG006494 Message accepted for delivery)
Feb 17 17:08:17 tuxsrv sendmail[6496]: l1HG8HxG006494: to=<f4eyq@free.fr>, delay=00:00:00, xdelay=00:00:00, mailer=esmtp, pri=120841, relay=mx1.free.fr. [212.27.48.6], dsn=2.0.0, stat=Sent (ok 1171728673 qp 24304)
Les warning qui apparaissent sont dus aux options du PHP.INI
Si je retire l'option -i -f :
sendmail_path = /usr/sbin/sendmail -t -i
Voila ce que j'obtient :
[root@tuxsrv mail]# tail -f /var/log/maillog
Feb 17 17:19:45 tuxsrv sendmail[6738]: l1HGJjrK006738: from=nobody, size=693, cl ass=0, nrcpts=1, msgid=< crxcluster@192.168.0.156>, relay=nobody@localhost
Feb 17 17:19:46 tuxsrv sendmail[6739]: l1HGJjx9006739: from=<nobody@localhost.lo caldomain>, size=748, class=0, nrcpts=1, msgid=< crxcluster@192.168.0.156>, prot o=ESMTP, daemon=MTA, relay=tuxsrv [127.0.0.1]
Feb 17 17:19:46 tuxsrv sendmail[6738]: l1HGJjrK006738: to=f4eyq@free.fr, ctladdr =nobody (99/99), delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=30693, relay =[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (l1HGJjx9006739 Message accepted for delivery)
Feb 17 17:19:46 tuxsrv sendmail[6741]: l1HGJjx9006739: to=<f4eyq@free.fr>, ctlad dr=<nobody@localhost.localdomain> (99/99), delay=00:00:00, xdelay=00:00:00, mail er=esmtp, pri=120748, relay=mx1.free.fr. [212.27.48.6], dsn=5.6.0, stat=Data for mat error
Feb 17 17:19:46 tuxsrv sendmail[6741]: l1HGJjx9006739: l1HGJkx9006741: DSN: Data format error
Feb 17 17:19:46 tuxsrv sendmail[6741]: l1HGJkx9006741: to=root, delay=00:00:00, xdelay=00:00:00, mailer=local, pri=31979, dsn=2.0.0, stat=Sent
Annexe
Sources :
http://forums.fedora-fr.org/viewtopic.php?pid=120423
http://fr.php.net/manual/fr/ref.mail.php
http://drupal.org/node/1155