 |
| ::: T4VN Statistics ::: |
PHP Scripts : 61 |
PHP Example : 67 |
PHP Tutorials : 21 |
PHP News : 93 |
Total Coupon : 36 |
Other Tutorials : |
Member : 209 |
Visitor Online : 6 |
Today Visit: 308 |
Total Visitor : 262318 |
Most Online : 31 |
|
 |
 |
Mail with attachments
Author : JeeperEngineer
These two functions create a form and when submitted will email a set recipient.
They are intended to use SESSION variable, but that code is easily modified. Enjoy.
| PHP Example : | | <?
function emailUs() {
if(isset($_POST['action']) && $_POST['action'] == 'SEND_MAIL') {
echo " SENDING EMAIL...<br />n";
if(sendMyMail()) echo " EMAIL SENT.<br /><br />n";
}
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="form">
<form name="email" method="post" action="<?php echo "$_SERVER[PHP_SELF]"; if(isset($_GET['menu'])) echo "?menu=$_GET[menu]"; if(isset($_GET['subMenu'])) echo "&subMenu=$_GET[subMenu]"; ?>" enctype="multipart/form-data">
<tr>
<td>
<?php
if(!isset($_SESSION['userID'])) {
?>
YOUR NAME: <input type="text" name="mailer" size="40" value="<?php echo $_SESSION['userID']; ?>" /><br /><br />
YOUR EMAIL: <input type="text" name="mail_from" size="40" value="<?php echo $_SESSION['email']; ?>" /><br /><br />
<?php
}
?>
SUBJECT: <input type="text" name="e_subject" size="40" maxlength="80" /><br /><br />
MESSAGE:<br /><textarea name="e_message" cols="40" rows="10"></textarea><br />
<?php
if(isset($_SESSION['userID'])) {
echo " FILE(s):<br />n";
for($i = 0; $i < 4; $i++) {
echo " <input type="file" name="myFile{$i}" size="40" /><br />n";
}
}
?>
</td>
</tr>
<tr>
<td align="right">
<input type="hidden" name="action" value="SEND_MAIL" />
<input type="submit" name="submit" value="submit" class="button" />
</td>
</tr>
</form>
</table>
<?php
} // END emailUs
function sendMyMail(){
$mail_to = "abc@123.com";
if(!isset($_SESSION['userID'])) {
if(!empty($_POST['mailer'])) $mailerID = $_POST['mailer'];
else {
echo " CANNOT SEND EMAIL WITHOUT SENDERS NAME!<br /><br />n";
echo " ABORTING TRANSMISSION...<br /><br />n";
return 0;
}
if(!empty($_POST['mail_from'])) $mail_from = $_POST['mail_from'];
else {
echo " CANNOT SEND EMAIL WITHOUT SENDERS EMAIL ADDRESS!<br /><br />n";
echo " ABORTING TRANSMISSION...<br /><br />n";
return 0;
}
if(!eregi("^[a-z0-9]+([_.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+(.[a-z]{2,})$",$_POST['mail_from'],$regs)) {
echo " THE EMAIL ADDRESS ENTERED: ("".$_POST['mail_from']."") IS NOT A VALID FORMAT!<br /><br />n";
echo " ABORTING TRANSMISSION...<br /><br />n";
return 0;
}
}
else {
$mailerID = $_SESSION['userID'];
$mail_from = $_SESSION['email'];
}
$e_subject = stripslashes($_POST['e_subject']);
$e_message = stripslashes($_POST['e_message']);
$border_random = md5(time());
$mail_boundary = "x{$border_random}x";
$new_e_message = "From: {$mailerID} <{$mail_from}>rn";
$new_e_message .= "To: {$mail_to}rn";
$new_e_message .= "Reply-to: {$mail_from}rn";
$new_e_message .= "MIME-Version: 1.0rn";
$new_e_message .= "Content-type: multipart/mixed; boundary="{$mail_boundary}"rn";
$new_e_message .= "This is a multi-part message in MIME format.rnrn";
$new_e_message .= "--{$mail_boundary}rn";
$new_e_message .= "Content-type: text/plain; charset="iso-8859-1"rn";
$new_e_message .= "Content-Transfer-Encoding:7bitrnrn";
$new_e_message .= "{$e_message}rnrn";
for ($i = 0; $i < 4; $i++) {
$myFile{$i} = $_FILES['myFile'.$i]['tmp_name'];
if (is_uploaded_file($myFile{$i})) {
$myFile_type = $_FILES['myFile'.$i]['type'];
$myFile_name = $_FILES['myFile'.$i]['name'];
$myFile_size = $_FILES['myFile'.$i]['size'];
$fp = fopen($myFile{$i},"rb");
$fileData = fread($fp,$myFile_size);
fclose($fp);
$file = base64_encode($fileData);
$file = chunk_split($file);
$new_e_message .= "--{$mail_boundary}rn";
$new_e_message .= "Content-type: {$myFile_type}; name="{$myFile_name}"rn";
$new_e_message .= "Content-Transfer-Encoding:base64rn";
$new_e_message .= "Content-Disposition: attachment; filename="{$myFile_name}"rnrn";
$new_e_message .= $file."rnrn";
}
}
$new_e_message .= "--{$mail_boundary}--rn";
$e_body = "";
$e_message = $new_e_message;
$send = mail($mail_to,$e_subject,$e_body,$e_message);
if($send) return 1;
else {
echo " EMAIL COULD NOT BE SENT FOR UNKNOWN REASONS!<br /><br />n";
return 0;
}
} // END sendMyMail
| |
| |

|
 |
| ::: Resources ::: |
|
|
| ::: New Templates ::: |
|
| ::: Other Tutorials ::: |
|
 |