php auto sending mail from database? - email

php auto send mail at time and have setting?
i want create email and save database
at time email auto send to email
ex:i setting at 8:00 PM
Send email1 (to,subject1,message1),send email2(to,subject2,message2)....
Because i use database mysql so, i want auto send subject + message if status =0
status =0 is no send, status =1 is old send.
Please help.
Thanks.

You would need to use a cron job to visit a page:
0 20 * * * curl http://yourwebsite.com/emailscript.php
^Fires off at the 0 minute, of the 20th hour (8:00pm), of every day, of every week, of every month.
contents of emailscript.php:
$to = "email#email.com";
$subject = "Cron Job HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags and is a test of the cron jobs!</p>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: email#yoursite.com <email#yoursite.com>' . "\r\n";
mail($to,$subject,$message,$headers);

Related

Why might php mail sent but not received unless I manually call the script?

Why might php mail not be received in the second case where twilio is calling the page? Are there characters that might be returned from the api that make the email undeliverable?
1) If I type the URL into the broweser, it attempts to make a wav file in my recordings folder and I get the emails and text messages to all the recipients. The output below is served and the "1" indicates the message was sent.
2) If twilio calls the page and handles the xml, the actual message is recorded to my server, and the output below is served to twilio (which I have verified in my account), but no one receives the email or text. The body of the xml still shows "1" after good bye, indicating the message was sent.
My script:
<?php
date_default_timezone_set('America/New_York');
//copy the remote wav file to my server
$recording = file_get_contents($_REQUEST['RecordingUrl']);
$name = "recordings/".str_replace("+","",$_REQUEST['Caller'])."-".date('Y-m-d-G-i-s',time()).".wav";
$fh = fopen("../".$name, 'w') or die("can't open file");
$stringData = $recording;
fwrite($fh, $stringData);
fclose($fh);
//email the people that need to get the message
$to = "email1#yahoo.com";
$subject = "Voicemail from ".$_REQUEST['From'];
$message = "Click below to listen to your message:\n\r http://domain.com/twilio/".$name;
$from = "email2#domain.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
$to = "email3#domain.com";
mail($to,$subject,$message,$headers);
$to = "9545555555#messaging.sprintpcs.com";
$sent = mail($to,$subject,$message,$headers);
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n
<Response>
<Say>Thank you for your message. Good bye.".$sent."</Say>
<Hangup/>
</Response>";
?>
outputs:
<Response>
<Say>Thank you for your message. Good bye.1</Say>
<Hangup/>
</Response>
I just check to see what the contents of each variable in the php mail function are when twilio calls the script to see if there is a character present from a live twilio message. It comes out like this:
$to = 9545555555#messaging.sprintpcs.com
$subject = Voicemail from +19545555555
$message = Click below to listen to your message:
http://domain.com/twilio/recordings/19545555555-2013-10-12-22-57-03.wav
$headers = From:email2#domain.com
If I call the script manually in the browser they are:
$to = 9545555555#messaging.sprintpcs.com
$subject = Voicemail from
$message = Click below to listen to your message:
http://domain.com/twilio/recordings/-2013-10-12-23-04-37.wav
From:email2#domain.com
I tried removing the "+" out of the subject with no success, but when I removed the entire phone number from the subject it works every which way. The emails and texts are delivered appropriately. It makes no sense to me. Maybe its godaddy webhosting messing with my life again.
Perhaps the from string has bad characters that ruin the mail() call, but echo transparently. Try cleaning your string before putting it into mail().
Something like:
$subject= preg_replace("/[^a-zA-Z0-9]+/", "", $_REQUEST['From']);

Reply-To header not picking the php variable

I am creating a page that sends the response from contact form within mail but reply-to is not working (the variable i am using is having value, but is not added within headers)
here's my code:
<?php
//$uname=$_REQUEST['uname'];
if(isset($_REQUEST['name']))
{
$name=$_REQUEST['name'];
}
if(isset($_REQUEST['email']))
{
$email=$_REQUEST['email'];
}
if(isset($_REQUEST['phone']))
{
$phone=$_REQUEST['phone'];
}
if(isset($_REQUEST['message']))
{
$message=$_REQUEST['message'];
}
// TESTING IF VARIABLES HAVE VALUES
echo "$name $email $phone $message";
// RESULT: TRUE TILL HERE
if($name=="" || $email=="" || $phone=="" || $message=="")
{
header("location:../?inst=invalid");
}
else
{
// ---------------- SEND MAIL FORM ----------------
// send e-mail to ...
$to="mail#example.com";
// Your subject
$subject="$name Contacted you via Contact Form";
// From
$headers = "From: ME <no-reply#example.com>\r\n";
$headers .= 'Reply-To:' . $email . "\r\n";
$headers .= "Return-Path: info#example.com\r\n";
$headers .= "X-Mailer: PHP\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
print $message;
// send email
$sentmail = mail($to,$subject,$message,$headers);
//$sentmail = mail($to1,$subject,$message,$header);
}
// if your email succesfully sent
if($sentmail){
echo "Mail Sent.";
}
else {
header("location:../landing/?status=verification-pending");
}
?>
Now when i checked headers in my gmail, the value for $email doesn't appear in header information, Also no message is received. All I get is a blank message or may be $message is not printing anything like the same case i am facing with reply-to.
please help me a little with this. Thanks in advance.
Check that you have php warnings and notices enabled. When you are echoing out $name, $email, etc you are doing a redirect using headers after that. If your php notices etc aren't turned on, your header redirect will fail due to having already echoed something, and you won't know that you had invalid input. This is part of the reason you shouldn't echo things out during logic, but should store and out put values later.
Actually there was a little mistake with the above code. I mistakenly added = instead of == while comparison.
This line:
if($name=="" || $email="" || $phone="" || $message="")
Should read as:
if($name=="" || $email=="" || $phone=="" || $message=="")
Since = is for equation and not a condition for comparison ==
Fixed it in the Question

