mail()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

mail()

Post by NiGHTFiRE »

Hey,
I've done a php codw where people should get emailed from a database and i get this error:
Warning: mail() [function.mail]: SMTP server response: 501 Syntax error in parameters or arguments to MAIL command in D:\www\Apeal\mail\david\mail_send.php on line 59
Emailet skickades till 'david.wibergh@gmail.com'
Notice: Use of undefined constant subject - assumed 'subject' in D:\www\Apeal\mail\david\mail_send.php on line 59
And this is my code:

Code: Select all

<?php 

session_start(); // Alltid överst på sidan 

error_reporting(E_ALL);

 

// Kolla om inloggad = sessionen satt 

if (!isset($_SESSION['sess_user'])){ 

  echo "<script type='text/javascript'>
   document.location.href = 'index.php';
</script>";  

  exit; 

} 

?>

<?php include '../meny.php'; ?>

<?php include '../meny_mail.php'; ?>

<?php

include "../conn.php"; 



#################

#   VARIABLAR   #

#################

$text       = $_POST['meddelande'];

$subject    = $_POST['subject'];

$sender    = $_POST['sender'];

$user      = $_POST['user'];

$datum      = $_POST['datum'];

$tid      = $_POST['tid'];

$NewReplyTo    = $_POST['sender'];

$avser			= 'skarpt';

$tabell_arkiv	= "test_arkiv";

$tabell_epost	= "test_mailadresser";

$message2 = "<a href=\"http://www.nippe.net/test/lindahl/briefings/tabort.php?id=22?databas={$tabell_epost}\">Vill du inte längre få detta nyhetsbrev. Klicka här</a>\r\n"; 

##################

#   /VARIABLAR   #

##################

if(($text == "") || ($subject == "") || ($sender == "")) {

  echo "<script type='text/javascript'>
   document.location.href = 'http://www.apeal.se/mail/mail_form.php';
</script>";

exit;

} else {

$epost = "select epost from $tabell_epost where aktiv='yes'";

$result = mysql_query($epost);

$rows = mysql_fetch_array($result);

// To send HTML mail, the Content-type header must be 

   $MailHeader  = 'MIME-Version: 1.0' . "\r\n";

   $MailHeader .= 'Content-type: text/html; charset=iso-8859-9' . "\r\n";

//fungerar inte  $MailHeader .= 'bcc:' . "\r\n";

   $MailHeader .= 'From: '. $sender. '<'. $sender . '>'. "\r\n"; 

   $MailHeader .= 'Reply-To: '.$NewReplyTo.' <'.$NewReplyTo.'>'. "\r\n";

   $MailHeader .= 'Date: '.date('r'). "\r\n";

   $MailHeader .= 'Message-ID: <'.date('YmdHis').'info@'.$_SERVER['SERVER_NAME'].'>'. "\r\n"; 

   $MailHeader .= 'X-Mailer: PHP/' . phpversion(). "\r\n";

   $MailHeader .= 'X-Priority: 3' . "\r\n";

   //$Mailheader .= 'Return-path: '. $sender. '<'. $sender . '>'. "\r\n";

   $MailHeader .= 'Importance: Normal'."\r\n";

   $message1 = "<a href=\"http://www.nippe.net/test/lindahl/briefings\">Kan du inte läsa detta nyhetsbrev. Klicka här</a><br>\r\n";

while ($row = mysql_fetch_array($result)) {

$email_adrr = $row['epost'];

mail($email_adrr, stripslashes($_POST[subject]), stripslashes($text), $MailHeader);

echo "Emailet skickades till '$email_adrr'";

}

}

?>
I've also looked up the SMTP replies and seen that error 501 means: 501 Syntax error in parameters or arguments
So i've looked if I made the correct mail form and i think i've done it. I've also looked on diffrent places but can't find why.
Thanks a lot and I hope i'm not doing any easy error that's just a waste of your time.
Thanks
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

You need to have single quotes around subject.

Code: Select all

$_POST[subject]
to:

Code: Select all

$_POST['subject']
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Make sure you have < and > around the email address. Some SMTP servers don't like it otherwise.
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

with this code i still get the same errors.

Code: Select all

<?php 

session_start(); // Alltid överst på sidan 

error_reporting(E_ALL);

 

// Kolla om inloggad = sessionen satt 

if (!isset($_SESSION['sess_user'])){ 

  echo "<script type='text/javascript'>
   document.location.href = 'index.php';
</script>";  

  exit; 

} 

