Forum Moderators: coopster
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
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>");
}
}
?>
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";
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>
<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>";
$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
[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...]
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>
--------------------------------------------------------------------