I have a service that sends out text to an email entered after x number of days. I want to use cron, but I know that because my PHP script uses variables, this would not work. How can I change my PHP or do anything that would allow me to use cron (or even something else)? I just need something where it will store the emails then send them. I'm really new to PHP, so keep it simple please.
Here’s my code:
<?php
if(isset($_POST['email']))
{
$headers = "From: Memory Jet <your_company#example.com>\r\n";
$to_visitor = $_POST["email"];
$common_data = $_POST["message"];
mail($to_visitor, "Your Memory", $common_data, $headers);
}
?>
use argv array to read CLI params - http://php.net/manual/en/features.commandline.usage.php
if your script is named /bin/script.php then if invoked as /bin/script.php xyz the following:
$email = $argv[1];
will assign 'xyz' to $email.
just read doc I've provided - there's all you need
if you're wanting to store the data to transmit later, either using a database to store the info or writing it to a file would allow you to retrieve it.
If you need to potentially edit and manipulate the data later on, I'd recommend using a database.
I'd also recommend looking at http://www.tizag.com/phpT/ for some good, simple tutorials for PHP that really helped me when I first got going.
Related
I have a php file that I use to send newsletters. Recently I moved to a new server and they use PEAR Mail instead of the default PHP mail to send mails and I had to update my script to function. But it's still not working. I get the TXT version not the HTML version.
If I manually enter the html codes inside the setHTMLBody() it works but when I replace it with my ob_start $output_string variable it doesn't work.
Here is my script;
ob_start();
include "URL/To/File.php";
$output_string = ob_get_contents();
ob_end_clean();
$headers['From'] = 'from#email.com';
$headers['Subject'] = 'Newsletter Subject';
require_once('Mail.php');
require_once('Mail/mime.php');
$message = new Mail_mime();
$message->setTXTBody("Your client doesn't support HTML.");
$message->setHTMLBody(''.$output_string.'');
$mail =& Mail::factory('mail');
$result = $mail->send('myemailaddress#gmail.com', $message->headers($headers), $message->get());
if (PEAR::isError($result)) {
echo("<span>" . $result->getMessage() . "</span>");
} else {
echo("<span style='color: #f7941c; font-weight: bold'>Congratulations!
Your mail has been sent successfully</span>");
}
how do I correctly input the line below correctly? It's not working as is right now.
$message->setHTMLBody(''.$output_string.'');
So I'm cold on this subject right now (working on mobile) though let's see if I can help you out. So I looked up the setHTMLBody function. It's a little fuzzy on the type that the expected parameters should be. In PHP you can get the type using gettype($example) (like console.log(typeof example); in JavaScript though PHP is generally more forgiving about types (calculating a number that has a string type will work in PHP, not JavaScript)).
The name of the function implies that it should make this part of the email HTML. Now of all the modules I've built on my web platform email has been the most challenging not because it's inherently complex though because it's very subjective. In example some servers might expect you to serve an <html> element, others a <body> element and others won't care if you omit it (and I'm not sure what if any specifications declare what is "proper" here). I've not intentionally worked with compressing data in emails (just output in web mail though it's technical context is lost at that point). Long story straight here: the client's user agent (browser, email application, etc) should be handling the compression, not you.
PHP ob stuff is a bit convoluted. I dislike the same function/method being used for both compression and being able to capture and do find/replace with the output before sending it to a client. I think you're using it for compression though you could also be using it to replace bits of code for whatever reason. In this case your best bet for troubleshooting (presuming that your ob should work, most likely for replacing bits of code) is to use the string and test it outside of this environment. When I test cron jobs I always test them in normal environments first (though keep in mind cron jobs run in a much more limited environment so for debugging there I just have print_r($_SERVER) send me information via email).
So I think your ob code is messing up the parser setHTMLBody() function. Break your code down until you have working bits and then add your necessary and increasingly complex bits to it until you hit a problem and then because you know exactly what you just added you'll be able to single out the issue much easier.
I'd need further clarification though I can edit this answer later. Let me know where you're at, I always check notifications even if it takes a day.
I have a few dozen tools that I use when I develop. I'm not sure if this tool will validate though it may help you somehow since you are working on email. https://www.mail-tester.com/ helped me address some issues related to email (it's not related to this issue).
I'm using PHP's IMAP Functions extension to check e-mail from a POP3 account.
When it comes to actually fetching the message, I have the following code:
// Make new raw email message
// With PHP Imap, we need to fetch headers and body separately!
$body = imap_fetchheader($mailbox, $msgno);
$body .= imap_qprint(imap_body($mailbox, $msgno));
All the examples I am able to find mentions that one should use imap_qprint() here, but I've noticed that when I do all the GET parameters of a URI get mangled.
For example,
http://localhost/pronk.php?id=6248&key=c7eb7c5173e1525a47c63abc39d938e1
becomes:
http://localhost/pronk.php?idb48&keyÇeb7c5173e1525a47c63abc39d938e1
If I don't use imap_qprint() everything seems to work just fine. (I'm using imap_body instead of imap_fetch_body because I want the entire e-mail - headers, parts, and everything) But since all examples I can find say to use qprint, I wanted to know why my code seems to need it omitted?
Not sure if you're still curious about this, but if you do want to use imap_qprint(), you need to first check what the encoding of the message is.
You can do this using imap_fetchstructure(). http://php.net/manual/en/function.imap-fetchstructure.php.
Only if the encoding is set to 'QUOTED-PRINTABLE' would you want to use imap_qprint().
I'm working on a project that allows users to edit PHP code in PHP file.
I have a PHP page that uses the mail function to send emails. I want to allow the user to change the message sent out (in the mail function).
I know how to re-write over a file but how to edit it, I'm not sure...
So here's the mail function:
$name=$_POST['userName'];
$emaily=$_POST['userEmail'];
$email = "$sponsor_email";
$subject = "the subject";
$message = "This is the message I want the user to be able to edit.";
mail($email, $subject, $message, "From: $user-email");
Now I've got the $message var printed out on in the html form (so the users can see what will be changed but i don't know how I'd go about actually writing the code to change it on from submit.
Any ideas?
Thanks!
Sorry guys....
I didnt explain the situation properly.
The php file with the mail function will not be used to send an email from that form. All i want the form to do is edit the php file basically. I don't want to send the email with this form, this is for users to edit the mail function. Hope thats more clear...
On your editing page, just do something like below:
$message = file_get_contents("path to message file");
echo "<textarea id=\"message\">".$message."</textarea>";
Then you can edit whatever the contents of the file were.
Might be better though if you used a database, and sanitized the user input - rather than saving everything to a file.
Put the textarea in the form named "message", then do $message = $_POST['message'];
create a PHP that read what is in the PHP file that contains code to be edited then print it out in a HTML textarea then make another file that will write that modified code in the PHP file that has the code(to be modified). You can use google find how to read and write in a file.
I'm trying to figure out a basic web email form. I got the code from somewhere and am trying to make the checkboxes options.
The email gets sent but just has Array next to the option.
What am I doing wrong?
Some sample code will help diagnose the problem.
At a guess I'd say you've got something like
$message = "contact from message\r\n";
$message .= $_POST['check'];
since $_POST['check'] is an array, you need to use it like:
$message .= implode(', ', $_POST['check']);
This will convert the array of selected services options into a comma delimited string.
If you are a PHP beginner, you can use PHP Forms builder that allows to create professionally looking email forms within just a few minutes. You will only need to make a PHP form by means of an intuitive interface and generate a code that can be easily copied and pasted to any web page.
How can i send mails through the php script??
I am trying to do somthing like this:
for($k=0;$k<=$x->length-1;$k++)
{
for($l=0;$l<=$j-1;$l++)
{
if($y->item($k)->nodeValue==$JobNoArr[$l] && $AcceptanceDateArr[$l]=='0000-00-00')
{
//echo $v->item($k)->nodeValue ;
$email = $v->item($k)->nodeValue . ",";
$to = $email;
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: someother.valid#email.adr";
mail($to,$subject,$txt,$headers);
}
}
}
Please help me in this issue.
Best
Zeeshan
I strongly advise against sending mail using PHP's mail() function. Composing valid emails and delivering them successfully is trickier than it seems at first glance. There is encoding, putting parts together, validation & sanitation, error reporting (more than bool mail(...)), support for authentication and on and on ... The mail() function does not offer any of these things.
Try SwiftMailer or similar. You can configure it to use PHP's mail() function and so much more. I highly recommend it.
You're trying too much all at once. Try going one step at a time. First send a simple email, with hard-coded parameters to get that working, then troubleshoot it within the context of your nested loops.
the problem is mail function is very unreliable, especially when sending large amount of emails.
i would recommend looking into PHPmailer library (uses direct SMTP connection):
http://phpmailer.codeworxtech.com/
The code (the inner most block) looks correct. Make sure your environment is setup correctly.
http://ca3.php.net/manual/en/mail.setup.php
first do
echo $result = mail($to,$subject,$txt,$headers);
and see what u get , error ?
I recommend to use a class such phpMailer
why you have comma in the end of ths line ?
$email = $v->item($k)->nodeValue . ",";
you send to one mail every time.
php-s mail function uses sendmail as a MTA, so if some mails go through and some not, I would look at sendmail's log for errors.
If you are on a shared web host, or your home computer, the main domain for the server will be something like
server.your-isp-or-host.com
The spam filter will then see the email claiming to be from
yourdomain.com
when it really came from the first address, and would then delete it.
This would explain the hit-and-miss nature of your error.
If you are on a dedicated server, or a static IP pointing to your home computer with properly set up DNS, the above does not apply.
if it works sending emails to gmail then it should work fine sending emails to yahoo too
you may find that the issue is not the sending of the emails, but maybe yahoo is marking them as spam or blocking them at the gateway
i notice you are appending a comma to the end of the email address, what is the point of that ?
there may be other problems, are your loops correct, are they covering all the cases you expected.
are you sending thousands of emails ? can your mta handle the rate at which you are putting emails into the queue
is your script hitting max_execution time and stopping ?