Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

can't get php mail to work

Posted on 2009-04-09
11
Medium Priority
?
291 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

688 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