Solved

can't get php mail to work

Posted on 2009-04-09
11
289 Views
Last Modified: 2012-05-06
I found this script that works on my buddy's server but not mine.  Can't figure it out.  It's basically has a form validation script and once validating, it should send out the email.  The validation works fine but I get no email though.  Any ideas?  I have php version 5.2.5 on my server and apache.
<?php
 
$errors = array(); // set the errors array to empty, by default
$fields = array(); // stores the field values
$success_message = "Your form has been submitted. A representative from Franchise Business Daily will contact you shortly. Thank you!";
 
if (isset($_POST['submit']))
{
  // import the validation library
  require("php/validation.php");
 
  $rules = array(); // stores the validation rules
 
  // required form fields
  $rules[] = "required,firstName,Please enter your First Name.";
	$rules[] = "required,lastName,Please enter your Last Name.";
  $rules[] = "required,email,Please enter your email address.";
  $rules[] = "valid_email,email,Please enter a valid email address.";
	$rules[] = "range=1-100,number_range,Please enter a number between 1 and 100 for anti spam purposes.";
	$rules[] = "required,subscribe,Please check Yes or No for receiveing E-Mails."; 
	
  $errors = validateFields($_POST, $rules);
 
  // if there were errors, re-populate the form fields
  if (!empty($errors))
  {  
    $fields = $_POST;
  }
  
  // no errors! redirect the user to the thankyou page (or whatever)
  else 
  {
  
mail( "some@email.com", "Test Email","First Name = $firstName\nLast Name = $lastName\nPhone Number = $phone\nE-mail = $email\nMy Website = $website\nIndustry of Interest = $category\nQuestion 1 = $question1\nQuestion 2 = $question2\nQuestion 3 = $question3\nNumber = $number_range\nMessage = $message\nSubscribe to Emails = $subscribe\n", "From: $firstName <$email>");
 
$message = "Your form has been submitted.";
    
    // here you would either email the form contents to someone or store it in a database. 
    // To redirect to a "thankyou" page, you'd just do this:
    // header("Location: thanks.php");
  }
 
}
 
?>

Open in new window

0
Comment
Question by:COwebmaster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 14

Expert Comment

by:shobinsun
ID: 24106899
I think your php mail function is not working.

Go to the terminal and type man mail or simply type mail end hit enter.

Then you can confirm that mail is existing or not.

Or you can use SMTP mail function.

Regards.

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24113148
Have you configured php to send email? You may need to modify php.ini:

http://php.net/manual/en/mail.configuration.php
0
 

Author Comment

by:COwebmaster
ID: 24115478
I'm looking on the phpinfo.php page and under sendmail_from, it says 'no value'.  Should it say something there?  Sendmail_path says /usr/sbin/sendmail -t -i
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:COwebmaster
ID: 24115545
why would this work..

$Name = "Da Duder"; //senders name
$email = "myemail@email.com"; //senders e-mail adress
$recipient = "formemail@email.com"; //recipient
$mail_body = "test"; //mail body
$subject = "Subject for reviever"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields

mail($recipient, $subject, $mail_body, $header); //mail command :)

but not this..

mail( "myemail@email.com", "Daily Email","First Name = $firstName\nLast Name = $lastName\nPhone Number = $phone\nE-mail = $email\nMy Website = $website\nIndustry of Interest = $category\nQuestion 1 = $question1\nQuestion 2 = $question2\nQuestion 3 = $question3\nNumber = $number_range\nMessage = $message\nSubscribe to Emails = $subscribe\n", "From: $firstName <$email>");

Also, in my php5.ini file, for email, I have:

[mail function]
; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
sendmail_from = me@localhost.com
sendmail_path = /usr/sbin/sendmail -t -i  
;mail.force_extra_parameters =

does something need changed there?
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24115637
If mail() works, you don't need to change the configuration.

The only possibly significant difference I can see between those two mail() calls, is in the From header. The working one ends the header with \r\n while the non-working one lacks these characters. Is $email defined in the second version?
0
 

Author Comment

by:COwebmaster
ID: 24116013
I can use the first example but how can I place form fields inside the body of the email?  In other works, this doesn't work..

$Name = "Da Duder"; //senders name
$email = "myemail@email.com"; //senders e-mail adress
$recipient = "formemail@email.com"; //recipient
$mail_body = $firstName;    <--- this doesn't work
$subject = "Subject for reviever"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields

mail($recipient, $subject, $mail_body, $header); //mail command :)
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24116226
$mail_body = $firstName;    <--- this doesn't work

Where is $firstName defined? If this is input from a form, it should be:

$mail_body = $_REQUEST['firstName'];
0
 

Author Comment

by:COwebmaster
ID: 24116240
yes it is.  I'll try that.
0
 

Author Comment

by:COwebmaster
ID: 24120085
Ok, that worked.  However, how can I place multiple form fields in the body of the email, not just 1 field?  This threw an error...

$Name = "test1"; //senders name
$email = "email@email.net"; //senders e-mail adress
$recipient = "email@email.net"; //recipient
$mail_body = $_REQUEST['firstName'], $_REQUEST['lastName'];
$subject = "Subject for reviever"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields

mail($recipient, $subject, $mail_body, $header); //mail command :)
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24120116
Concatenate with dots:

$mail_body = 'First name: '.$_REQUEST['firstName']."\n".
                      'Last name: '.$_REQUEST['lastName']."\n";
0
 

Author Comment

by:COwebmaster
ID: 24129408
Thanks cxr.   That worked great.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question