简体   繁体   中英

How to send an email in PHP reliably?

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

sendmail_from=me@localhost.com

i replaced localhost with mail.ptcl.net which is my dsl provider and replaced me@localhost.com with my email address h_k9@live.com

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

h_k9@live.com

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="h_k9@live.com";
$subject="Web site feedback";
$mailholders="from my website<geriadress@yourdomain.com>\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>h_k9@live.com</strong><br\>
<?php echo "$_POST[sender_email]";?>
<p><strong>Message:</strong><br>
<? echo "$_POST[message]";?>
</body>
</html>

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.

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

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.

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); 
            }   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM