Forum Moderators: coopster

Message Too Old, No Replies

php database emailing

php database emailing

         

soldbychris

2:57 pm on Sep 25, 2008 (gmt 0)

10+ Year Member



I am looking for a way to send out daily emails with information from a database. I have tried using numerous emailing software but they are not configured to send a different message to the same email address daily. And the content can’t be uploaded from a database and has to be typed for each message. I have very limited experience with php.

If anyone could please point me to a resource to achieve what I need.

I need a php script that looks at a mysql database.

The mysql database has seven fields.

Date
Email
Paragraph1
Paragraph2
Paragraph3
Paragraph4
Paragraph5

The script needs to run daily and if the date matches in the database then it emails all Paragraphs out in one email to the email attached to that record.

And what is the best method to upload the data file from excel to the mysql database.

Thanks

eeek

10:18 pm on Sep 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I need a php script that looks at a mysql database.

What's wrong with just writing one that does what you want to do?

soldbychris

4:55 pm on Sep 26, 2008 (gmt 0)

10+ Year Member



I have never used PHP and I not sure what to do.

Is there a resource that has examples and or guide to get me started.

Thanks

soldbychris

9:56 pm on Sep 27, 2008 (gmt 0)

10+ Year Member



I have figured this much out and I get a email send, but no data. I know the query part works, but I need some help on getting the fields inserted into the body of the email. I don't have a clue on how to do it. All the resources that I can find are for input of data using a form. I need these results emailed.

If someone knows a good resource or could give me a suggestion, I would appreciate it.

<?php
// Make a MySQL Connection
mysql_connect("server.net", "#*$!x", "#*$!xx") or die(mysql_error());
mysql_select_db("#*$!x") or die(mysql_error());

$query = "SELECT `Date`,`Email`,`Par1`,`Par2`,`Par3`,`Par4`,`Par5` FROM `Emailing` WHERE DATE_FORMAT(Date,'%Y-%m-%d') = CURDATE()";

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
$to = "myemail";
$subject = "Hi!";
$body = "On a user connected to the database server";
$headers = "myemail";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
}

?>

venelin13

11:42 am on Sep 28, 2008 (gmt 0)

10+ Year Member



Here is a hint for you - inside the "while" loop, you can access the fields as $row['Par1']

To make your life easier, do this:

1) change this line:
$result = mysql_query($query) or die(mysql_error());

to:
$result = mysql_query($query, MYSQL_ASSOC) or die(mysql_error());

2) change this line:
$body = "On a user connected to the database server";

to:

$body = implode("\n\n", $row);

In this case, the message body will contains all the fields you've got from the database and also the fields will have two lines of space between each other.

If you want the body to contains just the paragraphs, you should build it manually:

$body = $row['Par1']."\n\n".$row['Par2']."\n\n."$row['Par3']."\n\n".$row['Par4']."\n\n".$row['Par5']."\n\n";

soldbychris

7:10 pm on Sep 28, 2008 (gmt 0)

10+ Year Member



Thanks for the tip.

This is what I did last night and it works.

I have never done php before so I am amazed that I have gone this far.

I just barely understand how this works. But I can't get a hyperlink into the body of the email that works.
It just show as text
<link rel="new" href="http://www.soldbychris.com" />

Any ideas to help or a resource where I can learn how to do it.

<?php
// Make a MySQL Connection
mysql_connect("server.net", "#*$!x", "#*$!xx") or die(mysql_error());
mysql_select_db("#*$!x") or die(mysql_error());

$query = "SELECT `Date`,`Email`,`Par1`,`Par2`,`Par3`,`Par4`,`Par5` FROM `Emailing` WHERE DATE_FORMAT(Date,'%Y-%m-%d') =

CURDATE()";

$result = mysql_query($query) or die(mysql_error());

$Par1 = 'Par1'; // Assign the value
$Par2 = 'Par2'; // Assign the value
$Par3 = 'Par3'; // Assign the value
$Par4 = 'Par4'; // Assign the value
$Par5 = 'Par5'; // Assign the value

