Setting SMTP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

JennFish
Forum Newbie
Posts: 5
Joined: Fri Mar 05, 2004 9:05 pm

Post by JennFish »

phpinfo() shows jenn as the SMTP value. How weird is that?

Should I scrap PostCast and try JAMES?
peoples
Forum Newbie
Posts: 2
Joined: Thu Mar 11, 2004 1:13 pm

Post by peoples »

Is it possible to send mails from within PHP-scripts without running an SMTP server on localhost.
My default SMTP-server is mail.planetinternet.be, but I'm not able to send emails from PHP-scripts.

Code: Select all

http://peoples.dyndns.org/phpinfo.php

Code: Select all

http://peoples.dyndns.org/extra/mailtest.php
this last one contains:

Code: Select all

<html><head><title>PHP Mail Tester</title></head>
<body>
<?php

if ($address=="") &#123;

  echo "<form action="mailtest.php" method=get><p>Address to email: <input type=text name=address></p>";
  echo "<p><input type=submit value="Send email"></p>";

&#125; else &#123;

  $subject = "Testing mail() function";
  $message = "This is a test email";

  mail($address,$subject,$message,"From: "Mail Test" <$address>");

  echo "<p>Mail sent. Were any errors shown? If there were, mail is probably not set up correctly.</p>";

&#125;

?>
</body>
</html>
can somebody help me out here?
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

peoples wrote:Is it possible to send mails from within PHP-scripts without running an SMTP server on localhost?
I get the impression that you can't, unless you're running it on Windows. Under Linux, PHP drops out and runs 'sendmail' to send your mail.

Digging through the help, I found a note from a user that built his own mail sending function that looks like it would do what you need. I haven't tested it, but I'll paste it here:

grey at greywyvern dot com (09-May-2003 04:23)

Here's a function I'm continually working at to send multiple emails while only opening the socket once (much faster than mail()) while sending a separate email to each address. It also includes many headers which you can adjust to your liking. Note the comment which explains the array format for incoming "To:" addresses.

Code: Select all

<?php
function socketmail($toArray, $subject, $message) {
 // $toArray format --> array("Name1" => "address1", "Name2" => "address2", ...)
 ini_set(sendmail_from, "myemail@address.com");
 $connect = fsockopen (ini_get("SMTP"), ini_get("smtp_port"), $errno, $errstr, 30) or die("Could not talk to the sendmail server!"); 
   $rcv = fgets($connect, 1024); 
 fputs($connect, "HELO {$_SERVER['SERVER_NAME']}\r\n");
   $rcv = fgets($connect, 1024); 
 while (list($toKey, $toValue) = each($toArray)) {
   fputs($connect, "MAIL FROM:myemail@address.com\r\n"); 
     $rcv = fgets($connect, 1024); 
   fputs($connect, "RCPT TO:$toValue\r\n"); 
     $rcv = fgets($connect, 1024); 
   fputs($connect, "DATA\r\n"); 
     $rcv = fgets($connect, 1024); 
   fputs($connect, "Subject: $subject\r\n"); 
   fputs($connect, "From: My Name <myemail@address.com>\r\n"); 
   fputs($connect, "To: $toKey  <$toValue>\r\n"); 
   fputs($connect, "X-Sender: <myemail@address.com>\r\n"); 
   fputs($connect, "Return-Path: <myemail@address.com>\r\n"); 
   fputs($connect, "Errors-To: <myemail@address.com>\r\n"); 
   fputs($connect, "X-Mailer: PHP\r\n"); 
   fputs($connect, "X-Priority: 3\r\n"); 
   fputs($connect, "Content-Type: text/plain; charset=iso-8859-1\r\n"); 
   fputs($connect, "\r\n"); 
   fputs($connect, stripslashes($message)." \r\n"); 
   fputs($connect, ".\r\n"); 
     $rcv = fgets($connect, 1024); 
   fputs($connect, "RSET\r\n"); 
     $rcv = fgets($connect, 1024); 
 }
 fputs ($connect, "QUIT\r\n"); 
   $rcv = fgets ($connect, 1024); 
 fclose($connect);
 ini_restore(sendmail_from);
}
?>
peoples
Forum Newbie
Posts: 2
Joined: Thu Mar 11, 2004 1:13 pm

Post by peoples »

thx m8, but I am running my Apache on Win2k and the mail function does not work with my default SMTP-server.
How can I run my own SMTP-server to make the mailing work.
Please not to difficult 'cause I'm just a newbie at this all.
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

peoples wrote:thx m8, but I am running my Apache on Win2k
Ah, Windows.

It looks like you can set your SMTP server in php.ini. It looks as if it is set to "localhost" by default, but you could change it to "mail.planetinternet.be".

Will it work? I really haven't a clue. I'm just digging around in the help. You might find this section helpful:

http://www.php.net/manual/en/ref.mail.php

Sorry I can't be more help.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

peoples wrote:thx m8, but I am running my Apache on Win2k and the mail function does not work with my default SMTP-server.
How can I run my own SMTP-server to make the mailing work.
Please not to difficult 'cause I'm just a newbie at this all.
Congrats on being the 10,000th user to register on this forum! :D :wink:
Post Reply