Email activation smtp authentication

Hi ,
I have this script through which I would like to send an automatic email to clients upon registration, but the email is never sent. I am using my hostmonster’s Outgoing Mail Server: mail.ciudadanossinfronteras.com.
However when I go into hostmonster and check email settings it says: Outgoing Mail Server: mail.ciudadanossinfronteras.com (server requires authentication) port 26.

Can anyone let me know what I can do to authenticate my outgoing mail server?

PHP script:


<?php 
include ('connection.php');
if($submit)


{
  

$username = strtolower(strip_tags($_POST['username']));
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
$firstname = $_POST['inputfirstname'];
$lastname = $_POST['inputlastname'];
$desc = $_POST['inputdesc']; 
$email = $_POST['inputemail']; 
$email_confirm = $_POST['inputemail_confirmation'];
$phone = $_POST['inputphone']; 
$skype = $_POST['inputskype']; 
$house = $_POST['inputhousenumber']; 
$street = $_POST['inputstreet']; 
$city = $_POST['inputcity']; 
$state = $_POST['inputstate']; 
$country = $_POST['inputcountry']; 
$status = $_POST['inputstatus']; 
$period = $_POST['inputperiod']; 
$field = $_POST['inputfield'];
$language = $_POST['inputlanguage'];


$errorstring = "";

//default value of error string 
if (!$firstname ||(0 === preg_match("/\\S+/", $_POST['inputfirstname']))) $errorstring = $errorstring."<span style='color: rgb(204, 0, 0);'>Name<br></span>"; 
if (!$lastname ||(0 === preg_match("/\\S+/", $_POST['inputlastname']))) $errorstring = $errorstring."<span style='color: rgb(204, 0, 0);'>Lastname<br></span>"; 
if (!$email) $errorstring = $errorstring."<span style='color: rgb(204, 0, 0);'>Email<br></span>"; 
if ($status == "emergency"&& !$phone) $errorstring = $errorstring."<span style='color: rgb(204, 0, 0);'>Phone number is required for emergency assistance <br></span>";
if (preg_match('/[a-z]+/i', $_POST['inputphone'])) $errorstring = $errorstring."<span style='color: rgb(204, 0, 0);'>Phone number cannot contain letters<br></span>";
if (!$desc) $errorstring = $errorstring."<span style='color: rgb(204, 0, 0);'>Description<br></span>";
if ($country=="---" || $country=="Africa" || $country=="Europe" || $country=="Central-America" || $country=="North-America" || $country=="South-America" || $country=="Asia" || $country=="Middle-East")
    $errorstring = $errorstring."<span style='color: rgb(204, 0, 0);'>Country<br></span>";
if (!$language ||(0 === preg_match("/\\S+/", $_POST['inputlanguage'])))
    $errorstring = $errorstring."<span style='color: rgb(204, 0, 0);'>Languages spoken<br></span>";
if ($field=="---") $errorstring = $errorstring."<span style='color: rgb(204, 0, 0);'>Field of interest<br></span>";
if ($status=="---") $errorstring = $errorstring."<span style='color: rgb(204, 0, 0);'>Urgency of assistance<br></span>"; 
if ($period=="---") $errorstring = $errorstring."<span style='color: rgb(204, 0, 0);'>Period of time your information will stay posted<br></span>"; 
if (0 !==strcmp($_POST['inputemail'], $_POST['inputemail_confirmation'])) $errorstring = $errorstring."<span style='color: rgb(204, 0, 0);'>Emails do not match.</span>"; 

$namecheck = mysql_query("SELECT username FROM mainweb WHERE username='$username'");
  $count = mysql_num_rows($namecheck);

if (!$count==0)
      $errorstring = $errorstring."<span style='color: rgb(204, 0, 0);'>Username already taken!<br></span>";

if($username&&$password&&$repeatpassword)
  {
    if ($password==$repeatpassword)
    {
    if (strlen($username)>25)
                             {echo "Length of username is too long";}
       else {
      if (strlen($password)>25||strlen($password)<6)
                 {
                   echo "password must be between 6 and 25 characters";
                 }
                        else
                        {
                          //register the user 


                                //open database
                                   if ($errorstring!="")
echo "<u>Please fill out the following fields</u>:<br><br>$errorstring";

else { $password = md5($password);
                   $repeatpassword = md5($repeatpassword);
                   //generate random number for activation process
                     $random = rand(23456789,98765432);

  mysql_query ("INSERT INTO mainweb (`id`,`date`,`firstname`,`lastname`,`description`,`email`,`phonenumber`,`skype`,`house`,`street`,`city`,`state`,`country`,`soa`,`periodoftime`,`field`,`languages`,`username`,`password`,`random`,`activated`)
                   VALUES(NULL,NOW(),'$firstname','$lastname','$desc','$email','$phone','$skype','$house','$street','$city','$state','$country','$status','$period','$field','$language','$username','$password','$random','0')") or die(mysql_error());

                 [COLOR="Red"] $lastid = mysql_insert_id();

                   //send activation email
                   $to = $email;
                   $subject = "Activate your account";
                   $headers = "From: adamschroder@asia.com";
                   $server = "mail.ciudadanossinfronteras.com";

                   ini_set("SMTP",$server);

                   $body="
                   Hello $firstname,\
\

                   You need to activate your account with the link below:/n/n
                   
                   http://ciudadanossinfronteras.com/activate.php?id=$lastid&code=$random
                   ";
                  [/COLOR]
                   //function to send email
                   mail($to, $subject, $body, $headers);

                   echo"<meta http-equiv='refresh' content='0;URL=http://ciudadanossinfronteras.com/thankyouhost.html' />"; 
     }

                        }

            }

    }
    else
        echo "Your passwords do not match";

       }
  else
      echo "Please fill out all necessary fields!";

}
?>

PHP mail() command does not support authentication.

Use the PEAR mail package

Ya, I tried pear to authenticate smtp but it didn’t work.

I must be doing something wrong.

Which of the pear files do I have to upload to my websites’s server before testing my php code? I tried uploading pear.php and mail.php.

You need to include mail.php in your script too.

Heres a good tut.

Easier mail sending with PEAR::Mail | Practical PHP Programming | TuxRadar Linux

Check out Swift Mailer too, it’s worth a look and standalone. :slight_smile:

I tried the easier mail sending link you gave me.
I uploaded mail.php to my website’s server
and ran the following code: but it gave me this error: Call to undefined method PEAR_Error::send() in /home2/ciudada2/public_html/example.php on line 7

code:

<?php
    include('Mail.php');
    $mail = Mail::factory("mail");

    $headers = array("From"=>"adam@asia.com", "Subject"=>"Test Mail");
    $body = "This is a test!";
    $mail->send("adamschroeder@asia.com", $headers, $body);
?>

why do I keep on getting that error?
do I need to upload any other files to my website’s server"

Anthony,
the swift mailer you proposed doesn’t seem to have smtp authentication capabilities. At least not in their guide, and I wouldn’t know how to do it on my own.

thanks any way.

Yes, it does. It’s even on the front page of the site in big black letters. :stuck_out_tongue:

Here’s a link to the documentation. :wink:

Ok, thankx I got your link.

I’ll try it out.

After having downloaded the swift mailer, I tried to send an email to my own private email, but it wouldn’t work. I could only get it to send email to my web site hots’s email account (admin@ciudanossinfronteras.com).

Do you have any idea why?

this is the code I used:


<?php

require_once 'lib/swift_required.php';

//Create the Transport
$transport = Swift_SmtpTransport::newInstance('mail.ciudanossinfronteras.com', 26)
  ->setUsername('admin@ciudadanossinfronteras.com')
  ->setPassword('******')
  ;

//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

//Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('duke2kill@hotmail.com' => 'Adam schroeder'))
  ->setTo(array('admin@ciudanossinfronteras.com', 'other@domain.org' => 'Ben'))
  ->setBcc(array('adamschroeder@mail.com', 'person2@otherdomain.org' => 'Adam'))
  ->setBody('This is what I said yesterday')
  ;



//Send the message
$result = $mailer->send($message);

?>

//in your pear_mail_script.php do the following.
<?PHP
require_once(“Mail.php”);

$paramsSMTP = array(
		"host" =&gt; "smtp.yourhost.co.za",
		"auth" =&gt; true,
		"username" =&gt; "youremailwithhost.co.za",
		"password" =&gt; "somepassword"

);
	$mail = Mail::factory("smtp", $paramsSMTP);

?>

// in your registration form include this.
//…all your other stuff here and then the following.
$user_ip = $_SERVER[‘REMOTE_ADDR’];

// stores sha1 of password
$sha1pass = PwdHash($data[‘pwd’]);

// Automatically collects the hostname or domain like example.com)
$host = $_SERVER[‘HTTP_HOST’];
$host_upper = strtoupper($host);
$path = rtrim(dirname($_SERVER[‘PHP_SELF’]), ‘/\\’);

// Generates activation code simple 4 digit number
$activ_code = rand(1000,9999);

$usr_email = $data[‘usr_email’];
$usr_email = strtolower($usr_email);
$user_name = $data[‘user_name’];
$user_name = ucwords(strtolower($user_name));

$sql_insert = "INSERT into users
(full_name,user_email,pwd,activation_code,user_name
)
VALUES
(‘$data[full_name]’,‘$usr_email’,‘$sha1pass’,‘$activ_code’,‘$user_name’
)
";
mysql_query($sql_insert,$link) or die(“Insertion Failed:” . mysql_error());
$user_id = mysql_insert_id($link);
$md5_id = md5($user_id);
mysql_query(“update users set md5_id=‘$md5_id’ where id=‘$user_id’”);
// echo “<h3>Thank You</h3> We received your submission.”;

if($user_registration) {
$a_link = "
ACTIVATION LINK\

http://$host$path/activate.php?user=$md5_id&activ_code=$activ_code
";
} else {
$a_link =
"Your account is PENDING APPROVAL and will be soon activated the administrator.
";
}

$message =
"Hello \

Thank you for registering with us. Here are your login details…\

We require that you validate your password to ensure that you instigated this action. \

This protects against unwanted spam and malicious abuse. Simply click on the link below. \

User ID: $user_name
Email: $usr_email \

Passwd: $data[pwd] \

$a_link

Thank You

Administrator
$host_upper


THIS IS AN AUTOMATED RESPONSE.
DO NOT RESPOND TO THIS EMAIL*
";

require_once(“pear_mail_script.php”);

$recipient = $_POST['usr_email'];
$headers['From']    = "no-reply@someemail.co.za";
$headers['To']      = $_POST['user_name'];
$headers['Subject'] = 'Login Details';
$body = $message;

$mail-&gt;send($recipient, $headers, $body);

header(“Location: thankyou.php”);
exit();

 }

}

?>
i hope this helps.

Adam, I’m trying to do the same thing you are. Did you ever get your initial problem worked out?

Thanks!