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 = "[email protected]";
$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 <[email protected]>\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
https://forums.phpfreaks.com/topic/180530-help-with-email-script/
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 = "[email protected]";
$from_email = "From: mcgdesignstudio.com  <[email protected]>";
$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.

 

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

 

I just ran this script

 

<?php
$from = '[email protected]';
$to = '[email protected]';
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...

 

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 = "[email protected]";
$from_email = "From: mcgdesignstudio.com  <[email protected]>";
$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.';

          }

}

?>

Hi. Try this.

 

   $to_email = "[email protected]";

   $from_email  = 'MIME-Version: 1.0' . "\r\n";
   $from_email .= 'Content-type: text/plain; charset=UTF-8' . "\r\n";
   $from_email .= 'From: mcgdesignstudio.com <[email protected]>' . "\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 = "[email protected]";

   $from_email  = "[email protected]";

   $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.';

          }

}

 

mail()

 

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

#to;
$user = '[email protected]';

#from email;
$from_email = '[email protected]';

#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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.