PHP Mail
From KOP KB
Contents
Summary
Simple php mail script as well as other related trouble shooting mail scripts
Simple PHP Mail Script
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"]))
{
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
From: <input type="text" name="from"><br>
Subject: <input type="text" name="subject"><br>
Message: <textarea rows="10" cols="40" name="message"></textarea><br>
<input type="submit" name="submit" value="Submit Feedback">
</form>
<?php
}
else
// the user has submitted the form
{
// Check if the "from" input field is filled out
if (isset($_POST["from"]))
{
// this is mostly for windows you can comment this line out or remove it completely
ini_set("sendmail_from","[email protected]");
$from = $_POST["from"]; // sender
$subject = $_POST["subject"];
$message = $_POST["message"];
// message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// send mail
mail("[email protected]",$subject,$message,"From: $from\n");
echo "Thank you for sending us feedback";
}
}
?>
Form Mail based Troubleshooting
The below is where you make yourself the sent email as well the error message to mail you something with the error in it. Keep in mind also need to make sure you change recipients, which is a hidden input field
// You can put in full emails here but you can just change the domain and extension if need to
$TARGET_EMAIL = array($EMAIL_NAME . "@domain\.com$");
/* Help: http://www.domain.com/pathtofile/def_alert.php */
$DEF_ALERT = "[email protected]";
PHP Mail easy way attachments
function multi_attach_mail($to, $subject, $message, $senderMail, $senderName, $files){
$from = $senderName." <".$senderMail.">";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
if(count($files) > 0){
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}\n";
$fp = @fopen($files[$i],"rb");
$data = @fread($fp,filesize($files[$i]));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $senderMail;
//send email
$mail = @mail($to, $subject, $message, $headers, $returnpath);
//function return true, if email sent, otherwise return fasle
if($mail){ return TRUE; } else { return FALSE; }
}
//email variables
$to = $_REQUEST['email'];
$from = '[email protected]';
$from_name = 'name you want to see';
//attachment files path array
$files = array('filename1.pdf','filename2.pdf','filename3.pdf','filename4.jpg');
$subject = 'Dear Prospective Parents';
$html_content = "html message can span multiple lines";
$send_email = multi_attach_mail($to,$subject,$html_content,$from,$from_name,$files);