Sending Mail with PHP

i am an experienced perl programmer but a beginner with PHP, but i know the basics....

i need to send the contents of a submitted form to an email address...

in perl this would be done by parsing the name/value and then opening sendmail on a unix server....

open (MAIL, "|/usr/lib/sendmail -t") like that

in php, parsing is no problem but how do you actually send the body of the email, and how does php know the path to sendmail coz in the books i've read it seems to automatically know where this is....how?

code for sending the subject, sender, cc and body would be very much appreciated....the body of an email would be plain text but upto 500 characters..

cheers guys....
 
You use the mail() function.

PHP.net describes it here thus:

PHP:
bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]])

Therefore...

PHP:
$sent = mail("recipient@company.com, "My Message Subject", "My first PHP Mail Message");
if($sent)
{
    echo "Mail Sent";
}
else
{
    echo "Mail Not Sent";
}

You should use the extra headers, especially, the "From" and "Reply To", because many incoming-mail servers will regard messages without these headers as spam. I would build variables for everything, like this:
PHP:
// Set up the message
$to = "recipient@company.com";
$from = "me@mycompany.com";
$subject = "The message subject";
$message = "My special e-mail message";

// Now send the message
$sent = mail($to, $subject, $message, "From: Me <".$from.">\r\nReply To: ".$from);

Then you can do your sent test as detailed in the previous code fragment.

HTH :)
 
thanks Giles that helps a lot...

its the $message(parameter) varaible that always puzzled me wit php....how is it defined?, how long, and what type of data is it..?

and lastly..how are html headers send via an email message with php...

cheers buddy....:)
 
Originally posted by TurboSmoke
thanks Giles that helps a lot...

its the $message(parameter) varaible that always puzzled me wit php....how is it defined?, how long, and what type of data is it..?

Erm, I'm not sure I understand the question. I wonder if you're confusing variables with function calls.

PHP:
$message = "Some text";
Sets the variable $message to contain "Some text". Remember that PHP is a weakly-typed language, so there's no real explicit declaration of variables like there is in, say, C. I'm not aware of any limitations to string length.

PHP:
dostuff($withthisdata);
Passes the variable $withthisdata to the function dostuff(). However, in many cases, functions can pass data back to the line that called them, using the syntax
PHP:
return $result
In this instance, if you wish to use that data, you set a variable equal to it, like this:

PHP:
$functionoutput = dostuff($withthisdata);
This will set the variable $functionoutput to $result, in this example.
and lastly..how are html headers send via an email message with php...
You add the following to the headers string:

PHP:
"MIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1"


cheers buddy....:)

You're welcome!
 
thanks again Giles..

i think i confused you with my bad wording...

what i was trying to ask was when you assign string data to a variable $message, how long can the string be? dont you have to first declare the variable type before you use it...?

all i am needing to do is send data from a form and send that data via email to a predefined address, the message entered by the user is formatted in html by the script before sending it....

the problem is, the data taken on by the $message variable may be quite long....infact it contains the whole of the body of the email...

anyway, you have given me all the info i need to get going...

cheers buddy

TS
 
Originally posted by TurboSmoke
thanks again Giles..

i think i confused you with my bad wording...

what i was trying to ask was when you assign string data to a variable $message, how long can the string be? dont you have to first declare the variable type before you use it...?

all i am needing to do is send data from a form and send that data via email to a predefined address, the message entered by the user is formatted in html by the script before sending it....

the problem is, the data taken on by the $message variable may be quite long....infact it contains the whole of the body of the email...

anyway, you have given me all the info i need to get going...

cheers buddy

TS

No. PHP is weakly-typed. So the variable takes on the type of the data that you're assigning to it. I'm not aware of any limitations in string length, although I have manipulated 500K images as MIME data using variables, so I wouldn't have thought you need to worry if you're below that level.
 

Latest Posts

Back