Fire an automated email when database updated? - php

I am trying to figure out the best way to send an automated email to a customer when we update our database with "frames in" i'm thinking javascript & php but don't really know how to implement as a nobo!?
My HTML Form showing checkbox that needs to fire email (only partially shown due to length)
<form action="<?php echo $editFormAction; ?>" name="form" method="POST">
<input name="frame_in" type="checkbox" id="long_tiny" value="yes" <?php if (!(strcmp($row_Recordset1['frame_in'],"yes"))) {echo "checked=\"checked\"";} ?> />
<input type="hidden" name="MM_update" value="form">
</form>
The php mail script (Not sure if completely correct)
$mailTo = $row_customer['email'];
$subject = 'Your Frames Now in!!';
$cName = $row_Recordset2['cName'];
$jobRef = $row_Recordset2['customer_ref'];
$ourRef = $row_Recordset2['our_ref'];
$jobTotal = $row_recordset2['amount'];
mail($mailTo, $subject,
$cName your job Ref: $jobref is now in
<br>
<h2>Details:</h2>
<p>Our ref: $ourRef<br>
Customer Ref: $jobref<br>
Job total: $jobtotal</p>
);
I really don't know how to tie it all together I'm guessing there would need to be error handling as not every customer has an email (But the error doesn't need to be shown)

$mailTo = $row_customer['email'];
$subject = 'Your Frames Now in!!';
$cName = $row_Recordset2['cName'];
$jobRef = $row_Recordset2['customer_ref'];
$ourRef = $row_Recordset2['our_ref'];
$jobTotal = $row_recordset2['amount'];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = "$cName your job Ref: $jobref is now in
<br>
<h2>Details:</h2>
<p>Our ref: $ourRef<br>
Customer Ref: $jobref<br>
Job total: $jobtotal</p>";
if($_POST['frame_in'] == "yes") {
mail($mailTo, $subject,$message, $headers);
}

This is a very broad question. What you need to do is:
Process the form in $editFormAction
If $_POST['frame_in'] is set, do the following:
Get all customers
Send an email to the ones with an e-mail address
Publish a warning on your site for logged-in users (or in general...) that do not have an email address registered.
No aditional javascript needed.
Where exactly are you stuck?

You could try a database trigger that sends an email. The database itself would be sending out the e-mail, and would not involve php at all. This would also be more inclusive, as it would send an email regardless or who/what changes the field, including changes from PhpMyAdmin, or other webpages/scripts.
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
http://forums.mysql.com/read.php?99,33635,33635#msg-33635
This may be limited depending on your hosting environment. If you are using shared hosting, odds are you cannot utilize triggers for security reasons.

Related

how to send a message to email with php

I'm trying to send a message to the email a user provides in the contact form. The problem is the message never gets sent, but I always arrive at a blank page where my php code is located. Nothing warns me of any error in my code. Can anyone explain why this is happening and offer a solution to the problem?
<form action="site.php" method="POST">
<input
type="text"
class="form"
name="email"
placeholder="Your email address"
/>
<button class="submit" type="submit">Join Waitlist</button>
</form>
<?php
if (isset($_POST["submit"]))
{
$mailTo = $_POST["email"];
$mailFrom = "Dumele";
$message = "https://docs.google.com/forms/d/1lpj2XnKW4HT_qHFfGwpUxcvzPmK2USZ0MGSDP0XCqfg/edit";
$subject = "Welcome to Dumele";
$txt = "Thank you for your interest in Dumele. We're glad to have
you join our network and mission to enhance the technological
innovation of our African diaspora. Below is a link to a survey
we would like you to answer so we can better assist you.\n\n".message;
$headers = "From: ".mailFrom;
(mail($mailTo, $subject, $txt, $headers));
header("Location: index.php?mailsend");
}
?>
First of all make sure you enabled error reporting. You can check another Stackoverflow question and it's answers here about it.
As I see in your code you have syntax errors. You didn't place $ sign before variable names. For example you typed $headers = "From: ".mailFrom; instead of $headers = "From: ".$mailFrom; Let's fix it:
<?php
if (isset($_POST["submit"]))
{
$mailTo = $_POST["email"];
$mailFrom = "Dumele";
$message = "https://docs.google.com/forms/d/1lpj2XnKW4HT_qHFfGwpUxcvzPmK2USZ0MGSDP0XCqfg/edit";
$subject = "Welcome to Dumele";
$txt = "Thank you for your interest in Dumele. We're glad to have
you join our network and mission to enhance the technological
innovation of our African diaspora. Below is a link to a survey
we would like you to answer so we can better assist you.\n\n".$message;
$headers = "From: ".$mailFrom;
(mail($mailTo, $subject, $txt, $headers));
header("Location: index.php?mailsend");
}
Now with the mail() function of PHP; some servers disables mail() function for security purposes. If so; you can use SMTP to securely send your emails. To use SMTP in PHP of course you need additional processes but some free software packages and libraries like PHPMailer or SwiftMailer can help you about it.
This is looking for a form value with the name "submit":
if (isset($_POST["submit"]))
But there's no form element in the HTML with that name. So this will always be false. Give your submit button that name:
<button class="submit" type="submit" name="submit">Join Waitlist</button>
It shouldn't necessarily need a value, it would just default to an empty string. But it needs a name in order for the browser to send anything at all with that key.
As an aside, your mail server may reject the message since this is not really an email address:
$mailFrom = "Dumele";
For completeness... It looks like your PHP variables are also syntactically incorrect. Variable names need to begin with a $. For example, this:
$headers = "From: ".mailFrom;
Should be this:
$headers = "From: ".$mailFrom;
The same error would need to be corrected anywhere you're mis-using variable names.
Use value attribute in button tag. You are testing
if(isset($_post['submit']))
But what is submit? You should use value attribute and give a value submit i.e. Submit

Accept and record condition value from received HTML email form

I'm trying to figure out with email confirmation function via email, received by user.
If I have <input type='submit' value='Accept' id='btn1'>, (which is correct way shown by ADyson, instead double quotes: <input type="submit" value="Accept" id="btn1">), then how do I use PHP $_POST['acceptval'] for confirmation from this HTML, which must record value to MySQL database from received message form by included ID, then to pass this condition from the website.
So, if I will include require 'connect.php'; to code below, and will get data for to, message, subject, I will receive message to my userreceiveremail#gmail.com, then I want allow user press button, and record value to his record field, but I can't figure out, how it works:
<?php
$to = "userreceiveremail#gmail.com";
$subject = "Confirmation notification";
$message = "
<html>
<head>
<title>Accept registration</title>
</head>
<body>
<p>Hello, dear User! Please, accept your registration</p>
<input type='submit' value='Accept' id='btn1'>
</body>
</html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <senderemail#gmail.com>' . "\r\n";
$headers .= 'Cc: Some text' . "\r\n";
mail($to,$subject,$message,$headers);
echo "Mail sent successfully";
?>
Maybe my scenario does not correspond to the classic or correct approach to solving this particular task, because I'm just trying to figure out, how to get desired result, anyway yet I can't find any close example for this.
Any advice, guide or example would be very helpful

Best way to send emails generated by visitors using website get quota form

I'm struggling with this issue for months now.
I have a simple website wich has a "Get price quota" php form that interested visitors can use to ask for prices.
The PHP form sends an email to the admin using PHP's "mail".
Everything works fine with Google, Hotmail, etc EXCEPT, I get my email rejected all the time from Yahoo recipients. Every two or three months I have to get my dedicated IP changed and the hosting company to make unblacklisting requests because emails sent back to the customers bu the admin using email accounts on the same server\domain are rejected. Weird or not, not all of them are rejected.
I went through all info and tips I could find about securing my form, checked the logs to see if somehow my domain\website is used for spam, I have DKIM enabled, etc, nothing works, I know Yahoo has bad problems but I wonder if there is another way to send emails from forms, small amounts, max 100 per month.
I'm going to post my php email script below, please tell me if there is something wrong that could cause Yahoo to blacklist my domain or is it just Yahoo.
//Secure The Submitted Data
$quoteFirstName = stripslashes($_POST["quoteFirstName"]);
$quoteLastName = stripslashes($_POST["quoteLastName"]);
$quoteEmail = stripslashes($_POST["quoteEmail"]);
$quoteDetails = stripslashes($_POST["quoteDetails"]);
$quoteProduct = stripslashes($_POST["quoteProduct"]);
$quoteTel = stripslashes($_POST["quoteTel"]);
$mailtolink = '' . $quoteEmail .'';
//Build The Email To The Admin
$to = "orders#mydomain.com";
$subject = "Price quota request";
$theEmail = "$quoteFirstName $quoteLastName <br /> $mailtolink <br /> Telephone $quoteTel <br /> Price quota for <br /> - $quoteProduct.<br /> Additional info: $quoteDetails";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: " . $quoteEmail . "\r\n" . "Reply-To: " . $quoteEmail . "\r\n" . "X-Mailer: PHP/" . phpversion();
mail($to, $subject, $theEmail, $headers);

retrieve the receiver's email address using html

So I am sending an HTML e-mail which is an invitation, and I would want users to click Join! if they would want to join the invitation, but then I have no idea how to retrieve their e-mail addresses. I can only get hits of how many "Yes"es I have got, I can't see which e-mail has actually said yes. How can I get their e-mail address too? beside providing them with a text field asking them to fill it up.
Any thoughts? thanks :)
My cade so far:
<html>
<body>
<form action="myserver.com" method="POST" target="_blank">
<input type="hidden" name="answer" value="yes"/>
<input type="submit" name="submit" value="Join!"/>
</form>
</body>
</html>
For the server side, I am using PHP to retrieve the data. Could anything be done from there? I can't use PHP or JS in my e-mails as far as I know.
Since you mention PHP, why not send the email programmatically?
Assuming you have a working SMTP configuration (see Local SMTP), all email adresses in an array, you could use PHP to send out the emails.
See the examples at the resources for specific headers needed for HTML
<?php
$email_addresses = array('foo#bar.tld', 'no#name.it');
// subject
$subject = 'Invitation';
// message
$message = '
<html>
<body>
<form action="myserver.com" method="POST" target="_blank">
<input type="hidden" name="answer" value="yes"/>
<input type="hidden" name="email" value="%placeholder%" />
<input type="submit" name="submit" value="Join!"/>
</form>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set - general part of the header
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Invitation <your_email#example.com>' . "\r\n";
// iterate over all email addresses
foreach ($email_addresses as $address)
{
// specific version of header
$header = $headers . 'To: ' . $address . ' . "\r\n";
// specific version of message
$msg = str_replace('%placeholder%', $address, $message);
// Mail it
mail($address, $subject, $msg, $header);
}
?>
As always, there are many ways to rome, so you could also input a link into the mail which has the email address (or some other sort of ID) as GET parameter.
Take special care with encodings (ISO vs UTF).
Also, your webserver will be the sender of the Email, so it might get caught in spamfilters if the address differs from the FROM field.
Getting PHP's mail() function to work can be a bit tricky, but the internet is full of solutions, so good luck!
You would have to put their address in the href url as a parameter. It is the only way to get it back.
I won't bother putting all the PHP mail code in here but when you pass something like this:
$address = "their.email#gmail.com";
$msg = '... click here ...'
mail($address, $subject, $msg, $header);
It will render in the email like this:
click here
When they click it, it will send the parameter to your webpage, where you can access it with $_GET['email']

