PHP mail function

Discussion in 'PHP' started by fr33d0m, Jul 22, 2007.

  1. #1
    Hi, i want to create a PHP script that will use mail function.
    Do i need to install any addition packages for mail function?
    Can someone guide me, pls?
    Thanks in advance
     
    fr33d0m, Jul 22, 2007 IP
  2. Pryda

    Pryda Peon

    Messages:
    506
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    From http://www.php.net/manual/en/ref.mail.php:
    For an example of actual usage of mail(), see http://www.php.net/function.mail
     
    Pryda, Jul 22, 2007 IP
  3. Bartuc

    Bartuc Active Member

    Messages:
    120
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #3
    you don't need to install anything.

    usage:
    mail("to", "subject", "message", "headers");
     
    Bartuc, Jul 22, 2007 IP
  4. fr33d0m

    fr33d0m Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi...
    Thanks for the replies :) Really appreciate them.
    I got this error : Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\321_Test\site\download_viewer_check.php on line 9

    Below is a piece of code that i copied from manual.
    
    <?php
    	$to      = 'aileen_p@yahoo.com';
    	$subject = 'CSCI321t;
    	$message = 'hello. testing email';
    	$headers = 'From: webmaster@example.com' . "\r\n" .
    	    'Reply-To: webmaster@example.com' . "\r\n" .
    	    'X-Mailer: PHP/' . phpversion();
    
    	mail($to, $subject, $message, $headers);
    ?>
    
    Code (markup):
    Any ideas why is this happening? Thanks...
     
    fr33d0m, Jul 22, 2007 IP
  5. ds316

    ds316 Peon

    Messages:
    154
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You need to have an SMTP server running on the computer for mail to be sent. If you are only getting this error when testing on your own computer then its not a huge error, to test out the mail sending you should upload the script to a webserver, even a free one will do, and run it from there.

    If you are running Windows XP pro you can install IIS which has an SMTP server built into it. I'm sure others may also be able to recommend other SMTP server software that can do the trick.
     
    ds316, Jul 22, 2007 IP
  6. fr33d0m

    fr33d0m Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Well, we have a SMTP server installed at our server.
    I've upload my files there..
    We're using Apache for our webserver
     
    fr33d0m, Jul 22, 2007 IP
  7. ds316

    ds316 Peon

    Messages:
    154
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    All should work well then, as I said if you want to test on your own PC you can do so, but its only for a bit of convenience.
     
    ds316, Jul 22, 2007 IP
  8. fr33d0m

    fr33d0m Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Yes, it can work now after resetting my SMTP server :D
    Thanks for your replies :)
    Btw, can i ask you something again? :D
    Does mail function support attachment of files?
    I read some articles about PHPMailer class, if i want to attach a file to my email, do i need to use this class?
     
    fr33d0m, Jul 22, 2007 IP
  9. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #9
    I would suggest you use a pre-build class to attach a file to a email. Makes life easier and you don't have to learn how to do it all by yourself. phpclasses.org has a bunch of them for you to try out.

    Also, since you were asking. The error code you shown to us said everything you should have needed to know. Verify your "SMTP" settings in php.ini. as it looks like you corrected.
     
    exodus, Jul 23, 2007 IP
  10. fr33d0m

    fr33d0m Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hi, exodus.
    I try to use PHPMailer class. But when i try to send an email to my account, i never receive the email. Though the code doesn't result any error messages.
    Below is my code :
    
    <?php
    	require("class.phpmailer.php");
    
    	$mail = new PHPMailer();
    
    	$mail->IsMail();
    
    	$mail->AddAddress("cemplukcute2003@yahoo.com","Aileen Pranoto");
    
    	$mail->WordWrap = 50;                              // set word wrap
    	$mail->AddAttachment("./license/license.zip");      // attachment
    	$mail->IsHTML(true);                               // send as HTML
    
    	$mail->Subject  =  "Sig-Share is being tired";
    	$mail->Body     =  "This is the <b>HTML body</b>";
    	$mail->AltBody  =  "This is the text-only body";
    
    	if(!$mail->Send())
    	{
     	  	echo "Message was not sent <p>";
      		echo "Mailer Error: " . $mail->ErrorInfo;
       		exit;
    	}
    	else {
    		echo "Message has been sent";
    	}
    ?>
    
    Code (markup):
    Can you help me, pls? Thanks
     
    fr33d0m, Jul 23, 2007 IP
  11. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #11
    exodus, Jul 24, 2007 IP