Jump to content

help with email script...


martingreenwood

Recommended Posts

hey

 

I need help with my mail script

 

when the form on http://www.mcgdesignstudio.com/contact.html is filled out, it send the from with no errors.. however i never receive it..

 

my hosting company have told me as long as i have the fourth element in if -f or something it will work, dunno where  to put that though..

 

this is my script:

 

<?php

if(!$_POST) exit;

$email = $_POST['email'];


//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','message');
$required = array('name','email','message');

$your_email = "martin@mcgdesignstudio.com";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";

foreach($values as $key => $value){
  if(in_array($value,$required)){
	if ($key != 'subject' && $key != 'company') {
	  if( empty($_POST[$value]) ) { echo 'You need to fill in all the required fields...'; exit; }
	}
	$email_content .= $value.': '.$_POST[$value]."\n";
  }
}

if(mail("$your_email", "$email_subject", "$email_content",
        "To: Martin Greenwood <$your_email >\n" .
      "From:  mcgdesignstudio.com <martin@mcgdesignstudio.com>\n" .
      "MIME-Version: 1.0\n" .
        "Content-type: text/html; charset=iso-8859-1"
)){ 
          echo 'Your message has been sent successfully..';

          } else {

          echo 'Oops, something went wrong...try again later.';

          }

}

?>

 

 

 

Link to comment
Share on other sites

Hi martingreenwood,

 

Chnage your script to the below:

 

<?php

if(!$_POST) exit;

$email = $_POST['email'];


//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','message');
$required = array('name','email','message');

$to_email = "martin@mcgdesignstudio.com";
$from_email = "From: mcgdesignstudio.com  <martin@mcgdesignstudio.com>";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";

foreach($values as $key => $value){
  if(in_array($value,$required)){
	if ($key != 'subject' && $key != 'company') {
	  if( empty($_POST[$value]) ) { echo 'You need to fill in all the required fields...'; exit; }
	}
	$email_content .= $value.': '.$_POST[$value]."\n";
  }
}

if(mail($to_email, $email_subject, $email_content, $from_email))
        { 
          echo 'Your message has been sent successfully..';

          } else {

          echo 'Oops, something went wrong...try again later.';

          }

}

?>

 

Changes made:

 

1.  Changed the variable name of the "To" address to $to_email.

2.  Added a new $from_email variable and added the relevant "From" information

3.  Removed the extra header information

4.  Rewritten the mail() command

 

Give the above a try and report back with the result.

 

Hope this helps.

 

Link to comment
Share on other sites

hey, cheers for that, it sent the mail... but again I didn't receive it...

 

I just ran this script

 

<?php
$from = 'martin@mcgdesignstudio.com';
$to = 'martin@mcgdesignstudio.com';
if ( mail("$to", "Test mail from phpmail","This is a test PHP Email", "From: $from", "-f$from" ))
{
print "Mail sent";
}
else
{
print "failed";
}
?> 

 

And I got an email instantly...

 

Link to comment
Share on other sites

OK, add the -f to your script as below:

 

<?php

if(!$_POST) exit;

$email = $_POST['email'];


//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','message');
$required = array('name','email','message');

$to_email = "martin@mcgdesignstudio.com";
$from_email = "From: mcgdesignstudio.com  <martin@mcgdesignstudio.com>";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";

foreach($values as $key => $value){
  if(in_array($value,$required)){
	if ($key != 'subject' && $key != 'company') {
	  if( empty($_POST[$value]) ) { echo 'You need to fill in all the required fields...'; exit; }
	}
	$email_content .= $value.': '.$_POST[$value]."\n";
  }
}

if(mail($to_email, $email_subject, $email_content, "-f$from_email"))
        { 
          echo 'Your message has been sent successfully..';

          } else {

          echo 'Oops, something went wrong...try again later.';

          }

}

?>

Link to comment
Share on other sites

