The mail() function allows you to send an email directly from a script.
Returns TRUE if the email delivery was successfully accepted, FALSE otherwise.
mail(to,subject,message,headers,parameters)
parameter | describe |
---|---|
to | Required. Specifies the recipients of the email. |
subject | Required. Specifies the subject of the email. Note: This parameter cannot contain any newline characters. |
message | Required. Define the message to be sent. Separate each line with LF (n). Lines should not exceed 70 characters. Windows Note: When PHP connects directly to an SMTP server, if a period is found at the beginning of a line in a message, it will be removed. To fix this, replace a single period with two periods: <?php$txt = str_replace("n.", "n..", $txt);?> |
headers | Optional. Specifies additional headers such as From, Cc, and Bcc. Additional headers should be separated by CRLF (rn). Note: When sending an email, it must include a From header. This parameter can be set or set in the php.ini file. |
parameters | Optional. Specifies additional parameters for the sendmail program (defined in the sendmail_path configuration setting). For example: when sendmail is used with the -f sendmail option, sendmail can be used to set the sender address. |
NOTE: You need to keep in mind that just because an email is accepted for delivery, it does not mean that the email reaches its intended destination.
Send a simple email:
<?php$txt = "First line of textnSecond line of text";// Use wordwrap() if lines are longer than 70 characters$txt = wordwrap($txt,70);// Send emailmail("somebody@example. com","My subject",$txt);?>
Send an email with extra headers:
<?php$to = "[email protected]";$subject = "My subject";$txt = "Hello world!";$headers = "From: [email protected]" . "rn" ."CC : [email protected]";mail($to,$subject,$txt,$headers);?>
Send an HTML email:
<?php$to = "[email protected], [email protected]";$subject = "HTML email";$message = "<html><head><title>HTML email</title></head ><body><p>This email contains HTML Tags!</p><table><tr><th>Firstname</th><th>Lastname</th></tr><tr><td>John</td><td>Doe</td ></tr></table></body></html>";// Always set content-type when sending HTML email$headers = "MIME-version: 1.0" . "rn";$headers .= "Content-type:text/html;charset=iso-8859-1" . "rn";// More headers$headers .= 'From: <[email protected]>' . "rn";$headers .= 'Cc: [email protected]' . "rn";mail($to,$subject,$message,$headers);?>