重要なお知らせ

「教えて! goo」は2025年9月17日(水)をもちまして、サービスを終了いたします。詳細はこちら>

電子書籍の厳選無料作品が豊富!

PHPで作成したpostfixを使ったメール送信プログラムが上手くいきません。

最下部の動作環境において下記のプログラムを実行したところ、trueが返ってきているためmb_send_mail()自体は成功しているようですが、yahooのウェブメールにメールが来ておらず、どうやら送信に失敗しているようです。(他のアドレスにも試しに送ってみましたがいずれも届きませんでした)

postfixなどの設定が上手くいっていないのではないかと思い、一応ウェブで調べて自分なりに,/etc/postfix/main.cfを設定してみたのですが、うまく行きません。main.cfの設定では

mydomain = example.comを加える
myhostname = example.comを加える
#inet_interfaces = all → inet_interfaces = all(コメントアウトをはずす)

といったことを試みてみました。

他にどこに原因があるのか分かりません。php.iniなども特にいじっていません。どうすればメールの送信が出来るようになるのでしょうか?
どうぞご教授お願いいたします。

<?php

$to = "xxxxx@yahoo.co.jp";
$subject = "テストメール";
$body = "テスト";

mb_language("ja");
mb_internal_encoding("ISO-2022-JP");

if(mb_send_mail($to, $subject, $body)){
echo "true\n";
}else{
echo "false\n";
}

?>


動作環境
-----------------------
本体:MacBook MB467J/A
OS: Mac OS X
PHP 5.2.8
Postfix 2.4.3
-----------------------

A 回答 (5件)

自宅サーバーでテストなさっているのですね。


SMTPの設定は間違いなくできていますか。(構築したSMTPを使って、メールソフトからは送信できますね)
Postfixにはsendmail互換プログラムが付いてますが、これが有効になっていますか。
そして、php.iniのデフォルトでは、sendmeilのパスが /usr/sbin/sendmail になっているはずですがここんとこはOKですか。

この回答への補足

自力で解決することが出来ました。

原因はyahooのメールサーバでスパム除けのために送信ドメインと送信IPアドレスが一致しないものは拒否しているためでした。そのためmain.cfでrelayhostの設定で下記のようにプロバイダのsmtpサーバを経由するようにし、送信元のアドレスのドメインを変更すると、上手く行きました。

relayhost = [smtp.xxx.ocn.ne.jp]
myhostname = xxx.ocn.ne.jp

同様の問題で悩んでいる方のために、参考にしたURLを載せておきます。

http://blog.fkoji.com/2008/12291614.html

ご回答くださった方、ありがとうございました。

補足日時:2009/08/19 03:52
    • good
    • 0
この回答へのお礼

ご回答ありがとうございます。

>SMTPの設定は間違いなくできていますか。
SMTPの設定と言うと具体的にどのようなことでしょうか?ローカルのメールクライアントを使用しておらず(ウェブメールのみ)、メールソフトからの送受信は確認出来ていないのですが他に確認方法はありますでしょうか?

>Postfixにはsendmail互換プログラムが付いてますが、これが有効になっていますか。
有効になっているというのは具体的に何を確認すればよいでしょうか?
/etc/postfix/main.cfにおいて、
sendmail_path = /usr/sbin/sendmail
と設定してあるのですが、他に設定が必要でしょうか?

>php.iniのデフォルトでは、sendmeilのパスが /usr/sbin/sendmail になっているはずですがここんとこはOKですか。
sendmail_path/usr/sbin/sendmail -t -i
に設定されているので、大丈夫だと思います。

