Send mail by smtp server gmail


#1

[php]<?php
$database = ‘./usersdb.php’;
$success_page = ‘./enviado.php’;
$error_message = “”;
if (!file_exists($database))
{
die(‘User database not found!’);
exit;
}
if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’ && $_POST[‘form_name’] == ‘signupform’)
{
$newusername = $_POST[‘username’];
$newemail = $_POST[‘email’];
$newpassword = $_POST[‘password’];
$confirmpassword = $_POST[‘confirmpassword’];
$newfullname = $POST[‘fullname’];
$website = $SERVER[‘HTTP_HOST’];
$script = $SERVER[‘SCRIPT_NAME’];
$timestamp = time();
$code = md5($website.$timestamp.rand(100000, 999999));
if ($newpassword != $confirmpassword)
{
$error_message = ‘Password and Confirm Password are not the same!’;
}
else
if (!ereg("^[A-Za-z0-9
!@$]{1,50}$", $newusername))
{
$error_message = ‘Username is not valid, please check and try again!’;
}
else
if (!ereg("^[A-Za-z0-9
!@$]{1,50}$", $newpassword))
{
$error_message = ‘Password is not valid, please check and try again!’;
}
else
if (!ereg("^[A-Za-z0-9
!@$.’ &]{1,50}$", $newfullname))
{
$error_message = ‘Fullname is not valid, please check and try again!’;
}
else
if (!ereg("^.+@.+…+$", $newemail))
{
$error_message = ‘Email is not a valid email address. Please check and try again.’;
}
$items = file($database, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($items as $line)
{
list($username, $password, $email, $fullname) = explode(’|’, trim($line));
if ($newusername == $username)
{
$error_message = ‘Username already used. Please select another username.’;
break;
}
}
if (empty($error_message))
{
$file = fopen($database, ‘a’);
fwrite($file, $newusername);
fwrite($file, ‘|’);
fwrite($file, md5($newpassword));
fwrite($file, ‘|’);
fwrite($file, $newemail);
fwrite($file, ‘|’);
fwrite($file, $newfullname);
fwrite($file, ‘|0|’);
fwrite($file, $code);
fwrite($file, “\r\n”);
fclose($file);
$subject = ‘Your new account’;
$message = ‘A new account has been setup.’;
$message .= “\r\nUsername: “;
$message .= $newusername;
$message .= “\r\nPassword: “;
$message .= $newpassword;
$message .= “\r\n”;
$message .= “\r\nhttp://”.$website.$script.”?user=”.$newusername.”&code=$code”;
$header = “From: webmaster@yourwebsite.com”."\r\n";
$header .= “Reply-To: webmaster@yourwebsite.com”."\r\n";
$header .= “MIME-Version: 1.0”."\r\n";
$header .= “Content-Type: text/plain; charset=utf-8”."\r\n";
$header .= “Content-Transfer-Encoding: 8bit”."\r\n";
$header .= “X-Mailer: PHP v”.phpversion();
mail($newemail, $subject, $message, $header);
header(‘Location: ‘.$success_page);
exit;
}
}
else
if (isset($_GET[‘code’]) && isset($_GET[‘user’]))
{
$found = false;
$items = file($database, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($items as $line)
{
list($username, $password, $emailaddress, $fullname, $active, $code) = explode(’|’, trim($line));
if ($username == $_GET[‘user’] && $code == $_GET[‘code’])
{
$found = true;
}
}
if ($found == true)
{
$file = fopen($database, ‘w’);
foreach($items as $line)
{
$values = explode(’|’, trim($line));
if ($_GET[‘user’] == $values[0])
{
$values[4] = “1”;
$values[5] = “NA”;
$line = ‘’;
for ($i=0; $i < count($values); $i++)
{
if ($i != 0)
$line .= ‘|’;
$line .= $values[$i];
}
}
fwrite($file, $line);
fwrite($file, “\r\n”);
}
fclose($file);
}
else
{
die (‘User not found!’);
}
header(“refresh:5;url=”.basename(FILE));
echo ‘Your user account was succesfully activated. You’ll be redirected in about 5 secs. If not, click here.’;
exit;
}
?>[/php]

i got this generated by a program.

but my webhosting doesnt support sending mails at least it is smtp

so i need to edit this to use SMTP and i gont no idea of php please help me!


#2

Really? Who is your hosting company? Usually standard PHP will send it easily.

I think it is a header error. I do not see any TO: address… Here is how I send it.
$email_to = "JohnDoe@mydomain.com";
$email_from = "ErnieAlex@someplace.com";
$email_subject = “Test email…”;
$mail_message .= "Email was sent on " . $current_month . " " . $current_date . ", " . $current_year . " at " . $current_time; //just something for showing you…

// create email headers
$headers = 'From: ' . $email_from . "\r\n" . 'Reply-To: ' . $email_from . "\r\n" . 'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);

I read that the headers can be in a different order, but, I did have trouble once before with them.
If you want to use SMTP, you can use the hosting companies if they give you the info or you can use your own. I normally do it without SMTP because I do not like putting my SMTP info out in the public… Anyway, here is a link that talks a lot about this and gives the code needed to do it. Hope that helps with this project:
http://www.dreamincode.net/forums/topic/36108-send-emails-using-php-smtp-direct/