Hi. Try this.

 

   $to_email = "martin@mcgdesignstudio.com";

   $from_email  = 'MIME-Version: 1.0' . "\r\n";
   $from_email .= 'Content-type: text/plain; charset=UTF-8' . "\r\n";
   $from_email .= 'From: mcgdesignstudio.com <martin@mcgdesignstudio.com>' . "\r\n";

   $email_subject = "New Message: ".$_POST['subject'];
   $email_content = "new message:\n";
   
   foreach($values as $key => $value){
     if(in_array($value,$required)){
      if ($key != 'subject' && $key != 'company') {
        if( empty($_POST[$value]) ) { echo 'You need to fill in all the required fields...'; exit; }
      }
      $email_content .= $value.': '.$_POST[$value]."\n";
     }
   }
    
if(mail($to_email, $email_subject, $email_content, $from_email))
        { 
          echo 'Your message has been sent successfully..';

          } else {

          echo 'Oops, something went wrong...try again later.';

          }

}

 

or this if you don't want to send headers

 

   $to_email = "martin@mcgdesignstudio.com";

   $from_email  = "martin@mcgdesignstudio.com";

   $email_subject = "New Message: ".$_POST['subject'];
   $email_content = "new message:\n";
   
   foreach($values as $key => $value){
     if(in_array($value,$required)){
      if ($key != 'subject' && $key != 'company') {
        if( empty($_POST[$value]) ) { echo 'You need to fill in all the required fields...'; exit; }
      }
      $email_content .= $value.': '.$_POST[$value]."\n";
     }
   }
    
if(mail($to_email, $email_subject, $email_content, null, $from_email))
        { 
          echo 'Your message has been sent successfully..';

          } else {

          echo 'Oops, something went wrong...try again later.';

          }

}

 

Link to comment
Share on other sites

hi,

 

I might be wrong, but it seems to me that you are missing an argument here

if(mail($to_email, $email_subject, $email_content, "-f$from_email"))

 

Could you try like this:

if(mail($to_email, $email_subject, $email_content, $from_email, "-fmartin@mcgdesignstudio.com"))

 

Link to comment
Share on other sites

mail()

 

<?php
#boundary 1;
$boundary1 = rand(0,9)."-"
.rand(10000000000,9999999999)."-"
.rand(10000000000,9999999999)."=:"
.rand(10000,99999);

#to;
$user = 'martin@mcgdesignstudio.com';

#from email;
$from_email = 'martin@mcgdesignstudio.com';

#from;
$from = 'Wayne Gretzky <'.$from_email.'>';

#headers;
$headers  = 'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; boundary='.$boundary1. "\r\n" .
'Content-Transfer-Encoding: 8bit' . "\r\n" .
'From: ' . $from . "\r\n" .
'Reply-To: ' . $from_email . "\r\n";

#subject;
$subject = 'New Message: ' . $_POST['subject'];

#message;
$message  = 'Thank you for contacting us.<br /><br />' . "\r\n";
$message .= 'Please allow 24 hours for us to get back to you.' . "\r\n";

$mail = (mail ($user, $subject, $message, $headers, "-f {$from_email}") ? true : false);

if ($mail)
{ echo 'Sent!'; }
else
{ echo 'Failed!'; }
?>

 

that code will email somebody.  so, if it doesn't work, there's something wrong with the mail server, or there is something you are leaving out.

Link to comment
Share on other sites

another note dealing with email is security.  a lot of malicious activity can take place via a mail script, so beefing up security is near as important as when doing security for SQL injections, XSS attacks, etc.

 

an example of mail() code manipulation would be when an attacker adds aditional To:, Bcc:, Cc: addresses, and ultimately solicits thousands of emails using your form(s), server(s), routing, etc.  this can get you blackballed.

 

as well, tracking darts can also be placed with mail scripts to gather information from users, as well as malicious iframes can also be embedded within email body's set to execute code upon opening of email.

 

take all the necessary precautions when using email as you would with any other security.

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.