補足:
mail.logには、下記のように記載されており、yahooのメールサーバにsmtpで接続出来ていないようですが、いまだにその原因が分かりません。
Aug 19 02:24:16 kendai-mac postfix/smtp[2328]: connect to mx2.mail.yahoo.co.jp[203.216.243.171]: Connection refused (port 25)
Aug 19 02:24:21 kendai-mac postfix/smtp[2328]: connect to mx2.mail.yahoo.co.jp[124.147.39.208]: Connection refused (port 25)
Aug 19 02:24:22 kendai-mac postfix/smtp[2327]: connect to mx1.mail.yahoo.co.jp[124.83.171.181]: Connection refused (port 25)
Aug 19 02:24:26 kendai-mac postfix/smtp[2327]: connect to mx3.mail.yahoo.co.jp[203.216.247.183]: Connection refused (port 25)

サーバ関連やプログラミング初心者なため、勉強のために上記のプログラムを作成したのですが上手くいっておりません。稚拙な質問が多いと思いますが、ご容赦下さい。

お礼日時:2009/08/19 03:06

■上手行くかどうかはわかりません。


※mb_convert_encoding()関数追加

mb_language("Japanese");
$to = "xxxxx@yahoo.co.jp";
$subject = "テストメール";
$header="From: ".$mail_address."\r\n" ."Content-Type:text/plain;charaset=\"iso-2022-jp\"";
$body = "テスト";

mb_send_mail($to,$subject,mb_convert_encoding($body,"JIS","UTF-8"),$header))

JIS UTF-8の部分はPHP設定と関連かな?
そのへん調べてください。
    • good
    • 0

どうしても解決できないのであればPEARのmail_mimeなどでsmtpサーバーに


アクセスするとよいでしょう
    • good
    • 0
この回答へのお礼

ご回答ありがとうございます。
しばらく努力してみて解決しないようでしたら、そちらの方法を試させていただきます。

お礼日時:2009/08/18 21:31

サーバのメールのログも確認した方がよいかもしれません。

この回答への補足

