# Php Mail Function+SMTP Authentication



## Alien56

I want to start using the mail function with php, but the mail server I'm using from verizon requires authentication for sending mail, so I'm wondering what do I need to do to get the mail function working with SMTP authentication?


----------



## Rockn

Does the web host allow fo SMTP through the web server? You don't need a mail server to send mail.


----------



## Alien56

I'm not sure and I didn't realize I didn't need a mail server. So if I don't need a mail server to send mail how do I do it then? Because in the php.ini file for SMTP I put outgoing.verizon.net and for the other required field I put my email address, restarted apache, and it didn't work so what should I do?


----------



## Rockn

Check with your web host to see if the server allows sending SMTP directly from the server. All you would need to do then is either enter an IP address or your web server URL.


----------



## Alien56

I'm not on a web server right now. Everything I'm doing is testing and is at the localhost. So I'm not actually using the internet just yet. I'm just trying to send a message to my email, using php but without being on the internet for testing purposes.


----------



## DrP

I had a quick go at that but found the server on the internet worked straight away so didn't follow it up.
I remember there were 3 things I had to modify in the php.ini file on my own machine:
SMTP: (set it to localhost?)
smtp_port: (the port PHP uses to connect to the SMTP server)
sendmail_from: (the From address)
Is that any help? As I said, I found it easier to use the proper server on the web.


----------



## Rockn

Testing locally will require authentication by the server if it isn't on your network.


----------



## brendandonhu

mail() doesn't support SMTP authentication at all. You can use a 3rd party class like the PEAR package: http://pear.php.net/package/Mail
Or you could send the email manually by using fsockopen() to connect to the mailserver, and fwrite() to send the data.


----------



## Alien56

How do I set up Pear? Also can you give a good example of the usage of fsockopen() and fwrite()?


----------



## brendandonhu

Here's an example of SMTP auth using fsockopen(): http://codewalkers.com/seecode/538.html


----------



## Alien56

So this should work for smtp servers that require user authentication? So if I try to send mail with all the correct information filled in like my username and password at verizon I should see mail at that account?


----------



## brendandonhu

Correct


----------



## Alien56

Thanks a lot. I'll try it out when I get the chance.


----------



## Alien56

I put in all the right information (information provided by my ISP) and all I got was a blank page and nothing in my inbox. Whats wrong?


----------



## brendandonhu

Can you post the code you have? Just edit out your password.


----------



## Alien56

Sure here it is:

<?php 
//new function

$to = "[email protected]"; 
$nameto = "Who To"; 
$from = "[email protected]"; 
$namefrom = "Who From"; 
$subject = "Hello World Again!"; 
$message = "World, Hello!" 
authSendEmail($from, $namefrom, $to, $nameto, $subject, $message); 
?>

<?php 
/* * * * * * * * * * * * * * SEND EMAIL FUNCTIONS * * * * * * * * * * * * * */

//Authenticate Send - 21st March 2005 
//This will send an email using auth smtp and output a log array 
//logArray - connection,

function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message) 
{ 
//SMTP + SERVER DETAILS 
/* * * * CONFIGURATION START * * * */ 
$smtpServer = "outgoing.verizon.net"; 
$port = "25"; 
$timeout = "30"; 
$username = "[email protected]"; 
$password = "mypassword"; 
$localhost = "localhost"; 
$newLine = "\r\n"; 
/* * * * CONFIGURATION END * * * * */

//Connect to the host on the specified port 
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout); 
$smtpResponse = fgets($smtpConnect, 515); 
if(empty($smtpConnect)) 
{ 
$output = "Failed to connect: $smtpResponse"; 
return $output; 
} 
else 
{ 
$logArray['connection'] = "Connected: $smtpResponse"; 
}

//Request Auth Login 
fputs($smtpConnect,"AUTH LOGIN" . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['authrequest'] = "$smtpResponse";

//Send username 
fputs($smtpConnect, base64_encode($username) . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['authusername'] = "$smtpResponse";

//Send password 
fputs($smtpConnect, base64_encode($password) . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['authpassword'] = "$smtpResponse";

//Say Hello to SMTP 
fputs($smtpConnect, "HELO $localhost" . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['heloresponse'] = "$smtpResponse";

//Email From 
fputs($smtpConnect, "MAIL FROM: $from" . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['mailfromresponse'] = "$smtpResponse";

//Email To 
fputs($smtpConnect, "RCPT TO: $to" . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['mailtoresponse'] = "$smtpResponse";

//The Email 
fputs($smtpConnect, "DATA" . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['data1response'] = "$smtpResponse";

//Construct Headers 
$headers = "MIME-Version: 1.0" . $newLine; 
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine; 
$headers .= "To: $nameto <$to>" . $newLine; 
$headers .= "From: $namefrom <$from>" . $newLine;

fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n"); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['data2response'] = "$smtpResponse";

// Say Bye to SMTP 
fputs($smtpConnect,"QUIT" . $newLine); 
$smtpResponse = fgets($smtpConnect, 515); 
$logArray['quitresponse'] = "$smtpResponse"; 
} 
?>


----------



## brendandonhu

I think you need to change $from because Verizon only delivers messages that are from addresses @verizon.net

Also, I don't think you need to put the @verizon.net in $username


----------



## Alien56

Yeah that didn't work. Any other suggestions? Does gmail have one or not?


----------



## brendandonhu

Its smtp.gmail.com but it requires TLS so I'm not sure if it will work. If you just need this for testing, you can just install a mail server on your computer.


----------



## Alien56

Do you recommend any mail servers? If I install my own mail server for testing I will not have to worry about having to connect to a mail server so send mail?


----------



## brendandonhu

I haven't tried it but this should work: http://www.softstack.com/freesmtp.html
Your messages may be marked as spam by recipients but it should work well enough for testing.


----------



## brendandonhu

Also, there's a class here that seems to work with Gmail's server: http://www.phpclasses.org/browse/package/14.html


----------



## Alien56

Thanks. BTW what should I fill in for SMTP in the php.ini for this to work? Is there anything else I should know on how to set this up besides editing the php.ini?

Offtopic: I'm using charset=iso-8859-1 for my websites character encoding and I was wondering how do I make the ampersand symbol without actually using it because W3C complains about the use of the ampersand when I try to validate it if its just left in the document as "&" and not the encoding value ie: &amp; or something.


----------



## brendandonhu

If you're installing your own SMTP server it would be localhost in php.ini. If you're using another class to send the email you don't need to change php.ini

Ampersand is &amp;


----------



## Alien56

I'm using the free SMTP server you linked me to. How do I use that to send mail to my email address form a form in php? I setup the php.ini to have SMTP= localhost and the port on 50 because I specified 50 in the program as the port number to use.


----------



## brendandonhu

Just stick the settings in your php.ini:
[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 50
sendmail_from = [email protected]

That SMTP server wasn't working for me though so I ended up using this one and it worked: http://www.postcastserver.com/download/release.aspx?p=3


----------



## Alien56

THANK YOU!!!!!!!

I finally got it to work.


----------



## brendandonhu

[tsg=yourewelcome]yourewelcome[/tsg]


----------



## stevend

Hi all,

Great post... but Alien56... did you get this server to work with your php script?

Thank you for your time,

Steve


----------

