PDA

View Full Version : Simple php script help needed



jcstille
23rd November 2004, 03:59 AM
ok so we have this php script that will just send a simple e-mail and I am having trouble. I need a from to have it sbuscribe to the mailing list. However it keeps saying it is from anonymous@ourserver.com


<?
$to = "subscribe@ourserver.com";
$email=$HTTP_POST_VARS[email];
$subject = "\n ";
$contents ="\n ";
$from = "From: $email\n";
reset ($HTTP_POST_VARS);
if ($email != "")
{
//send mail - $subject & $contents come from surfer input
mail($to, $subject, $contents, $from);
// redirect back to url visitor came from
header("Location: /lists/thanks-s.html");
}
else
{
print("<HTML><BODY>Error, no email address was submitted!");
print("</BODY></HTML>");
}
?>

Any ideas?

What am I doing wrong?

websavages
23rd November 2004, 04:24 AM
yep, you have to tell sendmail to change the 'mail from' header. have a look at

http://www.php.net/manual/en/ref.mail.php

and then the bottom post (something to do with sendmail -f)

hope this helps, ws

sej7278
23rd November 2004, 07:57 AM
also try $_POST['email'] instead of $HTTP_POST_VARS[email]

jcstille
23rd November 2004, 05:09 PM
Alright on quick question, what variable should we set in the php.ini file for being able to send from whatever address the user sets?

websavages
24th November 2004, 05:25 AM
jcstille,

the way i have solved this is to use the Mail and Mail.mime pear modules. pear.php.net i think

make sure you change the
$mail =& Mail::factory('mail');
to
$mail =& Mail::factory('sendmail');

Cheers ws

websavages
24th November 2004, 05:59 AM
jcstille,

easiest way i found is to use the Mail and Mail_Mime modules from pear


pear install Mail Mail_Mime

and then


include("Mail.php");
include("Mail/mime.php");

$hdrs = array(
"To" => $to,
"Cc" => $cc,
"Bcc" => $bcc,
"From" => $from,
"Subject" => $subject,
);

$mime = new Mail_mime(CRLF);
$mime->setTXTBody($bodypart);
$body = $mime->get(); // can't remember what this line does perhaps i should read the documentation!
$hdrs = $mime->headers($hdrs);


$mail =& Mail::factory('sendmail');
// ensure you have changed the above from $mail =& Mail::factory('mail');
// this lets you set the mail-from header to $from
$mail->send($to, $hdrs, $body);


no need to fiddle with php.ini :D

hope this helps

Cheers ws

dirtbag
25th November 2004, 02:25 AM
so heres what I got..

<?php
include("Mail.php");
include("Mail/mime.php");

$from = "From: " . $_REQUEST['email'] . "\r\n";
$to = "adulted-subscribe@ministry.stmichaelcary.org";
$subj = "subscription request";
$body = "subscribe me";
$hdrs = array(
"To" => $to,
"Cc" => $cc,
"Bcc" => $bcc,
"From" => $from,
"Subject" => $subject,
);

$mime = new Mail_mime(CRLF);
$mime->setTXTBody($bodypart);
$body = $mime->get(); // can't remember what this line does perhaps i should read the documentation!
$hdrs = $mime->headers($hdrs);
$mail =& Mail::factory('sendmail');
$mail->send($to, $hdrs, $body);
// redirect back to url visitor came from
header("Location: /lists/thanks-s.html");

?>

and this just goes nothing.. no email tries to get sent..
Im sure im doing something wrong.. but I dont know php.. ;)

-DB

websavages
25th November 2004, 05:16 AM
the From is not needed so you can change


$from = "From: " . $_REQUEST['email'] . "\r\n";

to


$from = $_REQUEST['email'];

and I also had CRLF defined elsewhere as


define(CRLF, "\r\n");

other than that I can't see where your code is going wrong, perhaps you could have a look in your log files and/or include debugging 'echo' code in your script to see where the script is going wrong

Cheers ws

dirtbag
25th November 2004, 08:01 PM
It works... thanks folks.
:)

websavages
26th November 2004, 04:51 AM
What was it that made it work? just out of curiosity?

Cheers ws