Solved

can't get php mail to work

Posted on 2009-04-09
11
286 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
  • 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP query / monitor data from Telnet to MySQL 7 49
Is there a simpler dropbox system? 10 34
What's wrong with this PDO query? 5 27
Ajax and PHP 9 30
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

861 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