Solved

can't get php mail to work

Posted on 2009-04-09
11
282 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now