tags:

views:

108

answers:

5

i am learning how to send an email.i have installed appserver and in files php.ini-dist and php.ini-recommended i did the following changes

SMTP=localhost

[email protected]

i replaced localhost with mail.ptcl.net which is my dsl provider and replaced [email protected] with my email address [email protected]

i get the following message when i submit the form

"warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in C:\AppServ\www\Hamza\send_simpleform.php on line 14 The following e-mail has been sent:

Hamza: hamza

[email protected]

Message: adasdas"

following is the script i am using

<html>
<head>
<title>Simple Feedback Form</title>
</head>
<body>
<form method="post" action="send_simpleform.php"/>
<p><strong>Your name</strong><br/>
<input type="text" name="sender_name" size="30" /></p>
<p><strong>Senders Email address</strong><br/>
<input type="text" name="sender_email" size="30" /></p>
<p><strong>Enter your Message here</strong><br/>
<textarea name="message" cols="30" rows="5" wrap="virtual"></textarea></p>
<p><input type="submit" value="send this form" name="submit"/></p>
</form>
</body>
</html>


<?php
if(($_POST[sender_name]=="")||($_POST[sender_email]="")||($_POST[message==""]                                                                                                          ))
{   header("location=simple_form.html");
    exit;
}
$msg="E-MAIL SENT FROM WWW SITE\n";
$msg="Sender's Name:  $_POST[sender_name]\n";
$msg="Sender's E-Mal I.D:    $_POST[sender_email]\n";
$msg="Message:                 $_POST[message]\n";
$to="[email protected]";
$subject="Web site feedback";
$mailholders="from my website<[email protected]>\n";
$mailheader="Reply-to: $_POST[sender_email]\n";
mail($to, $subject,$msg,$mailheaders);
?>
<html>
<head>
<title>Simple Feedback Form Sent</title>
</head>
<body>
<h1>The following e-mail has been sent:</h1>
<p><strong>Hamza:</strong><br\>
<?php echo "$_POST[sender_name]";?>
<p><strong>[email protected]</strong><br\>
<?php echo "$_POST[sender_email]";?>
<p><strong>Message:</strong><br>
<? echo "$_POST[message]";?>
</body>
</html>
A: 

It could be simplier to use an external library to send mails, as http://swiftmailer.org/ instead of playing with php.ini

It's never good to be php.ini dependant.

Guillaume Lebourgeois
`It's never good to be php.ini dependant.` What's that supposed to mean? You're pretty much always dependent on php.ini. Not all configuration options are available for runtime modification. Meanwhile, a mail library is a good idea, as they are easier to use than the `mail()` function. However, if things aren't configured correctly, they don't do jack for you.
George Marian
I mean sometimes you have to deploy scripts or sites on several servers, and it can happens you have not the possibility to modify php.ini. It's why I don't like to have scripts too much dependant on that.
Guillaume Lebourgeois
That's a fair point, which I believe should be made more obvious in your statement. You do allude to that by stating `instead of playing with php.ini`
George Marian
A: 

Try this: This worked for me in my site.

$email = "your e-mail address";
$myname = "name";
$mymail = "reply Address";
    $subject = "SUBJECT LINE";
    $body = "TEXT GOES HERE";
    $headers = "Content-Type: text/plain; charset=us-ascii\nFrom: $myname <$mymail>\n
    Reply-To: <$mymail>\nBCC:<$mymail>\nReturn-Path: <$mymail>\nX-Mailer: PHP";

            //send the email
            if ($email != "") { 
                mail($email,$subject,$body,$headers); 
            }   
Jeff V
"worked for me on my site", oh ffs!
SilentGhost
@SilentGhost - I know that seems like a cop out to say that, but... It did. I don't remember changing anything in the php.ini file so maybe, he is just missing something in his script that mine isn't. At least it is something.
Jeff V
The issue is that the OP's error message points to an issue with the "from" email header. What you've posted is a total shot in the dark which ignores that important bit and it doesn't set the from header, so it would need to correctly set in php.ini.
George Marian
I was thinking it was the Content_Type: and the Reply-To: syntax as I remember it was very finicky. I was looking at it from the low hanging fruit side rather than having to dive into the php.ini.
Jeff V
Upon further review, it does set the From header. /facepalm
George Marian
+2  A: 

