Alittle Help with send emails using php

Hi all i need a little help with sending emails using the following code.

php code

<?php if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "[email protected]"; <<<<<<<<send email to adress enterted by the user $email_subject = "Your email subject line"; function died($error) { // your error code can go here echo "We are very sorry, but there were error(s) found with the form you submitted. "; echo "These errors appear below.

"; echo $error."

"; echo "Please go back and fix these errors.

"; die(); } // validation expected data exists if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['comments'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $first_name = $_POST['first_name']; // required $last_name = $_POST['last_name']; // required $email_from = $_POST['email']; // required $telephone = $_POST['telephone']; // not required $comments = $_POST['comments']; // required $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.
'; } $string_exp = "/^[A-Za-z .'-]+$/"; if(!preg_match($string_exp,$first_name)) { $error_message .= 'The First Name you entered does not appear to be valid.
'; } if(!preg_match($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.
'; } if(strlen($comments) < 2) { $error_message .= 'The Comments you entered do not appear to be valid.
'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "First Name: ".clean_string($first_name)."\n"; $email_message .= "Last Name: ".clean_string($last_name)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Telephone: ".clean_string($telephone)."\n"; $email_message .= "Comments: ".clean_string($comments)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?>

Thank you for contacting us. We will be in touch with you very soon.

"[email protected]"; to an email adress entered by the user on the form. (inputbox or formfield)

sorry for the dumb question - just starting out in php

any help with this would be great

thanks all :slight_smile:

I’m also a newb in PHP (:
I think it would help to know why you need to do this. The reason I say so is that by changing WHO they can send emails to, while still inserting their own email into the form, you’re basically acting as an unnecessary intermediary between the user and whoever they want to email.

Forms like this are used to make it convenient for a user to email YOU. If you want them to be able to choose who to email, why not just let them use their emails??

Of course, you may have a very good reason for doing so. I think if you explained what this was all about, it might help me (or someone better than me) to answer it. I have one solution in mind, but don’t want to send you on a mission to write the code if it won’t suit your needs. Let me know.

I was creating an email page for my fathers website. Which would allow him to send emails to customers direct from the site. The page would be password protected and can only be accessed by myself and my father who is old school and has no computer experience at all (he struggles sending text messages lol).Just trying to make it abit easier for him. is there any otherway i can achieve this.

thanks in advance

First things first, I noticed there are multiple areas in your script where the it will say messages like “There was an error” and not end the script. This means eventually the script will send you an email no matter what. Instead, try using die()
die(“There was an error”)
This will end the script.

Now to answer your question, guessing that there was an email form on the other page, all you would have to do is set action to this page and method to post: on another page with those textboxes, then do:
email_to = $_POST[‘email’] . If you have to put post in quotes, always remember to get rid of the quotes in the array key, like: email_to = “$_POST[email]” that.

But most importantly, the mail. The thing is, mailing with mail() has to be set up with php.ini. In php.ini, you have to find “SMTP” and put the proper SMTP for your site (usually smtp.youremailsite.com) or similar. So like this:
SMTP = smtp.gmail.com
On windows, you also have to set smtp_port to the smtp port supplied by the website and remove the semicolon next to the statement.
smtp_port = 25 (change 25 to the port supplied by your email website)
On Linux, there is also smtp_path that you have to change. I have windows and have never done this so let’s skip that…
HOWEVER, if your email website such as gmail uses SSL or TLS, (which gmail does) it will not work with mail(). For this reason it is pretty unsecure. Another problem yet is that mail() has two possible actions depending on system. On LINUX systems, mail() opens Sendmail with your message in it. The user must press “Send” on the top left to send it and can see the message before it is sent. In windows, it connects to SMTP and automatically mails the message for you. Instead, I recommend using PHPMailer. It is rather easy to use. You can find it at phpmailer.worxware.com . Download version 5.2.0 and you can use the tutorial at http://annofone.wordpress.com/2010/06/29/phpmailer-setup-for-the-uninitiated/ although I must warn you that PHPMailer ONLY WORKS ONLINE. So even if you have apache installed, it won’t work, you must upload the PHPMailer folder online and use it there. Then you can get started with the tutorial.

I hope this and PHPMailer helps you as I know it helped me. I don’t know if you use gmail but I found a function at http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/ that can help you with that and it works great.
Have a nice day,
Tommy

That was a great help thanks alot :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service