?
Solved

php.ini & SMTP= - how do you pass username & password

Posted on 2003-03-12
12
Medium Priority
?
8,742 Views
Last Modified: 2013-12-13
My ISP account requires that I send a username & password for outbound SMTP mail. How do I get PHP to use this when executing php.mail()? The php.ini file only contains entries for the server (SMTP= ) and From: (sendmail_from= ).

I suppose that I could run a localhost SMTP service which itself doesn't need username/password but which knows how to talk with my ISP and supply username/password, but this seems like overkill. Can't PHP send this directly?

John
0
Comment
Question by:john-m-calvert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 6

Expert Comment

by:carchitect
ID: 8121716
0
 

Author Comment

by:john-m-calvert
ID: 8122279
Thanks for your reply. I'm aware of this link and http://www.php.net/manual/en/function.mail.php as well, neither of which mentions how to specify a username / password for outgoing SMTP mail. The php.ini has settings for host and port (SMTP= and SMTP_port= ) but what about SMTP_user= and SMTP_pwd= ?
0
 
LVL 1

Expert Comment

by:pcaylor
ID: 8122854
Just add these two lines into your PHP.INI file

SMTP_user=username
SMTP_pwd=xxxxx

-Peter
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:carchitect
ID: 8125679
does this really work....
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 8127223
Not according to the source.

No mention of smtp_user or smtp_pwd.

Also, the source has no code to deal with logons for smtp servers.

It does ...


sprintf(Buffer, "HELO %s\r\n", LocalHost);
snprintf(Buffer, MAIL_BUFFER_SIZE, "MAIL FROM:<%s>\r\n", RPath);
snprintf(Buffer, MAIL_BUFFER_SIZE, "RCPT TO:<%s>\r\n", token);

No USER or PASS.

So, basically not supported.

Richard.
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 8127253
A solution would be to do the whole job manually with sockets.

That way you are in TOTAL control. But that would be wheel re-invention.

Do any of the mail classes (MIME_HTML, etc) support secured SMTP access?
0
 

Author Comment

by:john-m-calvert
ID: 8127384
Thank you Richard for that informed answer. My original mention of "SMTP_user=" and "SMTP_pwd=" was pure wishful thinking, although perhaps that wasn't clear to the other respondents.

Could you add one more detail to your reply? You mention referencing the source code. Source for what module exactly, and where can mere mortals view this?
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 8127417
Source of PHP.

You can download it from www.php.net (or the mirrors).

When you unpack the source, look in ...

php-4.3.1\win32\sendmail.c

Richard.
0
 
LVL 1

Accepted Solution

by:
pcaylor earned 60 total points
ID: 8129301
Ok, this code should do the trick.  It manages the SMTP connection via sockets, so you can add or remove authentication as you need it.

<?php
$smtp_server = "smtp.acme.com";
$port = 25;
$mydomain = "acme.com";
$username = "user"; // MS Exchange servers will probably require a
valid NT domain name as part of the username.  E.g., "ntdomain\user"
$password = "password";
$sender = "me@acme.com";
$recipient = "joe@company.com";
$subject = "test";
$content = "test";

// Initiate connection with the SMTP server
$handle = fsockopen($smtp_server,$port);
fputs($handle, "EHLO $mydomain\r\n");

// SMTP authorization
fputs($handle, "AUTH LOGIN\r\n");
fputs($handle, base64_encode($username)."\r\n");
fputs($handle, base64_encode($password)."\r\n");

// Send out the e-mail
fputs($handle, "MAIL FROM:<$sender>\r\n");
fputs($handle, "RCPT TO:<$recipient>\r\n");
fputs($handle, "DATA\r\n");
fputs($handle, "To: $recipient\n");
fputs($handle, "Subject: $subject\n\n");
fputs($handle, "$content\r\n");
fputs($handle, ".\r\n");

// Close connection to SMTP server
fputs($handle, "QUIT\r\n");
?>
0
 

Author Comment

by:john-m-calvert
ID: 8132221
Thank you pcaylor for this sample PHP script. It works great.

I'm a little rusty with my C, and I'm totally new to PHP, but if I'm reading the PHP C source code correctly, the critical function is SendText()in php-4.3.1\win32\sendmail.c which has the same socket conversation as the above PHP sample. So, if an ambitious C programmer wants to extend PHP to offer an authenticated version of mail(), that would be super duper! I'm not quite there yet as I don't have the appropriate build environment for the PHP C source code, but maybe next month ;)

All that is needed is a few lines of C and a convention as to where to retrieve the usr/pwd from in order to send the SMTP command and reply on the socket:
AUTH
username
password

John
0
 

Author Comment

by:john-m-calvert
ID: 8420205
Following pcaylor's PHP example SMTP socket code I was able to hack the PHP source code in C and add authentication to the mail() function. Thanks again for pointing me in the right direction.
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 8420308
Can you share the PHP Source mods? Or have you passed it up to cvs?
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question