Jump to content

Email script challenge? Needs some feedback.


sayedsohail

Recommended Posts

Hi everyone,

 

Everyone is taking a lot about email and email and email, let see if someone can help us in improving this code, which is basically a very basic and great email script for sending emails with attachments and its working flawleslly, the improvements it requires are as follows:

 

1. Need a hidden IFRAME to process attachments, so the user won't leave the current page.

2. it needs a progress bar when uploading attachments.

3. Needs some validation scripts for email, subject and to and cc .

4. If anyway someone knows ajax functionality.

 

1. email.php

 

<?php
$max_no_img=5; // Maximum number of images value to be set here
echo “<form method=post action=attach.php enctype=’multipart/form-data’>”;
for($i=1; $i<=$max_no_img; $i++){
echo “<tr><td>Photo $i: </td><td>
<input type=file name=’images[]’ class=’bginput’></td></tr>”;
}

 

attach.php

 

<?php
require(”class.phpmailer.php”);
//Variables Declaration
$name = “the Submitter”;
$email_subject = “Images Attachment”;
$Email_msg =”A visitor submitted the following :\n”;
$Email_to = “you@yourSite.com”; // the one that recieves the email
$email_from = “someone@someone.net”;
$dir = “uploads/$filename”;
chmod(”uploads”,0777);
$attachments = array();
//
checkType();
//
//——Check TYPE——\\
function checkType() {
while(list($key,$value) = each($_FILES[images][type])){
strtolower($value);
if($value != “image/jpeg” AND $value != “image/pjpeg” AND $value != “”) {
exit(’Sorry , current format is <b>’.($value).’</b> ,only Jpeg or jpg are allowed.’) ;
}
}
//
checkSize();
//
}
//——-END OF Check TYPE——–\\
//
//—CheckSizeFunction —\\
function checkSize(){
while(list($key,$value) = each($_FILES[images][size]))
{
$maxSize = 5000000;
if(!empty($value)){
if ($value > $maxSize) {
echo”Sorry this is a very big file .. max file size is $maxSize Bytes = 5 MB”;
exit();
}
else {
$result = “File size is ok !<br>”;
//
}
//
}
//
}
uploadFile();
//
}
//——-END OF Check Size——–\\
//
//==============upload File Function============\\
//
function uploadFile() {
global $attachments;
while(list($key,$value) = each($_FILES[images][name]))
{
//
if(!empty($value))
{
$filename = $value;
//the Array will be used later to attach the files and then remove them from server ! array_push($attachments, $filename);
$dir = “uploads/$filename”;
chmod(”uploads”,0777);
$success = copy($_FILES[images][tmp_name][$key], $dir);
}
//
}
//
if ($success) {
echo ” Files Uploaded Successfully<BR>”;
SendIt();
//
}else {
exit(”Sorry the server was unable to upload the files…”);
}
//
}
//
//==== PHP Mailer With Attachment Func ====\\
//
function SendIt() {
//
global $attachments,$name,$Email_to,$Email_msg,$email_subject,$email_from;
//
$mail = new PHPMailer();
$mail->IsSMTP();// send via SMTP
$mail->Host = “localhost”; // SMTP servers
$mail->SMTPAuth = false; // turn on/off SMTP authentication
$mail->From = $email_from;
$mail->FromName = $name;
$mail->AddAddress($Email_to);
$mail->AddReplyTo($email_from);
$mail->WordWrap = 50;// set word wrap
//now Attach all files submitted
foreach($attachments as $key => $value) { //loop the Attachments to be added …
$mail->AddAttachment(”uploads”.”/”.$value);
}
$mail->Body = $Email_msg.”Name : “.$name.”\n”;
//
$mail->IsHTML(false);// send as HTML
$mail->Subject = $email_subject;
if(!$mail->Send())
{
echo “Message was not sent <p>”;
echo “Mailer Error: ” . $mail->ErrorInfo;
exit;
}
//
echo “Message has been sent”;
// after mail is sent with attachments , delete the images on server …
foreach($attachments as $key => $value) {//remove the uploaded files ..
unlink(”uploads”.”/”.$value);
}
//
}
//
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.