PHP - Sending Emails Using PHP
PHP - Sending Emails Using PHP
PHP must be configured correctly in the php.ini file with the details of how your system
sends email. Open php.ini file available in /etc/ directory and find the section
headed [mail function].
Windows users should ensure that two directives are supplied. The first is called SMTP
that defines your email server address. The second is called sendmail_from which
defines your own email address.
Linux users simply need to let PHP know the location of their sendmail application.
The path and any desired switches should be specified to the sendmail_path directive.
1
to
Required. Specifies the receiver / receivers of the email
2
subject
Required. Specifies the subject of the email. This parameter cannot contain any
newline characters
3
message
Required. Defines the message to be sent. Each line should be separated with a
LF (\n). Lines should not exceed 70 characters
4
headers
Optional. Specifies additional headers, like From, Cc, and Bcc. The additional
headers should be separated with a CRLF (\r\n)
5
parameters
Optional. Specifies an additional parameter to the send mail program
As soon as the mail function is called PHP will attempt to send the email then it will
return true if successful or false if it is failed.
Multiple recipients can be specified as the first argument to the mail() function in a
comma separated list.
While sending an email message you can specify a Mime version, content type and
character set to send an HTML email.
Example
<head>
<title>Sending HTML email using PHP</title>
</head>
<body>
<?php
$to = "[email protected]";
$subject = "This is subject";
</body>
</html>
A boundary is started with two hyphens followed by a unique number which can not
appear in the message part of the email. A PHP function md5() is used to create a 32
digit hexadecimal number to create unique number. A final boundary denoting the
email's final section must also end with two hyphens.
<?php
// request variables // important
$from = $_REQUEST["from"];
$emaila = $_REQUEST["emaila"];
$filea = $_REQUEST["filea"];
if ($filea) {
function mail_attachment ($from , $to, $subject, $message,
$attachment){
$fileatt = $attachment; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$email_txt .= $msg_txt;
if($ok) {
echo "File Sent Successfully.";
unlink($attachment); // delete a file after attachment
sent.
}else {
die("Sorry but the email could not be sent. Please go
back and try again!");
}
}
move_uploaded_file($_FILES["filea"]["tmp_name"],
'temp/'.basename($_FILES['filea']['name']));
mail_attachment("$from", "[email protected]",
"subject", "message", ("temp/".$_FILES["filea"]["name"]));
}
?>
<html>
<head>
</head>
<body>
<tr>
<td><input name = "from" type = "text"
id = "from" size = "30"></td>
</tr>
<tr>
<td class = "frmtxt2"><input name =
"emaila"
type = "text" id = "emaila" size =
"30"></td>
</tr>
<tr>
<td height = "20" valign = "bottom">Attach
File:</td>
</tr>
<tr>
<td height = "40" valign = "middle"><input
name = "Reset2" type = "reset" id =
"Reset2" value = "Reset">
<input name = "Submit2" type = "submit"
value = "Submit" onClick = "return
CheckData45()"></td>
</tr>
</table>
</form>
<center>
<table width = "400">
<tr>
<td id = "one">
</td>
</tr>
</table>
</center>
</td>
</tr>
</table>
</body>
</html>