14 Replies - 6001 Views - Last Post: 28 March 2009 - 11:56 AM Rate Topic: -----

#1 Nykc  Icon User is offline

  • Gentleman of Leisure
  • member icon

Reputation: 731
  • View blog
  • Posts: 8,644
  • Joined: 14-September 07

LAMP - PHP Mail() function question.

Posted 27 March 2009 - 05:39 PM

So I am working on a test script from a book I am reading which implements a mailing list and sends emails to all the subscribed users.

This is my first time using LAMP - I normally developed on my webhost server in a test section, but decided LAMP is better.

The form takes the information to distribute to the users.

<html>
<head>
<title>Make Me Elvis - Send Email</title>
</head>
<body>

	<h2><i>MAKE-ME-ELVIS.COM</i></h2>
	<P>Private: For Admin's use only<br />
	Write and send an email to mailing list members.</P>
	<form method="post" action="sendemail.php">
	<table border="0">
		<tr>
			<td><label for="subject"><strong>Subject of email:</strong></label></td>
			<td><input type="text" id="subject" name="subject" size="60" /></td>
		</tr>
		<tr>
			<td><label for="elvismail"><strong>Body of email:</strong></label></td>
			<td><textarea id="elvismail" name="elvismail" rows="8" cols="60"></textarea></td>
		</tr>
	</table>
		<p></p>
			<input type="submit" name="submit" value="Submit" />	
	</form>
</body>
</html>




Which works fine, and then I have the sendemail.php script

<html>
<head>
  <title>Make Me Elvis - Send Email</title>
</head>
<body>

<?php
  $from = 'mgmt@mail.com'; 
  $subject = $_POST['subject'];
  $text = $_POST['elvismail'];

  $dbc = mysqli_connect('localhost', 'nykc', '*****', 'elvis_store')
    or die('Error connecting to MySQL server.');

  $query = "SELECT * FROM email_list";
  $result = mysqli_query($dbc, $query)
    or die('Error querying database.');

  while ($row = mysqli_fetch_array($result)){
    $to = $row['email'];
    $first_name = $row['first_name'];
    $last_name = $row['last_name'];
    $msg = "Dear $first_name $last_name,\n$text";
    mail($to, $subject, $msg, 'From:' . $from);
    echo 'Email sent to: ' . $to . '<br />';
  } 

  mysqli_close($dbc);
?>

</body>
</html>



Which also seems to work fine. I am not getting any errors to work with so I am wondering is there some settings in LAMP to get the mail() function to work from using localhost?

I am pretty sure the code is correct, I even downloaded the code from the website to test that thinking I might have had a syntax error somewhere - and it also seems to work.

Any suggestions are appreciated.

edit- I also tried sending to a Gmail account as well as my main account to see if it was an issue with SMTP, or POP3. The emails never show up and never even go to the junk folder.

This post has been edited by Nykc: 27 March 2009 - 05:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: LAMP - PHP Mail() function question.

#2 ninethousandfeet@msn.com  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 310
  • Joined: 09-February 09

Re: LAMP - PHP Mail() function question.

Posted 27 March 2009 - 06:21 PM

i am not sure how LAMP works, but i have used MAMP with a few previous tutorials and the mail() does not work with their settings. i had to test through my actual web host for my mail script to work... your script looks okay so i'm guessing it has to do with the testing environment of LAMP.
hope that helps.
Was This Post Helpful? 1
  • +
  • -

#3 Nykc  Icon User is offline

  • Gentleman of Leisure
  • member icon

Reputation: 731
  • View blog
  • Posts: 8,644
  • Joined: 14-September 07

Re: LAMP - PHP Mail() function question.

Posted 27 March 2009 - 08:05 PM

Yes that is helpful - I believe that is the case as well, and it always helps to hear someone else say the same. I will set up the DB & Change the script to work on my webhost and test it out as well.

Next question would be then - how do I configure lamp to send mail via localhost?

Thank you for the response.
Was This Post Helpful? 0
  • +
  • -

#4 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: LAMP - PHP Mail() function question.

Posted 27 March 2009 - 08:44 PM

You're probably running into issues with the mail getting kicked out since it's not coming from the domain the sender address is at. Running it from your server should resolve this, provided the sender e-mail's domain matches the domain the mail is actually being sent from.
Was This Post Helpful? 0
  • +
  • -

#5 Nykc  Icon User is offline

  • Gentleman of Leisure
  • member icon

Reputation: 731
  • View blog
  • Posts: 8,644
  • Joined: 14-September 07

Re: LAMP - PHP Mail() function question.

Posted 27 March 2009 - 09:43 PM

It definitely makes sense but isn't there a way to send mail from localhost?

Well I guess I can go to apache\bin\php.ini and modify it there. I just can't see the point of setting up a test database on my live server when I have a production environment for that, it almost defeats the purpose.

