Jump to content
Sign in to follow this  
mdevelle

Email From Php

Recommended Posts

I fussed around with setting up a "Contact Us" page in my site and came up with this PHP script, with the help of Essential PHP Tools (D. Sklar, apress books) and the PEAR Mail Extension from the PEAR Website. I used a form in the final version, but the basic mechanics are here:

 

<?php

require 'Mail.php'; \\ From the PEAR mail package

$mailer =& Mail::factory('sendmail'); \\ Create object using the sendmail driver

$to = 'addressee@wherever.com'; \\ Where to send it

$headers = array('From' => 'sendermailname@senderaddress.com',

'Subject' => 'Hungry?',

'Cc' => 'sendheretoo@anotheraddress.com');

/* The cc: does not work right ... yet */

 

/* Create a body of text */

$body=<<<_MSGx_

Are you hungry? Wouldn't you like a cold,

sweet ice cream cone?

Why not stop by your local

ice cream parlor today for a few

scoops of Guava Mint

Bouillon?

 

Sincerely,

Your local ice cream booster

_MSGx_;

 

$res = $mailer->send($to, $headers, $body); \\ the send() method does the work

 

// If the message can't be sent, send() returns a

// PEAR::Error object

if (PEAR::isError($res)) {

print "Couldn't send message: " . $res->getMessage();

}

?>

Share this post


Link to post
Share on other sites

By using the normal PHP mail command and some variables, you can easily send emails:

 

<?php

$to = "yourplace@******";

$subject = "My email.";

$message = "message text goes here";

 

$headers = "From: myplace@here.com\r\n";

$headers .= "Reply-To: myplace2@here.com\r\n";

$headers .= "Return-Path: myplace@here.com\r\n";

$headers .= "CC: sombodyelse@noplace.com\r\n";

$headers .= "BCC: hidden@special.com\r\n";

 

if ( mail($to,$subject,$message,$headers) ) {

echo "The email has been sent!";

} else {

echo "The email has failed!";

}

?>

 

 

htmlite has a pretty clear and complete thing about using the mail command (i find it usefull):

http://www.htmlite.com/php029.php

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

×