Mail OneClick errormessage

I'm developing a site on whichs record are displayed after they are read from a .csv file.
Sometimes fields contain errors, and I would like to display a button/link that automatically sends me an email when a user clicks the button.
Now it works like this:
if ($url == null && $naam == null) {
echo "\n<li><span class=\"naam\">Foute Invoer.</span>\n<br /><span class=\"error\">Fout melden</span></li>\n";
}
Now the users still have to click send in Outlook/whatever email software they are using, and can edit the message too.
I'd like to have a button that automatically sends an email to me when users see an error.
I tried adding the following form instead of the mailto-link:
<form action="" method="post">
<input type="submit" value="Zend foutmelding" />
<input type="hidden" name="button_pressed" value="1" />
</form>
<?php
if(isset($_POST['button_pressed']))
{
$to = 'myname#institution.be';
$subject = 'Errorcode';
$message = 'File: ' . $csv . ' Line: ' . $line;
$headers = 'From: myname#institution.be';
mail($to, $subject, $message, $headers);
echo 'Email Sent.';
}
?>
But that way it sends an email for every error on the page (for example, the page I tested it on contained 3 errors, so I got three emails.)
Is there a solution where I only get one email, for the record selected? Because this site will have thousands of records, so I can't receive a thousand emails a day for every error on the site.
UPDATE + ANSWER
Though I know this is not the best solution for my problem, I have found a solution for the question I asked first:
I created an id for each record
$id = $filter . "_" . $line;
Then I changed the form to:
<form action="" method="post">
<input type="submit" value="Zend foutmelding" />
<input type="hidden" name="<?php echo $id; ?>" value="1" />
</form>
<?php
if(isset($_POST[$id]))
{
$to = 'myname#institution.be';
$subject = 'Errorcode';
$message = 'File: ' . $csv . ' Line: ' . $line;
$headers = 'From: myname#institution.be';
mail($to, $subject, $message, $headers);
echo 'Email Sent.';
}
?>
Yes, it works.
Yes, almost to simple to ask here, I know...
Now I will continue to think about more elegant solutions which really solve my problem with error-reporting.
Thanks for helping and thinking along with me.
You wont come along without logging here.
The best is make a table with errors code and timestamps so every errorcode will be selected, and if not available today recorded and then an E-Mail will be sent.
Without writing down what you already sent it will be very hard to make it work, except going by session or something like that but this is not a very clear solution cause every different use rwill generate an E-Mail again.

Categories