edit - Was doing a little digging on Google and exim seems to be pretty popular choice and the answer to my problem or I will just have to test sendmail scripts on my live server.

Thanks for the help guys!

This post has been edited by Nykc: 27 March 2009 - 10:11 PM

Was This Post Helpful? 0
  • +
  • -

#6 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Re: LAMP - PHP Mail() function question.

Posted 27 March 2009 - 10:19 PM

check your port settings and see if you got a firewall or router blocking the smtp or pop3 ports which ever one lamp uses.
Was This Post Helpful? 0
  • +
  • -

#7 Nykc  Icon User is offline

  • Gentleman of Leisure
  • member icon

Reputation: 731
  • View blog
  • Posts: 8,644
  • Joined: 14-September 07

Re: LAMP - PHP Mail() function question.

Posted 27 March 2009 - 10:33 PM

No there isn't anything blocking the ports.
Was This Post Helpful? 0
  • +
  • -

#8 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: LAMP - PHP Mail() function question.

Posted 27 March 2009 - 10:37 PM

You could set up a local database, and allow connection from your server on a certain account, and temporarily change the script so that it connects to the local development database instead of the one on the server itself, and that might allow you to test it appropriately.
Was This Post Helpful? 1
  • +
  • -

#9 Nykc  Icon User is offline

  • Gentleman of Leisure
  • member icon

Reputation: 731
  • View blog
  • Posts: 8,644
  • Joined: 14-September 07

Re: LAMP - PHP Mail() function question.

Posted 27 March 2009 - 10:41 PM

Great idea - I like that idea better than setting up Sendmail or eXim. I think I will give that a shot first. I know the script works, but in the future there is a chance I will be doing a lot more testing with mail().
Was This Post Helpful? 0
  • +
  • -

#10 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: LAMP - PHP Mail() function question.

Posted 27 March 2009 - 10:46 PM

People so frequently use localhost for the DB connection that they often forget that can do otherwise.

I know this because if I'd remembered it when I made my first reply, I'd have suggested it then :P
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: LAMP - PHP Mail() function question.

Posted 28 March 2009 - 06:35 AM

Nykc, is this on Windows? I noticed you have a Windows-type path up there in post #5. If that's the case, it ain't LAMP :)
Was This Post Helpful? 0
  • +
  • -

#12 Nykc  Icon User is offline

  • Gentleman of Leisure
  • member icon

Reputation: 731
  • View blog
  • Posts: 8,644
  • Joined: 14-September 07

Re: LAMP - PHP Mail() function question.

Posted 28 March 2009 - 07:00 AM

apache\bin\php.ini?

Might not be the exact path i found it on Google - but it is LAMP - I use Ubuntu 8.04 LTS.
Was This Post Helpful? 0
  • +
  • -

#13 TMKCodes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 48
  • View blog
  • Posts: 440
  • Joined: 21-March 09

Re: LAMP - PHP Mail() function question.

Posted 28 March 2009 - 07:58 AM

As i know php mail() funktion needs working mail server, On my own server mail() funktion does not work and i don't have configured mail to work on it and as i know LAMP/ WAMP does not have mail server, but you can still send mail with smtp authentication.

Sending Mail from PHP Using SMTP Authentication - Example

<?php
require_once "Mail.php";

$from = "Sandra Sender <sender@example.com>";
$to = "Ramona Recipient <recipient@example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_password";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
	'auth' => true,
	'username' => $username,
	'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
 } else {
  echo("<p>Message successfully sent!</p>");
 }
?>



Sending Mail from PHP Using SMTP Authentication and SSL Encryption - Example

<?php
require_once "Mail.php";

$from = "Sandra Sender <sender@example.com>";
$to = "Ramona Recipient <recipient@example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "ssl://mail.example.com";
$port = "465";
$username = "smtp_username";
$password = "smtp_password";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
	'port' => $port,
	'auth' => true,
	'username' => $username,
	'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
 } else {
  echo("<p>Message successfully sent!</p>");
 }
?>


Was This Post Helpful? 0
  • +
  • -

#14 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Re: LAMP - PHP Mail() function question.

Posted 28 March 2009 - 10:24 AM

Yea that's a good point lol, did you install mail? and by that i mean the smtp, pop3, etc, protocols?
Was This Post Helpful? 0
  • +
  • -

#15 Nykc  Icon User is offline

  • Gentleman of Leisure
  • member icon

Reputation: 731
  • View blog
  • Posts: 8,644
  • Joined: 14-September 07

Re: LAMP - PHP Mail() function question.

Posted 28 March 2009 - 11:56 AM

I use evolution currenlty to send/receive mail - as for LAMP (No I never did set up or configure mail)

Thanks TMKCodes - I will try that method as well. I just figured after setting up LAMP it would have piggy backed on my ISP and sent the mail.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1