-2

I am trying to send mail using PHP mail() function. I have install postfix, modified all of the required files as suggested by the Ubuntu help docs.

I also changed my php.ini file to set the path of the sendmail_path to sendmail_path = "/usr/sbin/sendmail -t -i" but still I cannot send mail to my hotmail account or yahoo or gmail.
Edit: Stop down voting me ; even I do not know my problem . All I realized so far is that my ISP is blocking every port except 80 . If any one can help me mitigate that problem , it will be my pleasure .

Few Tem
  • 631
  • 1
  • 7
  • 16
  • Please be more specific. When you attempt to send mail to your Yahoo or GMail account, do you get an error message? Do you get a bounce? Do you get anything in your mail logs? Does Jeff personally show at your door and yell "NO!"? – derobert Jan 15 '12 at 07:55
  • Well I am using Ubuntu - 10.04 on Virtual Box ; the mail function that I called using PHP mail() seems to work alright . However I did not receive any email in my inbox . It "seems" to me that my SMTP port in my host , i.e in Windows 7 is somehow blocked , which may block my mail to pass to hotmail server . Now do you think I am thinking in the right direction ? – Few Tem Jan 15 '12 at 15:32
  • Are you running this on a residential Internet connection? Its quite possible that your ISP has blocked TCP/25 to everything except their mail servers. Does `telnet gmail-smtp-in.l.google.com 25` (from a `cmd` prompt) connect and give you a `220 mx.google.com ESMTP…` banner? You'll need to configure your MTA to smarthost through your ISP's mail server. – derobert Jan 15 '12 at 18:35
  • My ISP has blocked everything except port 80 , sad but true . And no telnet to gmail-smtp... gives me connection error only , nothing more .So is there anything that I have to change on my host OS , or I have to reconfigure Postfix on Ubuntu to smarthost ? I am new to this stuff , so patience . – Few Tem Jan 16 '12 at 15:13
  • You need to reconfigure Postfix on Ubuntu. If you need help on that, ask on the Ubuntu site. – derobert Jan 16 '12 at 16:00

1 Answers1

2

When you configure Postfix you dont need sendmail which is also an MTA like Postfix. So sendmail_path = "/usr/sbin/sendmail -t -i" is not used by php.

You have to connect to your Postfix server (which is listening in localhost:25) by php to send mail.

Here is an example modified based on this answer,

<?php

    require_once "Mail.php";

    $from = "<from.gmail.com>";
    $to = "<to.yahoo.com>";
    $subject = "Hi!";
    $body = "Hi,\n\nHow are you?";

    $host = "localhost";
    $port = "25";
    $username = "<myaccount.gmail.com>";
    $password = "password";

    $headers = array ('From' => $from,
      'To' => $to,
      'Subject' => $subject);
    $smtp = Mail::factory('smtp',
      array ('host' => $host,
        'port' => $port,
        'auth' => true,
        'username' => $username,
        'password' => $password));

    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)) {
      echo("<p>" . $mail->getMessage() . "</p>");
     } else {
      echo("<p>Message successfully sent!</p>");
     }

?> 

Note that Mail is a pear package.

Community
  • 1
  • 1
Shiplu Mokaddim
  • 54,465
  • 14
  • 131
  • 183
  • Thanks for your answer , mate . I have got a new problem at hand . I somehow cannot open my port 25 on windows 7 . I have added exceptions to the inbound rules of windows firewall , even then when I telnet to localhost on port 25 , it gives me connection error . Did you came across this problem . http://www.canyouseeme.org/ : I have used the tool on this website to see if my port 25 is open , it said only port 80 is open . Btw , I used word to word description from this website for opening port 25 :http://www.wikihow.com/Open-Port-25 .Any help on opening port 25 will be appreciated . – Few Tem Jan 15 '12 at 17:11
  • @FewTem You have configured Postfix on Ubuntu. why are you trying to connect it from Windows? Its in you Ubuntu dude. – Shiplu Mokaddim Jan 15 '12 at 17:46
  • I thought that Ubuntu uses the same port as my Windows host , thats why ; anyway thanks for the heads up . – Few Tem Jan 15 '12 at 18:30
  • I got this error : and sorry I am new to php , so could not make much out of it . Strict Standards: Non-static method PEAR::isError() should not be called statically in /opt/lampp/htdocs/1/mailtosend.php on line 27 unable to authenticate to smtp server – Few Tem Jan 15 '12 at 19:01
  • @Few set the `error_reporting` setting to `E_ALL ^ E_STRICT` in your `php.ini` – Shiplu Mokaddim Jan 15 '12 at 19:08