Task of the day today is Email, I will be looking at how standard email is sent throught PHP and how html email can be sent too.
How does PHP send email?
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
What are the parameters?
This function requires the same information as an email program does at minimum to work. This information is the parametes of the function.
to – the first parameter is who the email is to be recived by
subject – the subject of an email
message – the content of the email
Next is the subject and the parameter after this is the actual message.
Following this are additional optional parameters that are headers and an additional.
How is this used in practice example 1
in tthe example below i am looking at how the mail() function can be used at the most basic level
mail('[email protected]','test','test'); echo 'Hi';
As decribed above the first parameter is the email address to send to, the secondis the subject and the third is the message.
This does indeed send an email to my mailbox.
The example below shows what I think is a more readable way to use the mail() function
<?php $email = '[email protected]'; $subject = 'test2'; $message = 'test2'; mail($email, $subject, $message); ?>
The simple diffrence is the values of the parametes are wrapped up in varables before they are passed to their respective parameters
What are headers?
The headers are the details in the email about who the email is from, who is copied in etc.
Headers are optional and can be skipped by having NULL in its parameter space instead of $header
Headrs that can be used are from: , Reply-to; Cc; Bcc; ect; headers should be seperated with CRLF (a line breaker) to do this add . "\r\n" . between each header
These values can be passed in as varable see example below:
<?php /*mail('[email protected]','test','test'); echo 'Hi';*/ $email = '[email protected]'; $subject = 'test3'; $message = 'test3'; $headers = 'From: [email protected]' . "\r\n" . 'Cc: [email protected]'; mail($email, $subject, $message, $headers); ?>
how can HTML be used in an email