I believe you have at least two problems:

  1. I am guessing that php.ini-recommended and php.ini-dist are respectively an example and a template configuration files; modifications to them may not impact the "real" php.ini, currently used by your instance of PHP. Use phpinfo() to make sure that your configuration is correct.
  2. Spam filters. Jeff Atwood has a pretty good blog post about implementing the checks that servers do when checking if your email message is spammy. Don't forget:

Just because you send an email doesn't mean it will arrive.

jhominal
+1 for point 1, good eye/interpretation.
George Marian
when i send an email wont i receive it in my email address [email protected]?
Rookie9
@rookie Assuming that things are configured correctly and that the email isn't blocked by a spam filter.
George Marian
+1  A: 

As jhominal points out, the php.ini files you've modified are just examples. You'll have to find out where the php.ini is for your distribution and modify it.

As mentioned by others, you can set a custom from header. However, if you don't set sendmail_from in php.ini you'll have to remember to always set it.

At any rate, here is a pretty good article/tutorial on sending email with PHP's mail() function:

http://articles.sitepoint.com/article/advanced-email-php

George Marian
A: 
<html>
    <head>
        <title>Simple Feedback Form</title>
    </head>
    <body>
        <form method="post" action="send_simpleform.php" />
            <p>
                <strong>Your name</strong><br />
                <input type="text" name="sender_name" size="30" />
            </p>
            <p>
                <strong>Senders Email address</strong><br />
                <input type="text" name="sender_email" size="30" />
            </p>
            <p>
                <strong>Enter your Message here</strong><br />
                <textarea name="message" cols="30" rows="5" wrap="virtual"></textarea>
            </p>
            <p>
                <input type="submit" value="send this form" name="submit" />
            </p>
        </form>
    </body>
</html>
<?php
    if($_POST['sender_name'] == '' OR $_POST['sender_email'] == '' OR $_POST['message'] == '')
    {
        header('Location: simple_form.html');
        exit;
    }
    $msg = "E-MAIL SENT FROM WWW SITE";
    $msg .= "Sender's Name: {$_POST['sender_name']}\r\n";
    $msg .= "Sender's E-Mail I.D: {$_POST['sender_email']}\r\n";
    $msg .= "Message:\r\n{$_POST['message']}\r\n";
    mail('[email protected]', 'Web site feedback', $msg, "Reply-to: {$_POST['sender_email']}\r\nFrom: [email protected]");
?>
<html>
    <head>
        <title>Simple Feedback Form Sent</title>
    </head>
    <body>
        <h1>The following e-mail has been sent:</h1>
        <p>
            <strong>Hamza:</strong><br />
            <?php echo $_POST['sender_name']; ?>
            <p><strong>[email protected]</strong><br />
            <?php echo $_POST['sender_email']; ?>
            <p><strong>Message:</strong><br />
            <?php echo $_POST['message']; ?>
    </body>
</html>
  1. Readable code is maintainable code. Indent your code, always. PHP or HTML ... It's nice to see your code follow a structure, as it should. People who read it, like me, will thank you for making my life just that much better. When you read your code again a year from now you'll thank yourself too.
  2. You should add a doctype to your HTML files.
  3. Never use an undefined constant. By that I mean, if you need to access an array member, you must surround the name of the key with quotes. $_POST[member] is not good, whereas $_POST['member'] is.
  4. When you used the $msg variable, you over wrote it 4 times. This happened because you used just the equals sign = not the concate operator (a period) with the equals sign .=.
  5. The underlying problem for the whole script, amazling, was that you where missing the From: [email protected] part of the $additional_headers within the mail function.
  6. Don't define variables your only going to use once. It's just a waste.
  7. Never, ever, ever echo raw user input. You open yourself up to XSS and you give the domain away to the person typing in the details. Take a look at PHP's security page about User Submitted Data.
Mark Tomlin