Aug 18 21:43:04 xxxx-mac postfix/master[226]: daemon started -- version 2.4.3, configuration /etc/postfix
Aug 18 21:43:04 xxxx-mac postfix/qmgr[228]: 0F263299E40: from=<xxxx@example.com>, size=298, nrcpt=1 (queue active)
Aug 18 21:43:04 xxxx-mac postfix/pickup[227]: A95AC29A193: uid=501 from=<xxxx>
Aug 18 21:43:04 xxxx-mac postfix/cleanup[229]: A95AC29A193: message-id=<20090818124304.A95AC29A193@example.com>
Aug 18 21:43:04 xxxx-mac postfix/qmgr[228]: A95AC29A193: from=<xxxx@example.com>, size=438, nrcpt=1 (queue active)
Aug 18 21:43:04 xxxx-mac postfix/qmgr[228]: AF88F29A064: from=<xxxx@example.com>, size=438, nrcpt=1 (queue active)
Aug 18 21:43:04 xxxx-mac postfix/qmgr[228]: BF57629A048: from=<xxxx@example.com>, size=438, nrcpt=1 (queue active)
Aug 18 21:43:04 xxxx-mac postfix/qmgr[228]: EBC6329A040: from=<xxxx@example.com>, size=438, nrcpt=1 (queue active)
Aug 18 21:43:04 xxxx-mac postfix/smtp[234]: connect to mx2.mail.yahoo.co.jp[203.216.243.170]: Connection refused (port 25)
Aug 18 21:43:07 xxxx-mac postfix/smtp[231]: connect to gmail-smtp-in.l.google.com[209.85.221.58]: Connection refused (port 25)
Aug 18 21:43:12 xxxx-mac postfix/smtp[231]: connect to alt1.gmail-smtp-in.l.google.com[74.125.93.114]: Connection refused (port 25)
Aug 18 21:43:15 xxxx-mac postfix/smtp[235]: connect to mx1.mail.yahoo.co.jp[124.83.183.240]: Connection refused (port 25)
Aug 18 21:43:17 xxxx-mac postfix/smtp[235]: connect to mx1.mail.yahoo.co.jp[124.83.179.206]: Connection refused (port 25)
Aug 18 21:43:22 xxxx-mac postfix/smtp[235]: connect to mx5.mail.yahoo.co.jp[203.216.247.182]: Connection refused (port 25)
Aug 18 21:43:23 xxxx-mac postfix/smtp[231]: connect to alt2.gmail-smtp-in.l.google.com[209.85.201.27]: Connection refused (port 25)
Aug 18 21:43:33 xxxx-mac postfix/smtp[235]: connect to mx1.mail.yahoo.co.jp[124.83.171.181]: Connection refused (port 25)
Aug 18 21:43:34 xxxx-mac postfix/smtp[231]: connect to alt3.gmail-smtp-in.l.google.com[209.85.199.114]: Connection refused (port 25)
Aug 18 21:43:34 xxxx-mac postfix/smtp[232]: connect to mx3.mail.yahoo.co.jp[114.111.75.205]: Operation timed out (port 25)
Aug 18 21:43:34 xxxx-mac postfix/smtp[234]: connect to mx3.mail.yahoo.co.jp[124.147.39.207]: Operation timed out (port 25)
Aug 18 21:43:34 xxxx-mac postfix/smtp[233]: connect to mx1.mail.yahoo.co.jp[124.83.183.240]: Operation timed out (port 25)
Aug 18 21:43:37 xxxx-mac postfix/smtp[233]: connect to mx5.mail.yahoo.co.jp[203.216.247.181]: Connection refused (port 25)
Aug 18 21:43:42 xxxx-mac postfix/smtp[233]: connect to mx5.mail.yahoo.co.jp[203.216.243.173]: Connection refused (port 25)
Aug 18 21:43:44 xxxx-mac postfix/smtp[235]: connect to mx2.mail.yahoo.co.jp[203.216.243.170]: Connection refused (port 25)
Aug 18 21:43:44 xxxx-mac postfix/smtp[235]: EBC6329A040: to=<xxxx@yahoo.co.jp>, relay=none, delay=646, delays=606/0.02/40/0, dsn=4.4.1, status=deferred (connect to mx2.mail.yahoo.co.jp[203.216.243.170]: Connection refused)
Aug 18 21:43:45 xxxx-mac postfix/smtp[234]: connect to mx1.mail.yahoo.co.jp[124.83.183.240]: Connection refused (port 25)
Aug 18 21:43:47 xxxx-mac postfix/smtp[234]: connect to mx5.mail.yahoo.co.jp[203.216.247.182]: Connection refused (port 25)
Aug 18 21:43:52 xxxx-mac postfix/smtp[234]: connect to mx3.mail.yahoo.co.jp[203.216.247.184]: Connection refused (port 25)
以下省略

補足日時:2009/08/18 22:03
    • good
    • 0
この回答へのお礼

ご回答ありがとうございます。
ログ(/private/var/log/mail.log)ですが、お礼の欄だと字数がオーバーしてしまうため補足欄に載せさせていただきました。

ログに関して追加の質問で恐縮ですが、smtpでyahooのメールサーバに接続しようとしているようですが、connection refusedになっているのはなぜでしょうか?

また途中に
Aug 18 21:43:07 xxxx-mac postfix/smtp[231]: connect to gmail-smtp-in.l.google.com[209.85.221.58]: Connection refused (port 25)
というように、gmailに接続しているようなのはプログラム以外の部分か何かで私のほうで間違った設定をしたためでしょうか?

お礼日時:2009/08/18 22:09

例えば一般論として、メールのヘッダにFromアドレスがないと、迷惑メールとして処理されたりしてませんか。

    • good
    • 0
この回答へのお礼

ご回答ありがとうございます。
以下のようにヘッダをつけてみましたが、解決しませんでした。

<?php

$to = "xxxx@yahoo.co.jp";
$subject = "テストメール";
$body = "テスト";
$headers = "From: xxxx@yahoo.co.jp\r\n";

mb_language("ja");
mb_internal_encoding("ISO-2022-JP");

if(mb_send_mail($to, $subject, $body, $headers)){
echo "true!\n";
}else{
echo "false!\n";
}

?>

お礼日時:2009/08/18 21:30

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!