View Full Version : php mail to external server?

09-01-2006, 04:50 PM
I'm wondering, is there a way to send php email to an external smtp server, and if so how??

thanks in advance for your time!

09-01-2006, 05:01 PM
you dont need a external smtp server to send mail with php use this


$ToEmail = "youremail";

$ToSubject = "testing mail";

$EmailBody = "hi! It worked!!";

mail($ToName." <".$ToEmail.">",$ToSubject, $EmailBody, "From: ".$FirstName." <".$Email.">");


09-01-2006, 05:06 PM
ok, i know all about the mail function, but thats not what I'm looking for.
I want to use an external smtp server.


09-01-2006, 05:13 PM
try this


$from = " Sender <sender@example.com>";
$to = "Recipient <recipient@example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_password";

$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'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>");

09-01-2006, 05:57 PM
I also found this function on php.net:

// modified to provide authenticated logins
function mymail($to,$subject,$message,$headers)

// set as global variable
global $GLOBAL;

// get From address
if ( preg_match("/From:.*?[A-Za-z0-9\._%-]+\@[A-Za-z0-9\._%-]+.*/", $headers, $froms) ) {
preg_match("/[A-Za-z0-9\._%-]+\@[A-Za-z0-9\._%-]+/", $froms[0], $fromarr);
$from = $fromarr[0];

// Open an SMTP connection
$cp = fsockopen ($GLOBAL["SMTP_SERVER"], $GLOBAL["SMTP_PORT"], &$errno, &$errstr, 1);
if (!$cp)
return "Failed to even make a connection";
if(substr($res,0,3) != "220") return "Failed to connect";

// Say hello...
fputs($cp, "HELO ".$GLOBAL["SMTP_SERVER"]."\r\n");
if(substr($res,0,3) != "250") return "Failed to Introduce";

// perform authentication
fputs($cp, "auth login\r\n");
if(substr($res,0,3) != "334") return "Failed to Initiate Authentication";

fputs($cp, base64_encode($GLOBAL["SMTP_USERNAME"])."\r\n");
if(substr($res,0,3) != "334") return "Failed to Provide Username for Authentication";

fputs($cp, base64_encode($GLOBAL["SMTP_PASSWORD"])."\r\n");
if(substr($res,0,3) != "235") return "Failed to Authenticate";

// Mail from...
fputs($cp, "MAIL FROM: <$from>\r\n");
if(substr($res,0,3) != "250") return "MAIL FROM failed";

// Rcpt to...
fputs($cp, "RCPT TO: <$to>\r\n");
if(substr($res,0,3) != "250") return "RCPT TO failed";

// Data...
fputs($cp, "DATA\r\n");
if(substr($res,0,3) != "354") return "DATA failed";

// Send To:, From:, Subject:, other headers, blank line, message, and finish
// with a period on its own line (for end of message)
fputs($cp, "To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
if(substr($res,0,3) != "250") return "Message Body Failed";

// ...And time to quit...
if(substr($res,0,3) != "221") return "QUIT failed";

return true;

09-01-2006, 06:19 PM
let me know if my code works :)

09-01-2006, 06:21 PM
your code *looks* like it should work, though I'm using the function that I posted it has a little more functionality for my needs.


09-02-2006, 03:46 PM

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum