e-mail php

They have: 5,633 posts

Joined: Jan 1970

How do you send a e-mail with php. I am looking but not finding. Client is gonna start yelling soon.

chrishirst's picture

He has: 379 posts

Joined: Apr 2005

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Well if you go to php.net, and look in the documentation, there is a section listed in the main table of contents called "Mail Functions", which will take you to http://us3.php.net/mail.

Then on that page, one of the two listed functions is called "mail", which takes you to http://us3.php.net/manual/en/function.mail.php which has some pretty good examples.

They have: 5,633 posts

Joined: Jan 1970

I did try going through php.net but I come up with some other function other than mail(); mail() could do waything. I was looking for something along the lines of send_mail();

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

do you have a reference for send_mail() to compare what the features you are expecting. I did a search on MSDN.com (a sight you compared the ease of PHP.net to) and did a search for send_mail(). Only one item came up, and man did it look like a lot more hastle to send mail than doing:

<?php
$to     
= '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [[email protected]][email protected][/email]' . "\r\n" .
       
'Reply-To: [[email protected]][email protected][/email]' . "\r\n" .
       
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
'

They have: 5,633 posts

Joined: Jan 1970

ASP is a little more confiseing if you want to send a e-mail.
ASP e-mail works like this...

You start with a mail oject (ususally a IIS add-on like bamboo or Jmail)
We will call this object 'Mail_Obj'

Then you modify the obect useing attrabutes. Example...
Mail_Obj.from = "[email protected]"
Mail_Obj.to = "[email protected]"
Mail_Obj.subject = "you get that thing I sent you?"

Then you send the object....
Mail_Obj.Send();

Personally I think that is way better than craming everything into one single function.

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

Greg K's example doesn't exactly 'cram everything in one function', and seems like it offers a little more flexibility than the ASP object model. If you really wanted to though, you could write a php mail class that mimicked the ASP one exactly.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Off the top of my head, and after finding an example of ASP's way, I made the following you can try:

<? // saved as MailMessage.class.inc
 
  class MailMessage
  {
  var $toField;
  var $fromField;
  var $subjectField;
  var $bodyField;
  var $mailerName;
 
  function MailMessage($mailerName="phpMailClass 2005.06.07")
  {
  $this->mailerName = $mailerName;
  }
 
  function setFrom($fromName)
  {
  $this->fromField = $fromName;
  }
 
  function setTo($toName)
  {
  $this->toField = $toName;
  }
 
  function setSubject($subject)
  {
  $this->subjectField = $subject;
  }
 
  function setBody($body)
  {
  $this->bodyField = $body;
  }
 
  function send()
  {
 
  // Unless you are sure about its use, add code here to check
  // to make sure the variables have been set and are valid
  // (ie. the to/from field looks like e-mail addresses)
 
  $headers  = "From: " . $this->fromField . "\r\n";
$headers .= "Reply-To: " . $this->fromField . "\r\n";
$headers .= "X-Mailer: " . $this->mailerName;
 
return $mail($this->toField, $$this->subjectField, $this->bodyField, $headers);
  }
  }
 
  ?>
'
You can then use it similar to what you are used to:
<?
 
   require_once ('MailMessage.class.inc');

  $Mail_Obj = new MailMessage;
  // To specify your own value for the X-Mailer field
  // in the message use the line below instead
  // $Mail_Obj = new MailMessage("My Mailer");
 
  $Mail_Obj->setFrom("[email protected]");
  $Mail_Obj->setTo("[email protected]");
  $Mail_Obj->setSubject("you get that thing I sent you?");
  $Mail_Obj->setBody("Depite the subject sounding like a spam subject, this isnt!");
 
  $Mail_Obj->send();
  ?>
'

As mentioned in the class, there is no error checking in the thing, for production use, should either have error checking in the function or somewhere.

IMO, classes are not always the best answer for simple things. (my boss keeps saying "won't it be easier to put it all in a class, everything should be in a class nowadays".

-Greg

They have: 5,633 posts

Joined: Jan 1970

I think this whole php mail think might of been one big waist of time. The host my client has doent allow sending e-mails.

The example I posted is etreamly simple.

I was just searching Jmail for a list of possable methods and I came across this...
http://dimac.net/Products/w3JMail/Version43/Examples/Speedmailer.htm

They have: 5,633 posts

Joined: Jan 1970

The only classes I have ever used/made are where in Java and I dont think that counts bacause all a Java file is, is a class. I need to look into writeing them for asp.

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

Quote: I was just searching Jmail for a list of possable methods and I came across this...
http://dimac.net/Products/w3JMail/V...Speedmailer.htm

So, if the problem was cramming it all into one function, and that's what speed mail does. Sounds to me like simply a bias towards ASP.

They have: 5,633 posts

Joined: Jan 1970

CptAwesome wrote: So, if the problem was cramming it all into one function, and that's what speed mail does. Sounds to me like simply a bias towards ASP.

Well I dident know that existed. I have seen many Jmail examples and odd enough I have never seen that used before. That's why I was supprised to see it. It is kind of funny though.

It seems useless if you tink about it. It would be for small amout of (text) data to be sent by e-mail. Which defeats the whole purpose of haveing to send a e-mail dot you think??

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

I think that is natural when you are used to a language, no matter what is. If you know something inside and out, switching to something else can be hard, and will seem more complicated.

This is especially true when you come up against a feature/function that do the same taks, but are named and used differently. You go from your known language looking for what you know but it is handled differently in another.

This is why I never caught on to C++ years ago, I knew Visual Basic very well, and I would get frustrated and say screw it and get the job done in VB instead Wink

-Greg

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

I agree with Greg. I face this problem often as I like to learn as many languages (ways of thinking) as I can. And with every new language, there are new challenges.

What Bryan is taking issue with in this case is a difference between object-oriented programming and plain procedural programming. It can be difficult switching from strong OOP to procedural because it's not so much the style of programming that's new but the way you design it and visualise the flow of the program. I get the feeling you're probably shooting from the hip rather than planning your attack on paper first, so that only amplifies your problem. It's also difficult to pick up best-practices for the new approach quickly, so that too can reduce the quality of your work.

Fortunately, PHP (as of v5) -- unlike Java -- doesn't enforce either OOP or non-OOP discipline on you. You can choose either, even mix them. Whatever suits. Of course, you'd likely have to know how to write your own classes to take advantage of its OOP features. Depending on your situation, maybe PEAR can help too. (Btw, check out PEAR Mail).

If you think all this is bad, try picking up functional programming (LISPs, etc) after only learning imperative languages (C, Java, etc). Wink

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

Abhishek Reddy wrote: If you think all this is bad, try picking up functional programming (LISPs, etc) after only learning imperative languages (C, Java, etc). Wink

You're absolutely right. Speaking as a CS graduate, LISP is from the devil... Laughing out loud

They have: 161 posts

Joined: Jan 2005

Well wish I knew what you guys are talking about. Sticking out tongue Are the codes posted in this forum good PHP form mail scripts? That's what I'm trying to look for..hehe Wink

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.