Jump to content

[SOLVED] mail function not sending mail


neridaj

Recommended Posts

Hello,

 

I have a function that sends an invoice to a customer, which for some reason has stopped sending the email. I have two functions, one which inserts the invoice data into a database table and one that emails the invoice. I have these functions wrapped in try catch blocks and when they are run both of them give the success message, but only the insert function is actually working.

 

  // create short vars
  $first = $_POST['first'];
  $last = $_POST['last'];
  $user = $_POST['user'];
  $agent = $_POST['agent'];
  $email = $_POST['email'];
  $description = $_POST['description'];
  $invnum = $_POST['invnum'];
  $amount = $_POST['amount'];
  $date = date("Ymd");

  // insert invoice into db
  try
  {
    add_invoice($invnum, $user, $amount, '', '', $description, $date);
    echo 'Invoice has been created.<br />';
  }
  catch (Exception $e)
  {
    echo 'Invoice could not be created - please try again later.';
    exit();
  }
  // email invoice
  try
  {
    send_invoice($first, $last, $user, $agent, $email, $invnum, $amount);
    echo 'Invoice has been sent.<br />';
  }
  catch (Exception $e)
  {
    echo 'Invoice could not be sent - please try again later.';
    exit();
  }

 

Here are the function definitions:

 

function add_invoice($invnum, $user, $due, $paid, $balance, $description, $date)
{
  // Add new invoice to the database
  $conn = db_connect();

  // check not a repeat invoice
  $result = $conn->query("select * from invoice where inv_number='$invnum'");
  if ($result && ($result->num_rows>0))
    throw new Exception('Invoice number already exists.');

  // insert the new invoice
  if (!$conn->query( "insert into invoice values
                          ('$invnum', '$user', '$due', '$paid', '$balance', '$description', '$date')"))
    throw new Exception('Invoice could not be inserted.'); 

  return true;
}

function send_invoice($first, $last, $agent, $email, $invnum, $amount)
// send client the invoice
{
      $from = "From: [email protected] \r\n";
      $mesg = "Your invoice is listed below. \r\n"
              ."Firs Name: $first \r\nLast Name: $last \r\nAgent Name: $agent \r\nEmail: $email \r\nInvoice Number: $invnum \r\nAmount $$amount \r\n";
      
      
      if (mail($email, 'Subject', $mesg, $from))
        return true;      
      else
        throw new Exception('Could not send email.');
}

 

Thanks,

 

Jason

Link to comment
https://forums.phpfreaks.com/topic/144985-solved-mail-function-not-sending-mail/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.