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

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?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

john-m-calvertAuthor Commented:
Thanks for your reply. I'm aware of this link and 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= ?
Just add these two lines into your PHP.INI file


Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

does this really work....
Richard QuadlingSenior Software DeveloperCommented:
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);


So, basically not supported.

Richard QuadlingSenior Software DeveloperCommented:
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?
john-m-calvertAuthor Commented:
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?
Richard QuadlingSenior Software DeveloperCommented:
Source of PHP.

You can download it from (or the mirrors).

When you unpack the source, look in ...


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.

$smtp_server = "";
$port = 25;
$mydomain = "";
$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 = "";
$recipient = "";
$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");

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
john-m-calvertAuthor Commented:
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:

john-m-calvertAuthor Commented:
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.
Richard QuadlingSenior Software DeveloperCommented:
Can you share the PHP Source mods? Or have you passed it up to cvs?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.