Page 1 of 1

mail()

Posted: Tue Jun 06, 2006 9:19 am
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

Posted: Tue Jun 06, 2006 9:26 am
by hawleyjr
You need to have single quotes around subject.

Code: Select all

$_POST[subject]
to:

Code: Select all

$_POST['subject']

Posted: Tue Jun 06, 2006 9:28 am
by Chris Corbyn
Make sure you have < and > around the email address. Some SMTP servers don't like it otherwise.

Posted: Tue Jun 06, 2006 9:57 am
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'";

}

}

?>

Posted: Tue Jun 06, 2006 10:10 am
by Chris Corbyn
You can try Swift (Signature) or PHPMailer (Sourceforge).

Posted: Tue Jun 06, 2006 11:09 am
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 :)

Posted: Tue Jun 06, 2006 12:59 pm
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

Posted: Tue Jun 06, 2006 2:59 pm
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?

Posted: Tue Jun 06, 2006 3:10 pm
by NiGHTFiRE

Code: Select all

<input name="sender" type="text" id="sender" value="<?php echo $row1['avsandare']; ?>">

Posted: Tue Jun 06, 2006 3:11 pm
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

Posted: Tue Jun 06, 2006 3:22 pm
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?

Posted: Wed Jun 07, 2006 2:47 pm
by NiGHTFiRE
email adress. i'll try your suggestion spikeZ

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