Solved

can't get php mail to work

Posted on 2009-04-09
11
284 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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 …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

808 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