iloveny Posted March 12, 2007 Share Posted March 12, 2007 Hi folks! I am trying to create a sending email feature in PHP. Let's somebody sign up and an email will be sent to them notifying them about their account. Do you have any tutorial for this sending email feature? Many many thanks in advanced for your time and help folks!!! Quote Link to comment https://forums.phpfreaks.com/topic/42412-solved-sending-email-feature-using-smtp-and-server-setting/ Share on other sites More sharing options...
guyfromfl Posted March 12, 2007 Share Posted March 12, 2007 The general syntax of the mail() function is this: mail(<email address of who it goes to>, <email subject>, <email body>, <headers [see php.net] for more on headers and other args]>); this is a sample from a project im working on makeEmail.php: <table> <tr> <form method='POST' action='sendEmail.php' valign = 'top' align='left'> Email address: <input type='text' name='email'><br> // The address of guy sigining up Mail Body: <textarea name='body'></textarea><br> // the email body <input type='submit' value='Send Request'> </form> </tr> </table> the following code is quite abit more than you need but it will sniff out spammers and invalid entries. to keep it simple just delete the function and function calls sendEmail.php: $to = $_REQUEST['email']; $subject = "PHP mail() Tutorial"; $message = $_REQUEST['body']; $email = $_REQUEST['email']; echo "<br><br><br><br><div align='center'><span>"; echo "<center><b><p>"; function is_valid_email($email) { return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si', $email); } function contains_bad_str($str_to_test) { $bad_strings = array ("content-type:", "mime-version:", "multipart/mixed", "Content-Transfer-Encoding:", "bcc:", "cc:", "to:"); foreach ($bad_strings as $bad_string) { if(eregi($bad_string, strtolower($str_to_test))) { echo "$bad_string found. Suspected injection attemp - mail not sent"; exit; } } } function contains_newlines($str_to_test) { if(preg_match("/(%0A|%0D|\\n+|\\r+)/i", $str_to_test) != 0) { echo "newline found in $str_to_test. Suspected injection attempt - mail not sent"; exit; } } if($_SERVER['REQUEST_METHOD'] != "POST") { echo "Unauthorized attempt to access the page!"; exit; } if (!is_valid_email($email)) { echo 'The email address you entered is not valid.'; echo "<br><br></b><a href='makeEmail.php'>Click here to try again.</a>"; echo "<br><br><br><br><br><br><br><br><br>"; exit; } contains_bad_str($email); contains_bad_str($subject); contains_bad_str($message); contains_newlines($email); contains_newlines($subject); $header = "From: $email"; mail($to, $subject, $message, $header); echo "Thank you. Your email has been sent."; echo "<br><br></b><a href='makeEmail.php'>Click here to continue</a>"; echo "</p></center></span></div><br><br><br><br>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/42412-solved-sending-email-feature-using-smtp-and-server-setting/#findComment-205756 Share on other sites More sharing options...
guyfromfl Posted March 12, 2007 Share Posted March 12, 2007 also check out this link to make sure your php.ini setting is correct http://www.php.net/mail Quote Link to comment https://forums.phpfreaks.com/topic/42412-solved-sending-email-feature-using-smtp-and-server-setting/#findComment-205762 Share on other sites More sharing options...
iloveny Posted March 13, 2007 Author Share Posted March 13, 2007 Hi guyfromfl! Thanks a lot for your reply, it has been really helpful for me. I am actually worry about the PHP.ini setting also. I am using shared hosting to host my website. Is this going to be a problem when I need to change PHP.ini? Thanks a lot for your time! Quote Link to comment https://forums.phpfreaks.com/topic/42412-solved-sending-email-feature-using-smtp-and-server-setting/#findComment-205970 Share on other sites More sharing options...
cshireman Posted March 13, 2007 Share Posted March 13, 2007 Just out of curiosity, why do you need to change the php.ini file? The default settings are usually good enough for most applications. Quote Link to comment https://forums.phpfreaks.com/topic/42412-solved-sending-email-feature-using-smtp-and-server-setting/#findComment-205975 Share on other sites More sharing options...
iloveny Posted March 13, 2007 Author Share Posted March 13, 2007 I haven't tested whether it will work properly or not. If it works properly and I don't need to change PHP.ini, then it's great! However, I believe when dealing with Window hosting, we will need to change the PHP.ini setting. When we host the website in LINUX hosting, I don't think we need to change PHP.ini. I haven't confirmed this yet, it is just my assumption. Do you have any idea? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/42412-solved-sending-email-feature-using-smtp-and-server-setting/#findComment-205980 Share on other sites More sharing options...
iloveny Posted March 13, 2007 Author Share Posted March 13, 2007 Hi guyfromfl! I have tried so many times, but the PHP script to email just don't work. I have also tried it in different server in case I missed the PHP.ini setting. All with no result. I have also tried to use different script from somebody else, it don't work. The funny thing is, it doesn't have any error message. It is successfully sent out, but I didn't receive any email that suppose to be in my inbox. Did you use your own server or did you share hosting as well? Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/42412-solved-sending-email-feature-using-smtp-and-server-setting/#findComment-206008 Share on other sites More sharing options...
sn33kyp3t3 Posted March 13, 2007 Share Posted March 13, 2007 I am assuming you have tried a simple: mail($to, $sub, $msg); to see if it works. Right? If that didn't work, I would contact your web hosting service. Quote Link to comment https://forums.phpfreaks.com/topic/42412-solved-sending-email-feature-using-smtp-and-server-setting/#findComment-206022 Share on other sites More sharing options...
guyfromfl Posted March 13, 2007 Share Posted March 13, 2007 I personally didnt have to change my php.ini file, i just sent that link just as a reference. my default SMTP setting works without a hitch. My setup is Fedora Core 5 & PHP 5 and until you get it working try what sn33kyp3t3 said. remember when youre debugging Keep It Simple Quote Link to comment https://forums.phpfreaks.com/topic/42412-solved-sending-email-feature-using-smtp-and-server-setting/#findComment-206588 Share on other sites More sharing options...
brad Posted March 14, 2007 Share Posted March 14, 2007 if you're sending to a yahoo, hotmail, gmail etc they generally block all emails sent from PHP scripts at least untill the owner of that inbox adds your address to their whitelist/address book. a message like this is what's needed: http://www.newcutsmile.net/you/register/ Quote Link to comment https://forums.phpfreaks.com/topic/42412-solved-sending-email-feature-using-smtp-and-server-setting/#findComment-206712 Share on other sites More sharing options...
iloveny Posted March 14, 2007 Author Share Posted March 14, 2007 sn33kyp3t3, yes I did try that and I also tried others codes that were as simple as the one you showed me. But it still doesnt work. I was also assuming something going wrong with the mail server, and I contacted my hosting server and yes they have SMTP server installed and run properly. What left is what Brad said. I sent to hotmail and yahoo. I should try send it to another email address. Thanks a lot guys! I will post the result here. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/42412-solved-sending-email-feature-using-smtp-and-server-setting/#findComment-206805 Share on other sites More sharing options...
iloveny Posted March 14, 2007 Author Share Posted March 14, 2007 Brad was right. If you send it to hotmail / yahoo / gmail, they automatically delete it. Basically, they don't like email from mail server, I think. This being said, we have a new problem. How about if we need to send information to our clients, the clients' emails are stored in MySQL, and (to make it easy) we write a script that will send email to each one of our clients that are listed in our database? If our clients' emails are hotmail, yahoo or gmail, they will not receive our message. This will be a big problem, isn't it? Thanks guys!!! Quote Link to comment https://forums.phpfreaks.com/topic/42412-solved-sending-email-feature-using-smtp-and-server-setting/#findComment-206826 Share on other sites More sharing options...
iloveny Posted March 15, 2007 Author Share Posted March 15, 2007 Continue to: http://www.phpfreaks.com/forums/index.php/topic,131516.0.html Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/42412-solved-sending-email-feature-using-smtp-and-server-setting/#findComment-207708 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.