Hi!

I have some forms that i have used to send to my e-mail using the php mail() function. I have had som issues with it because the mailservice i am using is not on the same server as the domain, so now i am forced to change the way i send e-mails(if i dont want to set ut an intirely new mail on that server).

Someone gave me a tip about the smpt authentication and pear mail, after reading about it i decided that this i something i can use. I have searched for post about setting up souch a form but i can't find anyting that describes my problem. I have a problem with structuring the script and now i am in a bit of a time crounch. This is the code that i use now with the mail() function.

<form action="" method="POST" class='kredittkortform'>
<table class='kreditttable'>
<tr>
<td colspan="2"><b>Personopplysninger:</b></td>
</tr>
<tr>
 <td>Fødselsnummer:</td>
   <td><input type='text' name='personnummer' value="<?php echo      isset($_POST["personnummer"])? $_POST["personnummer"] : ''; ?>" /></td>
</tr>

<tr>
 <td>Fullt navn:</td>
   <td><input type='text' name='navn' value="<?php echo isset($_POST["navn"])?  $_POST["navn"] : ''; ?>"  /></td>
</tr>
<tr>
		
<td><input type='submit' name='submit' value='Send søknad' style='height: 25px; width: 110px;margin-left:60px;' /></td>
</tr>

</form>

<?php 
 if(isset($_POST["submit"])){
  
  $besked = "<b>Personopplysninger:</b> <br />";
  $besked .= "Personnummer: ". $_POST["personnummer"]."<br />";
  $besked .= "Navn: ". $_POST["navn"]."<br />";
  
  $to2 = "mymail@myadress.com";
  $to = "myothermail@myadress.com";
  
  $subject = "Hei";
  $message = "Navn: " . isset($_SESSION["navn"]) . "<br />";
  $message .= "Email: " . isset($_SESSION["epost"]) . "<br /><br />";
  $message .= $besked;
  $headers = 'MIME-Version: 1.0' . "\r\n"; 
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  $headers .= "Fra:".isset($_SESSION["epost"]);
  $headers .= "\nReply-To:". isset($_SESSION["epost"]);
  $sentOk = mail($to,$subject,$message,$headers);
  $sentOk2 = mail($to2,$subject,$message,$headers);
 
  mysql_connect('***', '***', '***') or die(mysql_error());;
  //echo "Connected to MySQL<br />";
  mysql_select_db('***') or die(mysql_error());;
  //echo "Connected to Database";
  
  $email = isset($_SESSION["epost"]);
  
  mysql_query("INSERT INTO email (email, kilde) VALUES ('$email', 'Hei')");
  echo "<span style='color:red;font-size:30px;'>Takk for din søknad. Du vil bli kontaktet  innen kort tid.</span>";
 }
  
 ?>
  </div>
</body>
</html>

I have managed to make a simple form that sends the mail right when i open the page, but i need it to send the mail when te submit button is pushed. And i have not found out where and how to place the information regarding the mySQL server. Can anyone help me with this? It would be much appriciated!

Thanks in advance.

Dani AI

Generated

Short, practical checklist and fixes for (building on and ): the most likely problems are (1) the script is running the send code on every page load because the POST check is missing or using the wrong variable, (2) isset() is being used where the form value should be read (it returns a boolean, not the field value), and (3) DB credentials and the old mysql_* calls need modernizing. See the PHP docs for mail() and for how request variables behave. (php.net)

Example pattern to run a PEAR::Mail send only on submit (validate first, then use the SMTP backend). The snippet below shows the control-flow and Mail usage (replace host/credentials and use secure ports):

<?php
require_once 'Mail.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $from = filter_input(INPUT_POST, 'epost', FILTER_VALIDATE_EMAIL);
    $to   = 'recipient@example.com';
    $sub  = 'Hei';
    $body = '<b>Personopplysninger:</b><br />...';

    $headers = [
      'From' => $from,
      'To'   => $to,
      'Subject' => $sub,
      'Content-Type' => 'text/html; charset=UTF-8'
    ];

    $params = [
      'host' => 'smtp.example.com',
      'port' => 587,
      'auth' => 'PLAIN',
      'username' => 'smtp-user',
      'password' => 'smtp-pass',
      'debug' => false
    ];

    $smtp = Mail::factory('smtp', $params);
    $result = $smtp->send($to, $headers, $body);
    if (PEAR::isError($result)) {
       error_log($result->getMessage());
    } else {
       echo 'Message queued.';
    }
}
?>

PEAR Mail expects headers as an associative array and the SMTP params shown above; many providers publish similar examples. Use the debug option while troubleshooting. (authsmtp.com)

Store DB credentials outside the page (environment variables or a config file not in webroot) and stop using mysql_* — migrate to PDO or mysqli with prepared statements to avoid SQL injection. Example: open a PDO connection, prepare the INSERT, bind values and execute. The PDO docs and OWASP guidance describe this approach and why it is safer. (php.net)

Extra notes: isset($_SESSION['epost']) returns true/false — use $_POST['epost'] (or filter_input) to get the email value. mail() needs a proper From: header and is not a drop-in replacement for SMTP-authenticated sending. If PEAR becomes awkward, consider modern libraries like PHPMailer which simplify SMTP, TLS and authentication. Also, ext/mysql was deprecated/removed in later PHP versions — migrate now. (php.net)

Maybe you server config have something mistake.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.