I am fetching mail messages in php via imap.
Its working well to retrieve the email header and body however frequently some of the messages seem to be coming through twice, the only difference being that in that duplicate the email BODY is empty - all that exists are the email headers.
I have added logging and it seems that the EMPTY message normally comes through first and then on subsequent fetches the full message is retrieved.
Can anyone explain this to me? Is there any reason why retrieving messages from imap will first return empty body emails in SOME but not all cases?
This is my code that is fetching the emails.
$messages = imap_sort($imap, SORTARRIVAL, $sortorder,0,$searchval);
foreach ($messages as $message) {
$header = imap_header($imap, $message);
//Get user from reply-to header info
$useremail = "{$header->reply_to[0]->mailbox}#{$header->reply_to[0]->host}";
$userfullname = $header->reply_to[0]->personal;
//get subject from header
$subject = $header->subject;
$fh = imap_fetchheader($imap, $message);
$fb = imap_body($imap, $message);
$fullbody = $fh.$fb;
// GET TEXT BODY
$bodyTxt = get_part($imap, $message, "TEXT/PLAIN");
//GET HTML BODY
$bodyHtml = get_part($imap, $message, "TEXT/HTML");
In many cases the first time the message is retrieved it will have an empty body (ie $fullbody will be empty). Often this comes in batches for eg 10 messages will be logged as having empty body.
Subsequently the same batch is then retrieved but this time with a body - and I have no explanation for it.
Can anyone offer any ideas?
Thanks
Related
I am trying to send a message with one list with PHP, here is my code:
$packs = $db->QueryFetchArrayAll("SELECT * FROM configmoreg");
foreach($packs as $pack) {
echo '<a>'.$pack['setting_id'].'</a>
<a>'.$pack['config_name'].'</a>
<a>'.$pack['config_value'].'</a>
<br>';
}
mail('example#something.com',"My List",$msg);
How do I make it send only one message with list on it?
For example:
id 00000001
My_name game_over_new
NR_Job 11
type_secure MD5
5 etc...
Build up a string in the for-loop, and send that. Consider:
$msg = '';
foreach($packs as $pack)
$msg .= "<a>${pack['some_key']}</a>";
$subject = "My List";
mail('null#example.com', $subject, $msg);
Meanwhile, I gather you're still learning PHP, so hold on to this next piece of advice until you're ready: don't use PHP's builtin mail() function, use PHPMailer.
Try adding all the variables to an array; then send the array containing the variables.
As you don't know how to use arrays I suggest reading:
https://www.w3schools.com/php/php_arrays.asp
However to get what you want, you'll want something along these lines:
$infoToSend = array($pack['setting_id'], $pack['config_name'], $pack['config_value']);
mail('example#something.com',"My List", $infoToSend);
I am trying to use swiftmailer in my project so that I can send html newsletter to multiple users. I have searched thoroughly but all i got never worked for me. I want to paste more than one recipient in the form input field seperated by comma and send the html email to them.
I set the recipients to a variable($recipients_emails) and pass it to setTo() method in the sending code, same with the html_email.
Questions are:
Q1 How do i send to more than one recipient from the recipient input field.
I tried this:
if (isset($_POST['recipients_emails'])) {
$recipients_emails = array($_POST['recipients_emails'] );
$recipients_emails= implode(',',$recipients_emails);
}
Q2 How do I make the Html within heredoc tag. when i tried concatenating like this ->setBody('<<<EOT'.$html_email.'EOT;', 'text/html'); , my message would appears with the heredoc tag.
if (isset($_POST['html_email'])) {
$html_email = $_POST['html_email'];
}
How do I have input from $_POST['html_email']; to be within EOT tag;
this in part of swiftmailer sending script ;
$message = Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from)
->setTo($recipients_emails)
->setBody($html_email, 'text/html');
Nota bene : Am still learning these things.
According to this document
// Using setTo() to set all recipients in one go
$message->setTo([
'person1#example.org',
'person2#otherdomain.org' => 'Person 2 Name',
'person3#example.org',
'person4#example.org',
'person5#example.org' => 'Person 5 Name'
]);
You can input array directly into setTo, setCc or setBcc function, do not need to convert it into string
You should validate the input-data by first exploding them into single E-Mail-Adresses and push the valid Data into an Array. After this you can suppy the generated Array to setTo().
<input type="text" name="recipients" value="email1#host.com;email2#host.com;...">
On Submit
$recipients = array();
$emails = preg_split('/[;,]/', $_POST['recipients']);
foreach($emails as $email){
//check and trim the Data
if($valid){
$recipients[] = trim($email);
// do something else if valid
}else{
// Error-Handling goes here
}
}
Ok so first of all you need to create a variable that form your heredoc contents. The end tag of heredoc has to have no whitespace before it and then use that variable. (to output variables inside heredoc you need to wrap them with curly braces) see example..
$body = <<<EOT
Here is the email {$html_email}
EOT;
$message = Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from)
->setTo($recipients_emails)
->setBody($body, 'text/html');
I'm using the php mail function to send email and it's working fine.
The email body is dynamically generated and the problem is that the email recipient can only receive emails that are 160 characters or less. If the body of the email is longer than 160 characters, then I want to split the body into separate blocks, each less than 160 characters.
I'm using CRON Jobs and curl to automatically send the email.
How can I send each separate email to the same recipient when more than one body is generated? Below $bodyAll represents that there is only one email to be sent because the dynamically generated content fits within 160 characters. If the body content is more than 160, then $bodyAll would not be sent and $bodyFirstPart would be sent to the recipient, then $bodySecondPart, etc., until all the separate body texts are sent.
$body = $bodyAll;
$body = $bodyFirstPart;
$body = $bodySecondPart;
$body = $bodyThirdPart;
$mail->addAddress("recepient1#example.com");
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body</i>";
if(!$mail->send())
you can use strlen to check the length inside a while loop trimming it down with substr, and send each chunk every loop iteration:
<?php
$bodyAll = "
some really long text that exceeds the 160 character maximum for emails,
I mean it really just tends to drag on forever and ever and ever and ever and
ever and ever and ever and ever and ever and ever and ever......
";
$mail->addAddress("recepient1#example.com");
$mail->Subject = "Subject Text";
while( !empty($bodyAll) ){
// check if body too long
if (strlen($bodyAll) > 160){
// get the first chunk of 160 chars
$body = substr($bodyAll,0,160);
// trim off those from the rest
$bodyAll = substr($bodyAll,160);
} else {
// loop terminating condition
$body = $bodyAll;
$bodyAll = "";
}
// send each chunk
$mail->Body = "$body";
if(!$mail->send())
// catch send error
}
I have an array of emails and want to send an email for each entry in the array but currently it only sends it to the first address. I have looked around and can't see what I am doing wrong.
//This is the array
$emails = array($teamleademail, $coleademail, $sponsoremail, $facilitatoremail, $cisupportemail);
//Now the foreach statment
foreach ($emails as $value) {
//pulls in email sending code
require_once ("Mail.php");
//variables required to send the email
$from = "Scope Sheet Process Database (no reply)<scopesheetprocess#goodrich.com>";
//Make the $to variable that of the email in the array
$to = "$value";
$subject = "Scope Sheet $scopesheetid Closed";
$body = "Scope Sheet $scopesheetid has been closed and can no longer be edited.";
//send email code
require_once ("sendemail.php");
}
I am using pear php to send the email because of IT issues but that shouldn't matter since it should be running the scripts each time and sending separate emails.
The problem is this line:
require_once ("sendemail.php");
...should just be
require ("sendemail.php");
As it is, it will only be included on the first iteration of the loop, hence only the first email is sent.
This on its own may not fix your problem - if it doesn't we'll need to see the code in sendemail.php.
okay, here is the code
while (!feof($text)) {
$name = fgets($text);
$number = fgets($text);
$carrier = fgets($text);
$date = fgets($text);
$line = fgets($text);
$content = $_POST['message'];
$message .= $content;
$message .= "\n";
$number = $number;
mail("$number#vtext.com", "Event Alert", $message, "SGA");
Header("Location: mailconf.php");
}
I am trying to get a message sent to a phone, I have 'message' coming in from a text area, and then I assign it to "$content" then I put "$content" into "$message" and then as you can see, in my mail line, I want the address to be the variable "number"#vtext.com I have the while loop because there are many people in these files I wish to send a message to...
When I change the "$number#vtext.com" to my email address, it sends everything fine, I just cannot seem to get it to send to a phone number, please help!
thanks!
The problem is that fgets includes the newline character as part of the return. So you are effectively sending an email to:
1234567890
#vtext.com
Which of course is invalid. Change your line that reads $number = $number to:
$number = trim($number);
And the rest of your code should function as expected, with the email being received on your phone.
Part of my original answer
I would highly recommend using a (probably paid) service to send SMS messages to phones if you need anything remotely reliable. A quick search yielded a few results:
PHP/SMS Tutorial
A list of 5 ways to Text for free <-- This link is dated, but might still be helpful
You need to append the number variable and the "#vtext.com" literal string together:
mail($number . "#vtext.com", "Event Alert", $message, "SGA");