PHP mail function not working

I do not think the problem lies with the mail function code, but with my aproach of the $_SESSION variables. I have a form consisting of 5 pages, the fifth being a preview page. Upon the final submission, on the preview page, i want the entire $_SESSION data to be sent to two different email adresses.
I am displaying the data on the preview page as follows:
<?php
//retrieve session data
echo "<b> Varname: </b>". $_SESSION['varname'];
?>
in a form with method="post" and action="mail.php".
In the mail.php, i start the session, and then:
$_SESSION['email'] = $mail;
$_SESSION['varname'] = $varname;
$email_from = 'mail#company.de';
$email_subject = "Mail";
$email_body = "You have submitted the following data: $inhalt.\n";
$to = "mymail#company.de, $mail";
$headers = "From: Company";
mail($to,$email_subject,$email_body,$headers);
Upon submitting the form the page goes to blank. What exactly am i doing wrong?
I managed to solve the problem in the end, as follows:
$email_from = 'mail#company.de';
$email_subject = "Mail";
$to = ("myadress#work.de," . $_SESSION['email'] . "");
mail($to,"Form submission","Form data:
Inhalt: " . $_SESSION['inhalt1'] . "
");
As I said in the question, the problem was my aproach of the $_SESSION variables. Instead of $_SESSION['varname'] = $varname, i just went directly with . $_SESSION['varname'] .ยด.

PHP mail to variable

I've searched and searched for something similar, but i think i'm not doing it right. So i will ask a question. This is very basic. My problem is as follows:
I have a multi-page form, consisting of 4 pages + 1 preview page. On the preview page, upon submitting i want the entire form data to be sent to 2 different mail adresses. One standard, and the other one, the one that the user has submitted.
So i have:
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
?>
<?php
$email_from = 'mail#company.com';
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "yourname#yourwebsite.com, $email /n";
$headers = "From: Company";
mail($to,$email_subject,$email_body,$headers);
?>
How should I insert the submitted $email variable in order for it to work?
Remove the \n
$to = 'yourname#yourwebsite.com,' . $visitor_email;
According to the mail() function documentation the $to parameter will take a comma-separated list of addresses as you have attempted, but should have no line break at the end.
Also, your variable is $visitor_email, rather than $email.
$to = "yourname#yourwebsite.com, $visitor_email";
You might also consider adding the visitor's email as a CC or BCC rather than the TO address. In that case, add it as a CC or BCC header (separated by \r\n), but you need to be cautious to be sure that the address is an email address and contains no line break characters which could be used for header injection.
// The From should be an email address
$headers = "From: company#example.com\r\n";
$headers .= "CC: $visitor_email;

PHP form mail and register globals

I have a form and php processing file, and everything seems to be working fine except that the user-entered variables are not showing up in the emailed form results. From the research I've done (I'm beginner PHP), I'm guessing it's because my script uses HTTP_POST_VARS and my client's server has globals turned off? They recently switched from Godaddy Windows to Godaddy Linux hosting.
This is my processing file:
<?php
// where is your config file stored?
include ("ajaxSubmit.php");
// CLIENT INFORMATION
$Contactname = $HTTP_POST_VARS['Contactname'];
$email = $HTTP_POST_VARS['email'];
$Contacttitle = $HTTP_POST_VARS['Contacttitle'];
$Business = $HTTP_POST_VARS['Business'];
$Address = $HTTP_POST_VARS['Address'];
$City = $HTTP_POST_VARS['City'];
$State = $HTTP_POST_VARS['State'];
$Zip = $HTTP_POST_VARS['Zip'];
$Phone = $HTTP_POST_VARS['Phone'];
$Fax = $HTTP_POST_VARS['Fax'];
$product_desc = $HTTP_POST_VARS['product_desc'];
$delivery = $HTTP_POST_VARS['delivery'];
$sku = $HTTP_POST_VARS['sku'];
$annualturns = $HTTP_POST_VARS['annualturns'];
$seasonal = $HTTP_POST_VARS['seasonal'];
$minmaxpallet = $HTTP_POST_VARS['minmaxpallet'];
$avgpalletval = $HTTP_POST_VARS['avgpalletval'];
$avgpalletwt = $HTTP_POST_VARS['avgpalletwt'];
$maxpalletht = $HTTP_POST_VARS['maxpalletht'];
$casesperpallet = $HTTP_POST_VARS['casesperpallet'];
$unitweight = $HTTP_POST_VARS['unitweight'];
$reqlotnumctrl = $HTTP_POST_VARS['reqlotnumctrl'];
$freightclass = $HTTP_POST_VARS['freightclass'];
$hazardclass = $HTTP_POST_VARS['hazardclass'];
$barcodes = $HTTP_POST_VARS['barcodes'];
$avgupsfedex = $HTTP_POST_VARS['avgupsfedex'];
$avgorderweight = $HTTP_POST_VARS['avgorderweight'];
$ordersending = $HTTP_POST_VARS['ordersending'];
$custpickups = $HTTP_POST_VARS['custpickups'];
$flatfiles = $HTTP_POST_VARS['flatfiles'];
$shrinkwrap = $HTTP_POST_VARS['shrinkwrap'];
$repack = $HTTP_POST_VARS['repack'];
$specialreq = $HTTP_POST_VARS['specialreq'];
// MODIFY THE FOLLOWING SECTION
// your name
$recipientname = "Company X";
// your email
$recipientemail = "email#email.com";
// subject of the email sent to you
$subject = "Quote Request for $recipientname";
// send an autoresponse to the user?
$autoresponse = "yes";
// subject of autoresponse
$autosubject = "Thank you for your mail!";
// autoresponse message
$automessage = "Thanks for the message. We've successfully received your quote request and will get back to you shortly.";
// thankyou displayed after the user clicks "submit"
$thanks = "Thank you for contacting us. We will get back to you as soon as possible.";
// END OF NECESSARY MODIFICATIONS
// format message
$message = "Online-Form Response for $recipientname:
<br>
Contact Name: $Contactname
<br>
Business: $Business
<br>
Email: $email
<br>
Address: $Address
<br>
City: $City
<br>
State: $State
<br>
Zip: $Zip
<br>
Phone: $Phone
<br>
Fax: $Fax
<br>
<br>
<br>
Describe your product(s): $product_desc
<br>
How will your product be delivered?: $delivery
<br>
How many SKU's (items): $sku
<br>
How many turns per year?: $annualturns
<br>
Are your products seasonal?: $seasonal
<br>
Indicate minimum and Maximum pallet levels: $minmaxpallet
<br>
Average value per pallet: $avgpalletval
<br>
Weight of a typical pallet: $avgpalletwt
<br>
Maximum pallet stacking height: $maxpalletht
<br>
Cases per pallet? Or average case size?: $casesperpallet
<br>
Weight of each unit?: $unitweight
<br>
Do you require lot number control?: $reqlotnumctrl
<br>
What freight class?: $freightclass
<br>
Is the product hazardous? If so, what classifications?: $hazardclass
<br>
<br>
<br>
Do you need custom Barcodes made?: $barcodes
<br>
What is the average number of orders shipped via UPS/Fedex?: $avgupsfedex
<br>
What is the average order size in weight?: $avgorderweight
<br>
What is the average number of lines per order?: $avgorderlines
<br>
Will your orders be sent via E-mail, FAX, or other?: $ordersending
<br>
Will you have customer pick ups, and how often?: $custpickups
<br>
Can your company e-mail us flat files?: $flatfiles
<br>
Do your orders need to be shrink wrapped?: $shrinkwrap
<br>
Do you need repackaging?: $repack
<br>
Are there any special requirements that your company may have?: $specialreq
<br>";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: Company X <email#email.com>' . "\r\n";
// send mail and print success message
mail($recipientemail, $subject, $message, $headers);
if($autoresponse == "yes") {
$autosubject = stripslashes($autosubject);
$automessage = stripslashes($automessage);
mail($email,"$autosubject","$automessage","From: $recipientname <$recipientemail>");
}
echo "<script language=\"JavaScript\" type=\"text/JavaScript\"> window.location.href = \"thanks_quote.php\";</script>";
?>
EDIT: I tested on another server (with globals on), and the form worked fine, but I'm not sure how to rewrite my script so that it does not rely on POST?
The HTTP_POST_VARS is now deprecated. Also register_globals is deprecated in 5.3.0 and above. So use $_POST instead of HTTP_POST_VARS.
Also, instead of last line:
echo "<script language=\"JavaScript\" type=\"text/JavaScript\"> window.location.href = \"thanks_quote.php\";</script>";
to redirect to another page, use:
header('Location: thanks_quote.php');