TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-10-2005, 12:40 AM   #1 (permalink)
The Wanderer
 
Join Date: Apr 2005
Location: Missouri, USA
Posts: 15
Thanks: 0
William is on a distinguished road
Default 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.
Send a message via AIM to William Send a message via MSN to William
William is offline  
Reply With Quote
Old 04-10-2005, 01:31 AM   #2 (permalink)
The Contributor
 
Join Date: Mar 2005
Posts: 81
Thanks: 0
AlEast is on a distinguished road
Default

Great tutorial and thanks for detailed content.
__________________
NEWEDGE Services, Inc. - Developers of ClientExec
AlEast is offline  
Reply With Quote
Old 04-10-2005, 01:01 PM   #3 (permalink)
The Wanderer
 
Join Date: Apr 2005
Posts: 7
Thanks: 0
Danny Needs Help is on a distinguished road
Default

$subject = "My new mail() code!';

You used quotations, then an apostraphe :). Excellent tutorial though.
__________________
[DigitalFlavor]Web Design
Send a message via AIM to Danny Needs Help
Danny Needs Help is offline  
Reply With Quote
Old 04-11-2005, 01:23 AM   #4 (permalink)
The Wanderer
 
Join Date: Apr 2005
Location: Missouri, USA
Posts: 15
Thanks: 0
William is on a distinguished road
Default

Thanks and the subject thing was fixed.
Send a message via AIM to William Send a message via MSN to William
William is offline  
Reply With Quote
Old 04-14-2005, 09:11 PM   #5 (permalink)
The Wanderer
 
Join Date: Apr 2005
Posts: 15
Thanks: 0
ammo is on a distinguished road
Default

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.
ammo is offline  
Reply With Quote
Old 04-14-2005, 11:03 PM   #6 (permalink)
The Visitor
 
Join Date: Apr 2005
Posts: 4
Thanks: 0
nonenone is on a distinguished road
Default

Nice tutorial and very detailed, Thanks
nonenone is offline  
Reply With Quote
Old 04-15-2005, 12:47 AM   #7 (permalink)
The Wanderer
 
Join Date: Apr 2005
Posts: 18
Thanks: 0
Veolus is on a distinguished road
Default

that's really good for your first tutorial! :)

Keep up the good work :)
__________________
Please Join >> http://www.streetballforum.com :o
Veolus is offline  
Reply With Quote
Old 04-15-2005, 09:11 PM   #8 (permalink)
The Wanderer
 
Join Date: Apr 2005
Posts: 9
Thanks: 0
Adam is on a distinguished road
Default

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
Adam is offline  
Reply With Quote
Old 04-16-2005, 01:48 AM   #9 (permalink)
The Wanderer
 
Join Date: Apr 2005
Location: Ontario, Canada
Posts: 13
Thanks: 0
aquafuse is on a distinguished road
Default

This is a very nice detailed tutorial, thanks for sharing with us William.
aquafuse is offline  
Reply With Quote
Old 04-16-2005, 04:12 AM   #10 (permalink)
Super ModTastic
 
mike.fro's Avatar
 
Join Date: Apr 2005
Location: Apple Valley, MN
Posts: 89
Thanks: 1
mike.fro is on a distinguished road
Default

Great Tutorial. Good job William!
Send a message via AIM to mike.fro Send a message via MSN to mike.fro
mike.fro is offline  
Reply With Quote
Old 04-18-2005, 12:42 AM   #11 (permalink)
The Wanderer
 
Join Date: Apr 2005
Posts: 5
Thanks: 0
Sergio965 is on a distinguished road
Default

Pretty nice. With a few additions and some post comments from another page, you can make a Mass E-mailer :)
Sergio965 is offline  
Reply With Quote
Old 04-24-2005, 08:35 AM   #12 (permalink)
The Wanderer
 
Join Date: Apr 2005
Posts: 6
Thanks: 0
crash is on a distinguished road
Default

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.
crash is offline  
Reply With Quote
Old 05-05-2005, 09:30 AM   #13 (permalink)
The Wanderer
 
Join Date: Apr 2005
Location: Missouri, USA
Posts: 15
Thanks: 0
William is on a distinguished road
Default

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
Send a message via AIM to William Send a message via MSN to William
William is offline  
Reply With Quote
Old 05-05-2005, 11:39 PM   #14 (permalink)
The Wanderer
 
Join Date: Apr 2005
Posts: 6
Thanks: 0
crash is on a distinguished road
Default

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 :).
crash is offline  
Reply With Quote
Old 05-08-2005, 01:46 AM   #15 (permalink)
The Wanderer
Newcomer 
 
Join Date: May 2005
Posts: 5
Thanks: 0
zeromancer is on a distinguished road
Default

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.
Send a message via AIM to zeromancer
zeromancer is offline  
Reply With Quote
Old 07-04-2005, 08:32 PM   #16 (permalink)
The Visitor
 
Join Date: Jul 2005
Posts: 1
Thanks: 0
nagaki is on a distinguished road
Default

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
nagaki is offline  
Reply With Quote
Old 11-26-2007, 12:25 PM   #17 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

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.
EyeDentify is offline  
Reply With Quote
Old 11-27-2007, 02:38 AM   #18 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Quote:
Originally Posted by EyeDentify View Post
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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-27-2007, 02:42 AM   #19 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

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.
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 11-27-2007, 08:19 AM   #20 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

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.
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP 5.1.2 Released! AlEast News and Announcements 1 01-25-2006 11:56 PM
PHP 5.0.5 Released AlEast News and Announcements 0 09-20-2005 01:31 PM
PHP Tutorial Section Up and Running CreativeLogic News and Announcements 4 06-01-2005 06:32 PM


All times are GMT. The time now is 02:00 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design