Jump to content

[SOLVED] Sending Email Feature using SMTP and Server Setting


iloveny

Recommended Posts

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!!!

Link to comment
Share on other sites

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>";


?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.