while($row = mysql_fetch_array($result)){
$to = "email";
$from = "email";
$subject = "Real Estate Terms";
$body = "".$row['Par1'];
$body .= "".$row['Par2'];
$body .= "".$row['Par3'];
$body .= "\n";
$body .= "\n";
$body .= "".$row['Par4'];
$body .= "\n";
$body .= "\n";
$body .= "".$row['Par5'];
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
}

?>
</body>

venelin13

7:36 pm on Sep 28, 2008 (gmt 0)

10+ Year Member



Well, first of all, your HTML code is wrong! This is not a valid hyperlink code:

<link rel="new" href="http://www.soldbychris.com" />

You must use something like this:


<a href="http://www.example.org">click here</a>

Also, to put it into a PHP variable, you should escape the double quotes:


$body = "<a href=\"http://www.example.org\">click here</a>";

soldbychris

10:31 pm on Sep 28, 2008 (gmt 0)

10+ Year Member



When I put in this

$body = "<a href=\"http://www.example.org\">click here</a>";

It drops all of my data from the email and still does not show the hyperlink.

I am kinda stuck and do not know what to do.

Can you suggest a resource so I know how to structure my script since I really don't know what I am doing.

Any help would be appreciated.

thanks

soldbychris

11:19 pm on Sep 28, 2008 (gmt 0)

10+ Year Member



Since this is PHP script

adding a html in
\\\$body = "<a href=\"http://www.example.org\">click here</a>";

will not work or will it.

is there something for me to add to get it to recognize the hyperlink.

soldbychris

1:37 am on Sep 29, 2008 (gmt 0)

10+ Year Member




I was just informed that I have to send the email in html for it to work the way I want.

Thanks for you help.

venelin13

5:12 am on Sep 29, 2008 (gmt 0)

10+ Year Member



Well, there is a forth parameter, which holds all the necessary headers. One of them is the "content type". Read more in the PHP documentation:

[php.net...]

Also, why don't you try to use this PEAR package:
[pear.php.net...]

Here is an example of how it works:
[pear.php.net...]

axelotl

12:27 pm on Nov 16, 2008 (gmt 0)

10+ Year Member



This works for me as a simple and very configurable mass mailer - to use a date as a select parameter you would need to modify the SELECT query in the script - it currently grabs all email addresses in the DB:

Your HTML input form action calls the script below (I call mine 'mailer.php')

----------------------------------------------------
<?
//CONNECT TO YOUR DB
$username = "your_db_username";
$password = "your_db_password";
$db_name = "your_db_name";
$host = "localhost";
$cid = mysql_connect($host,$username,$password);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }

//GET THE DATA FROM THE INPUT FORM
if (isset($_POST['submit']))
{

$subject = $_POST['subject'];
$message = $_POST['message'];

//QUERY THE DB
$i = 1;
$query="SELECT 'anyfields_you_want' FROM your_db_table";
$result = mysql_db_query($db_name,"$query",$cid);
$numrows = mysql_num_rows($result);
if (!$numrows){
echo "<td>No emails in Database!</td>";}else{
while($row = @mysql_fetch_array($result)) {
$email = $row['email'];

//SEND THE E-MAIL TO THE ADDRESSES IN RESULT.

mail($email, $subject, $message, "From: Me at My Company<www.mywebsite.com>\nX-Mailer: PHP/" . phpversion());
echo "Email sent to: " . $email . "<br />";
$i++;
}
}
}
?>

-------------------------------------------------------

The form is below:

-------------------------------------------------------

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<form name="massmailform" action="mailer.php" method="post">
Subject <br />
<input name="subject" type="text" size="50" id="subject">
<br />
<br />
Message <br />
<textarea name="message" cols="50" rows="10" id="message"></textarea>
<br />
<br />
<input type="submit" name="submit" value="email">
</form>
</body>
</html>

--------------------------------------------------------------------