 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
|
 |
|
 |
04-10-2005, 12:40 AM
|
#1 (permalink)
|
|
The Wanderer
Join Date: Apr 2005
Location: Missouri, USA
Posts: 15
Thanks: 0
|
My first Tutorial (PHP Mail())
Well first of all I would like to say thanks for viewing my first tutorial on your board. This tutorial was requested by " Danny Needs Help".
Quote:
|
Originally Posted by Danny Needs Help
Form Processing/Mailing would be a good first tutorial :).
|
Now here we go. If you have problems understanding the tutorial please ask and I will correct the wording and help you with your problem. Now first of all there are more then just mail() to do your online eMailing. But if you just want to send eMail to someone this is the easiest way. So here we go.
First of all I think for such a easy script I will display a small sample code of the function in use and tell you how it works.
PHP Code:
<?php
$to = "Somone@domain.ext";
$subject = "My new mail() code!";
$body = "This message was made by my new mail() code!";
$from = "Bill@Microsoft.com";
mail($to, $subject, $body, "From: $from);
?>
Now first of all you are wondering how does this work? Well mail() is a function and it had 4 places for data. What address to send it to, the subject of the eMail, the body of the eMail, & the headers. Now you are probably thinking where is the place to put who it's from and where do I put my username and password for my eMail account. Well first of all... you can pick who its from using the header part of mail(). You can use anyones eMail you want just pop it in and it sends it. Then when they click reply it replys to that eMail. Your probley thinking awesome! Now you HAVE to have From: in front of the header so it knows what Bill@Microsoft.com or whatever you used is for. Or its going to think ok this isn't a header "ERROR!!!". Now everything else is pretty much easy. To format a eMail you can either use HTML or to make sure its safe since some eMail clients don't use HTML you can put: \n for breaking lines .ect you would have to learn more about normal text formatting. Now I hope that teaches you but one more thing you might want to know... How to send to more then 1 person? Well do this:
PHP Code:
<?php
$to = 'blah@blah.ext' . ', ';
$to .= 'blah@blah.ext' . ', ';
$to .= 'blah@blah.ext' . ', ';
$to .= 'blah@blah.ext';
$subject = "My new mail() code!';
$body = "This message was made by my new mail() code!";
$from = "Bill@Microsoft.com";
mail($to, $subject, $body, "From: $from");
?>
Now just replace the: blah@blah.ext with a eMail. now that sends the eMail to blah@blah.ext 4 times! I hope you get that. Please let me know if you don't get this. Thanks! also... http://us3.php.net/manual/en/function.mail.php is a good reference. All of those advanced programers that think this is a stupid tutorial. It's not made for you. It's made for users that want to make a simple feedback or just a way to eMail people when they sign up the site. Oh and if you wanna feel special and at bottom be like your IP address is logged you can just add $REMOTE_ADDR somewhere in the body. That variable will print there IP address. Thanks for viewing the tutorial and hope it helps.
Last edited by William : 05-05-2005 at 09:28 AM.
|
|
|
04-10-2005, 01:31 AM
|
#2 (permalink)
|
|
The Contributor
Join Date: Mar 2005
Posts: 81
Thanks: 0
|
Great tutorial and thanks for detailed content.
|
|
|
|
04-10-2005, 01:01 PM
|
#3 (permalink)
|
|
The Wanderer
Join Date: Apr 2005
Posts: 7
Thanks: 0
|
$subject = "My new mail() code!';
You used quotations, then an apostraphe :). Excellent tutorial though.
|
|
|
04-11-2005, 01:23 AM
|
#4 (permalink)
|
|
The Wanderer
Join Date: Apr 2005
Location: Missouri, USA
Posts: 15
Thanks: 0
|
Thanks and the subject thing was fixed.
|
|
|
04-14-2005, 09:11 PM
|
#5 (permalink)
|
|
The Wanderer
Join Date: Apr 2005
Posts: 15
Thanks: 0
|
Woahh! Great tutorial, Im getting ideas now, Im going to make a mailng system, were it gets the emails out of the database, and sends to everyone. Cheers for the tutorial is going to help me out alot.
|
|
|
|
04-14-2005, 11:03 PM
|
#6 (permalink)
|
|
The Visitor
Join Date: Apr 2005
Posts: 4
Thanks: 0
|
Nice tutorial and very detailed, Thanks
|
|
|
|
04-15-2005, 12:47 AM
|
#7 (permalink)
|
|
The Wanderer
Join Date: Apr 2005
Posts: 18
Thanks: 0
|
that's really good for your first tutorial! :)
Keep up the good work :)
|
|
|
|
04-15-2005, 09:11 PM
|
#8 (permalink)
|
|
The Wanderer
Join Date: Apr 2005
Posts: 9
Thanks: 0
|
maybe ive missed something but i belive that
$to = 'blah@blah.ext' . ', ';
$to = 'blah@blah.ext' . ', ';
$to = 'blah@blah.ext' . ', ';
$to .= 'blah@blah.ext';
these variables are wrong your only continuing the last 2 (since the 3rd one will overwrite the previous two)
i think it should be
$to = 'blah@blah.ext' . ', ';
$to .= 'blah@blah.ext' . ', ';
$to .= 'blah@blah.ext' . ', ';
$to .= 'blah@blah.ext';
then it will foward onto all 4
__________________
Fatal error: Call to undefined function: brain() in /data/www/brain.php on line 2
|
|
|
|
04-16-2005, 01:48 AM
|
#9 (permalink)
|
|
The Wanderer
Join Date: Apr 2005
Location: Ontario, Canada
Posts: 13
Thanks: 0
|
This is a very nice detailed tutorial, thanks for sharing with us William.
|
|
|
|
04-16-2005, 04:12 AM
|
#10 (permalink)
|
|
Super ModTastic
Join Date: Apr 2005
Location: Apple Valley, MN
Posts: 89
Thanks: 1
|
Great Tutorial. Good job William!
|
|
|
04-18-2005, 12:42 AM
|
#11 (permalink)
|
|
The Wanderer
Join Date: Apr 2005
Posts: 5
Thanks: 0
|
Pretty nice. With a few additions and some post comments from another page, you can make a Mass E-mailer :)
|
|
|
|
04-24-2005, 08:35 AM
|
#12 (permalink)
|
|
The Wanderer
Join Date: Apr 2005
Posts: 6
Thanks: 0
|
Three things.
1. As adam said :
$to = 'blah@blah.ext' . ', ';
$to = 'blah@blah.ext' . ', ';
$to = 'blah@blah.ext' . ', ';
$to .= 'blah@blah.ext';
should be
$to = 'blah@blah.ext' . ', ';
$to .= 'blah@blah.ext' . ', ';
$to .= 'blah@blah.ext' . ', ';
$to .= 'blah@blah.ext';
2. From variable breakup
Instead of using
$headers = "From: Bill@Microsoft.com";
with
mail($to, $subject, $body, $headers);
you should use (for more flexibility)
$from = "Bill.Gates@M$hit.com";
mail($to, $subject, $body, From: $headers);
The variable name 'headers' is mis-leading as a header can contain any amount of information from a vast range of categories ;).
3. Pretty nice. With a few additions and some post comments from another page, you can make a Mass E-mailer
- I decided to write a E-Mail Script. If you use it then please give credit to Omar Al-Kurd (me).
- The script i wrote is 1 page, name can be anything and it is self-sufficient/maintaing.
- If people like it alot, i may evolve it into a mailing list type thing ;).
email.php
PHP Code:
Hmm... deleted the script >.< I'll make it again pretty soon.
Last edited by crash : 05-05-2005 at 11:42 PM.
|
|
|
|
05-05-2005, 09:30 AM
|
#13 (permalink)
|
|
The Wanderer
Join Date: Apr 2005
Location: Missouri, USA
Posts: 15
Thanks: 0
|
Dear All,
Thank you for all the comments. I have fixed the . part I am very sorry for not seeing that. Also about the headers thing. Your right there are tons of headers so if there is a varible called headers. Why would you wanna put: From: $headers then all the headers are going to be from. That is why I put $headers and added From: blah. So people could add more headers. If you have any comments let me know.
-William Key
|
|
|
05-05-2005, 11:39 PM
|
#14 (permalink)
|
|
The Wanderer
Join Date: Apr 2005
Posts: 6
Thanks: 0
|
Hmmm... I just relized that my script that i made may not be faulty at all but rather a problem with PHP 4.3.10 (look at the change log under 4.3.11) so i will open my script up again :).
|
|
|
|
05-08-2005, 01:46 AM
|
#15 (permalink)
|
|
The Wanderer
Join Date: May 2005
Posts: 5
Thanks: 0
|
Very good example. However, just for constructive criticism, I would do this instead:
PHP Code:
$emails = array("email1@aol.com","email2@aol.com","email3@aol.com");
$subject = "Hello";
$body = "Message Here";
$headers = "From: Billy";
foreach($emails as $e) {
if(!mail($e,$subject,$body,$from)) {
die("Error in sending mail);
}
}
This way, if you do choose to send it to many people, spam blockers won't interpret the huge mailing list as a spam email. You can also use the last argument in many different ways other than "From". Based on the smtp protocol, you can do much more with it. see http://php.net/mail for more information.
|
|
|
07-04-2005, 08:32 PM
|
#16 (permalink)
|
|
The Visitor
Join Date: Jul 2005
Posts: 1
Thanks: 0
|
William,
Thanks for the great tutorial, and sharing it with us! I will try it out.
Zeromancer,
Thanks for pointing that out to us.
Cheers,
Nagaki
|
|
|
|
11-26-2007, 12:25 PM
|
#17 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
|
For very large e-mail sendning quantitys you should use a email class like php mailer. http://phpmailer.codeworxtech.com/
Because as i understand the mail() function opens a connection and closes it on every call. That might be of server load concern later on.
But i guess a few hundred emails is going the be allright but one would have to see. Optionally you could work in a cue system of some sort to keep the load down, maybe a delay between loops or something. Otherwise a email class would be good.
/EyeDentify
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
|
|
|
|
11-27-2007, 02:38 AM
|
#18 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Quote:
Originally Posted by EyeDentify
For very large e-mail sendning quantitys you should use a email class like php mailer. http://phpmailer.codeworxtech.com/
Because as i understand the mail() function opens a connection and closes it on every call. That might be of server load concern later on.
But i guess a few hundred emails is going the be allright but one would have to see. Optionally you could work in a cue system of some sort to keep the load down, maybe a delay between loops or something. Otherwise a email class would be good.
|
I didn't realise that the mail function behaved in that way, but I have noticed its performance to be rather inadequate for large batches of emails, as you say.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
11-27-2007, 02:42 AM
|
#19 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Posts: 133
Thanks: 6
|
Quote from phpmailer's website:
Quote:
|
As you may know, it is simply to send mails with the PHP mail() function. So why use PHPMailer? Isn't it slower? Yes that's true, but PHPMailer makes it easy to send e-mail
|
Sticking with the standard mail(); way is the best. Just have a good mail server.
|
|
|
11-27-2007, 08:19 AM
|
#20 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
|
Some people wish to implement a layout or maybe a little bit more detailed header information. Go to php.net/mail for more. ;) I like to use the user contributed content to mod my own script to my likings.
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|