?>

<?php include '../meny.php'; ?>

<?php include '../meny_mail.php'; ?>

<?php

include "../conn.php"; 



#################

#   VARIABLAR   #

#################

$text       = $_POST['meddelande'];

$subject    = $_POST['subject'];

$sender    = $_POST['sender'];

$user      = $_POST['user'];

$datum      = $_POST['datum'];

$tid      = $_POST['tid'];

$NewReplyTo    = $_POST['sender'];

$avser			= 'skarpt';

$tabell_arkiv	= "test_arkiv";

$tabell_epost	= "test_mailadresser";

$message2 = "<a href=\"http://www.nippe.net/test/lindahl/briefings/tabort.php?id=22?databas={$tabell_epost}\">Vill du inte längre få detta nyhetsbrev. Klicka här</a>\r\n"; 

##################

#   /VARIABLAR   #

##################

if(($text == "") || ($subject == "") || ($sender == "")) {

  echo "<script type='text/javascript'>
   document.location.href = 'http://www.apeal.se/mail/mail_form.php';
</script>";

exit;

} else {

$epost = "select epost from $tabell_epost where aktiv='yes'";

$result = mysql_query($epost);

$rows = mysql_fetch_array($result);

// To send HTML mail, the Content-type header must be 

   $MailHeader  = 'MIME-Version: 1.0' . "\r\n";

   $MailHeader .= 'Content-type: text/html; charset=iso-8859-9' . "\r\n";

//fungerar inte  $MailHeader .= 'bcc:' . "\r\n";

   $MailHeader .= 'From: '. $sender. '<'. $sender . '>'. "\r\n"; 

   $MailHeader .= 'Reply-To: '.$NewReplyTo.' <'.$NewReplyTo.'>'. "\r\n";

   $MailHeader .= 'Date: '.date('r'). "\r\n";

   $MailHeader .= 'Message-ID: <'.date('YmdHis').'info@'.$_SERVER['SERVER_NAME'].'>'. "\r\n"; 

   $MailHeader .= 'X-Mailer: PHP/' . phpversion(). "\r\n";

   $MailHeader .= 'X-Priority: 3' . "\r\n";

   //$Mailheader .= 'Return-path: '. $sender. '<'. $sender . '>'. "\r\n";

   $MailHeader .= 'Importance: Normal'."\r\n";

   $message1 = "<a href=\"http://www.nippe.net/test/lindahl/briefings\">Kan du inte läsa detta nyhetsbrev. Klicka här</a><br>\r\n";

while ($row = mysql_fetch_array($result)) {

$email_adrr = $row['epost'];

mail("<".$email_adrr.">", stripslashes($_POST['subject']), stripslashes($text), $MailHeader);

echo "Emailet skickades till '$email_adrr'";

}

}

?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You can try Swift (Signature) or PHPMailer (Sourceforge).
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Instead of posting a page of code. it is a lot easier to help if you only focus on the liness that have errors :)
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

okey, will do in the future. But can you guys see anything wrong with the mail() code? since i get error 501 but can't fix it:S
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It's something to do with the "From:" address if the syntax error is in the MAIL command. i.e.

Code: Select all

MAIL FROM: <you@domain.tld>
What does $sender look like?
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

Code: Select all

<input name="sender" type="text" id="sender" value="<?php echo $row1['avsandare']; ?>">
spikeZ
Forum Newbie
Posts: 2
Joined: Tue Jun 06, 2006 2:54 pm
Location: Manchester, UK

Post by spikeZ »

Howdy folks,
The problem is more likely to be the fact that you have defined a result handler to the query twice.
Once right after the query and again in the loop.
This will stop the code from running the second query and therfore won't populate the $email_adrr variable - hence the error of parameters.

Remove the mysql_fetch_array from after the query and leave the one in for the loop.

Just as a side note, I generally tend to sanitize the data before it gets as far as the functions such as mail. so do you stripslashes on the variables when they are registered. It doesn't make a great deal of difference but it is tidy ;)

Cheers

Spike
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

NiGHTFiRE wrote:

Code: Select all

<input name="sender" type="text" id="sender" value="<?php echo $row1['avsandare']; ?>">
But what does the variable actually look like? Is it a name or an email, address?
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

email adress. i'll try your suggestion spikeZ

edit: spikez idea just gave me the same results as before
Post Reply