It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Partner Sites
Board index Programming PHP and MySQL Forum Script-archive

Send an email from you website

Share your completed scripts.

Moderator: Malcolm

Send an email from you website

Postby flabbyrabbit on Mon Dec 24, 2007 1:11 pm

This question has been ask so often, so here it is.

Simple mail function, send a flat email.
[php]<?php
$to = 'to@hotmail.com'; //address email should be sent to
$subject = 'subject here'; //Subject of the email
$message = 'enter the message here'; //The message
//These extra headers allow you to set who the message is from
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//"From" is address that the email would have been sent from
//"Reply-To" is the address that any replies will be sent to
//This line sends your message
mail($to, $subject, $message, $headers);
?>[/php]
This wont be very useful to most people, as it is very rarely you want to sent the same email to one person repeatedly.

So next i will show you how to create a form that will be used to send the email. I this example i will use a contact form on a web page that allows visitors to send a message to the owner.

First create a page called contact.php, then enter this code:
Code: Select all
<form method=POST action="send.php">
Email: <input name="email"><br>
Subject: <input name="subject"><br>
Message:<br>
<textarea name="message" cols=40 rows=6></textarea><br>
<input type=submit value="send">
</form>

Then make another page called send.php:
[php]<?php
$to ='nosmo-kings@hotmail.com'; //Your email address (webmasters email address)
$subject = $_POST['subject']; //Gets the subject sent by the form
$message = $_POST['message']; //Gets the message sent by the form
$headers = 'From: ' . $_POST["email"] . "\r\n" .
'Reply-To: ' . $_POST["email"] . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//Changes the reply to email so the webmaster can easily reply
mail($to, $subject, $message, $headers);
//Shows a message when email is sent
echo "message sent";
?>[/php]
Now the contact form should be up and running. Emails may be placed in recipients junk mail box for security reasons.

I hope this answers everyones questions, but if you need any more help don't hesitate to ask,
Flabby Rabbit
User avatar
flabbyrabbit
500+ Club
 
Posts: 703
Joined: Thu Jan 25, 2007 2:10 pm
Location: Midlands, England

Postby anonymousnewbie on Mon Dec 31, 2007 10:02 pm

i think I'm too stupid..
may i know what is the good start for me to learn any basic programming language...do i need to download or installed it?can we start with notepad?i make a research that notepad can save to certain program like .vbs, .bat.
is any other?
anonymousnewbie
 
Posts: 4
Joined: Mon Dec 10, 2007 4:53 am

Postby Johnathan on Tue Jan 01, 2008 11:27 pm

Yea you can use notepad. Save it as .php the way you save the other stuff in different formats like .html, .vbs and so on.
Johnathan
1000+ Club
 
Posts: 1205
Joined: Thu May 31, 2007 4:28 pm
Location: Belfast, Northen Ireland

Postby DDragon on Wed Feb 20, 2008 3:53 am

ok. i have tried this out and have not got it working properly. with the 'contact.php' is there any php scripting needed? as i am new to PHP scripting and am not sure what to do with it.

Thanks DD
I put everything down to this: EWIT (Everything Works In Theory)
DDragon
50+ Club
 
Posts: 93
Joined: Wed Jun 20, 2007 1:51 am
Location: Australia

Postby webmaster on Wed Feb 20, 2008 9:42 am

The only changes in the PHP-script you'll have to do is to change the e-mail in:
$to ='nosmo-kings@hotmail.com';
Make sure to check out our TNX Review and Link Vault Review
User avatar
webmaster
Site Admin
 
Posts: 2589
Joined: Tue Aug 17, 2004 2:07 pm
Location: Sweden

Postby leonard on Wed Mar 12, 2008 2:50 pm

If you don't want your emails to be marked as spam when they are received, you may want to have a look at this topic:

how-to-avoid-being-spam-vt9653.html

cheers!
- leonard
:%s/^M//
There are 10 kinds of people:
Those who understand binary and those who don't.
User avatar
leonard
100+ Club
 
Posts: 147
Joined: Tue Dec 18, 2007 8:11 am
Location: Switzerland

Postby DDragon on Sat Jun 14, 2008 5:20 am

Ok i have a large form i need this script for. DO i have to input each individual field into the script or will it take all fields into the message and send as is?

DD
I put everything down to this: EWIT (Everything Works In Theory)
DDragon
50+ Club
 
Posts: 93
Joined: Wed Jun 20, 2007 1:51 am
Location: Australia

Postby Johnathan on Sat Jun 14, 2008 10:47 am

You need to put all the individual fields into the script. Well the ones you want to get from your user....
Last edited by Johnathan on Wed Oct 22, 2008 10:29 pm, edited 1 time in total.
Johnathan
1000+ Club
 
Posts: 1205
Joined: Thu May 31, 2007 4:28 pm
Location: Belfast, Northen Ireland

Postby DDragon on Sat Jun 14, 2008 11:59 am

lol ok now i see *points to self* learn to friggen read sip**** lol
thanks john
I put everything down to this: EWIT (Everything Works In Theory)
DDragon
50+ Club
 
Posts: 93
Joined: Wed Jun 20, 2007 1:51 am
Location: Australia

Re: Send an email from you website

Postby niz972 on Wed Jun 10, 2009 8:36 pm

Hey all! Trying to use this as a "Send this page to a friend" script so that it included the website link in the message. How would I go about doing this? Thanks in advance!
niz972
 
Posts: 1
Joined: Wed Jun 10, 2009 8:33 pm

Re: Send an email from you website

Postby dflynn on Tue Dec 29, 2009 6:13 am

niz972 wrote:niz972

Niz, just add your page url between the quotes in: $message = 'enter the message here';

You just need to devise a way to manage your URLs.
Most sites these days use $ _GET variables in the url such as: http://www.mysite.com/index.php?page=19

So for this exampe I would use:
Code: Select all
<?php
if(isset($ _GET['page']{
page = $ _GET['page']
}
//bunch of code..
$message = ' blah blah blah, my message.   Link: http://www.mysite.com/index.php?page='.$page.'';



Here's Flabbyrabbit's original flat-email code cleaned up a bit.
Code: Select all
<?php
$to = 'to@hotmail.com'; //address email should be sent to

$subject = 'subject here'; //Subject of the email

$message = 'enter the message here'; //The message
//These extra headers allow you to set who the message is from

$headers = 'From: webmaster@example.com' . "\r\n" .
                 'Reply-To: webmaster@example.com' . "\r\n" .
                 'X-Mailer: PHP/' . phpversion();
//"From" is address that the email would have been sent from
//"Reply-To" is the address that any replies will be sent to

//This line sends your message
mail($to, $subject, $message, $headers);
?>
Image
nnjoi.com - Portfolio
nnjoiart.com - Art & Design Blog
bathroomwriter.com - Interested in helping out with Beta Testing? PM me.
User avatar
dflynn
500+ Club
 
Posts: 859
Joined: Wed Oct 03, 2007 10:06 pm
Location: Guelph, Canada


Who is online

Users browsing this forum: No registered users and 0 guests