I have a emails.txt file with name and email Example: John, john#gmail.com
I can try to export with a different way the emails.txt (for example name: john email: john#gmail.com)
but the important part is
I want to send emails for all recipients that exist in emails.txt using his names to edit the message
$file = fopen("emails.txt", "r") or die("Unable to open file!");
//while(!feof($file)){
$line = fgets($file);
$to = $line;
$subject = "This is subject";
$message = 'Hello Mr %NAME%!';
$header = "From:TESTE \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
fclose($file);
Using explode() to break up the parts of email.txt will give you what you need:
list($name, $to) = explode(",", $line);
$message = sprintf('Hello Mr %s!', $name);
This is based on every row within emails.txt being formatted correctly and clearly such as John Smith, john.smith#example.com
You can also change the code entirely to use str_getcsv() which I will let you look into.
Related
I am trying to email piping with cpanel and PHP script. Its working fine. I am looking for get From Address, Subject and Message from received email. My script is like below
#!/usr/bin/php -q
<?php
/*$fd = fopen( "php://stdin", "r" );
$message = "";
while ( !feof( $fd ) )
{
$message .= fread( $fd, 1024 );
}
fclose( $fd );*/
$fd = fopen("php://stdin", "r");
$message = "";
while (!feof($fd)) {
$message .= fread($fd, 1024);
}
fclose($fd);
//split the string into array of strings, each of the string represents a single line, received
$lines = explode("\n", $message);
// initialize variable which will assigned later on
$from = "";
$subject = "";
$headers = "";
$message = "";
$is_header= true;
//loop through each line
for ($i=0; $i < count($lines); $i++) {
if ($is_header) {
// hear information. instead of main message body, all other information are here.
$headers .= $lines[$i]."\n";
// Split out the subject portion
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
//Split out the sender information portion
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
$from = $matches[1];
}
} else {
// content/main message body information
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="") {
// empty line, header section has ended
$is_header = false;
}
}
$to = 'myemail#gmail.com';
$subject = 'the subject';
$message1 = "You have new message from :".$from. "and message is" .$message;
$headers = 'From: webmaster#mydomain.com' . "\r\n" .
'Reply-To: webmaster#mydomain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message1, $headers);
?>
I am getting result in my email like below
You have new message from :FirstName LastName <senderemail#gmail.com>and message is--00000000000045a1e5059ef97af8
Content-Type: text/plain; charset="UTF-8"
This is Body text Message
--00000000000045a1e5059ef97af8
Content-Type: text/html; charset="UTF-8"
<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif;font-size:large">This is text<br></div></div>
--00000000000045a1e5059ef97af8--
However its contain unnecessary words in it. I want plain output like below
You have new message from :senderemail#gmail.com and message is This is Body text Message
Sorry I am new in PHP and does not able to solve the issue. Let me know if anyone can help me for extract and get output like this.
Thanks!
i am trying to send emails via Bcc and i was expecting that they were hidden but it seems not. Maybe my code is not correct?
// grab all emails from txt file
$myfile = fopen("database-email.txt", "r") or die("Unable to open file!");
$allEmails = fread($myfile,filesize("database-email.txt"));
fclose($myfile);
$afzender = "noreply#wisselslag.nl";
$to = 'pj.maessen41#live.nl';
$subject = 'Nieuwsbrief de Wisselslag';
$headers = "From: " . $afzender . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Bcc: " . $allEmails . "\r\n";
if (mail($to, $subject, $message, $headers)) {
echo 'Bericht is verstuurd!';
} else {
echo 'There was a problem sending the email.';
}
So i have a database-email.txt file in which all the emails are stored, comma seperated from each other like this:
joey.tims#gmail.com,
j.maessen#online.nl,
john.doe#live.nl,
diana.johnson#hotmail.com,
When sending to my gmail account, i can see this:
How is this possible that i can see to where the email also is sent to?
The email list should not have new line character.
Make it one line:
$allEmails = str_replace(array("\n","\r"), '', $allEmails);
// joey.tims#gmail.com, j.maessen#online.nl, john.doe#live.nl, diana.johnson#hotmail.com
As mentioned in my comment, any list of recipients should not contain newline characters.
I'd change the format of your file to a single line
joey.tims#gmail.com,j.maessen#online.nl,john.doe#live.nl,diana.johnson#hotmail.com
I'd also use file_get_contents() instead of fopen / fread.
Alternately, store your email addresses on each line, without commas, eg
joey.tims#gmail.com
j.maessen#online.nl
john.doe#live.nl
diana.johnson#hotmail.com
and use file() and implode()
$allEmails = implode(',' file(__DIR__ . '/database-email.txt',
FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
I'm trying to send email using PHP
I have emails.txt with a list of emails and names split by ';'
and I also have content.txt with the email message, for example, "Hello Mr. $name"
I want to send an email, for all users, using content.txt file and alter the $name using the emails.txt to replace the string $name
I built some parts, but I'm stuck
<?php
$file = fopen("emails.txt", "r");
$filecontent = fopen("content.txt", "r");
while(!feof($file)){
$line = fgets($file);
$to = $line;
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:abc#somedomain.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
}
fclose($file);
?>
another part
<?php
$path_to_file = 'content.txt';
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace("$name","$correctname",$file_contents);
file_put_contents($path_to_file,$file_contents);
?>
I am trying to write a script which will be used to get emails piped to it and then forwarded to another email address with a different "From" field
#!/usr/local/bin/php56 -q
<?php
$fd = fopen("php://stdin", "r");
$message = "";
while (!feof($fd)) {
$message .= fread($fd, 1024);
}
fclose($fd);
//split the string into array of strings, each of the string represents a single line, received
$lines = explode("\n", $message);
// initialize variable which will assigned later on
$from = emailFrom#example.com;
$subject = "";
$headers = "";
$message = "";
$is_header= true;
//loop through each line
for ($i=0; $i < count($lines); $i++) {
if ($is_header) {
// hear information. instead of main message body, all other information are here.
$headers .= $lines[$i]."\n";
// Split out the subject portion
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
//Split out the sender information portion
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
$from = $matches[1];
}
} else {
// content/main message body information
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="") {
// empty line, header section has ended
$is_header = false;
}
}
mail( "emailTo#example.com", $subject,$message, $from );
?>
this script is getting us bounced emails with delivery failure message "local delivery failed".
What am I doing wrong?
You need to verify that it's parsing the incoming messages properly. Add this line following your mail() command to verify the script's output:
echo "Subject:".$subject."\nFrom:".$from."\nMessage:".$message;
Then pass the script a test email from the command line and watch the screen. Something probably isn't getting parsed correctly.
When trying to attached the pdf file, it cannot be open in PDF and says its corrupted. I tried several solution found in the web but it didn't work. The file is being attached and sent but when you open the PDF its corrupted although it is being downloaded but with invalid size(1kb) wherein the original size is around 0.40mb
Any help would highly be appreciated.
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message)
{
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = "\n";
//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: ".$from_name.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
if (mail($mailto, $subject, $body, $headers)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR";
}
}
/*
// Only accept POSTs from authenticated source
if ($_POST['HandshakeKey'] != 'outsource-phil') {
echo "<h1>You are not who you say you are, mister man.</h1>";
die();
}
*/
// EDIT FROM HERE DOWN TO
// CUSTOMIZE EMAIL
//*
// File to attach
$filename = "outsource.pdf";
$path = $_SERVER['DOCUMENT_ROOT']. "/wp-content/uploads/2014/02/";
// Who email is FROM
$from_name = "COmpany Name";
$from_mail = "company#domain.com";
$replyto = "company#domain.com";
// Whe email is going TO
$mailto = $_POST['Field3'];
// Subject line of email
$subject = "Your file has arrived!";
// Content of email message (Text only)
$requester = $_POST['Field12']; // Comes from Wufoo WebHook
$message = "Hey $requester,
Your custom email message
goes here";
// Call function to send email
mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message);
Edit #1: After doing some research myself, I found that it's much easier to use an external library that sets up the e-mail with the attachment for you. I've done this using PHPMailer, which can be downloaded from https://github.com/Synchro/PHPMailer.
The following code should do what you're looking for. I highly recommend using a library such as PHPMailer.
// File to attach
$filename = "outsource.pdf";
$path = $_SERVER['DOCUMENT_ROOT']. "/wp-content/uploads/2014/02/";
// Who email is FROM
$from_name = "COmpany Name";
$from_mail = "company#domain.com";
$replyto = "company#domain.com";
// Whe email is going TO
$mailto = $_POST['Field3'];
// Subject line of email
$subject = "Your file has arrived!";
// Content of email message (Text only)
$requester = $_POST['Field12']; // Comes from Wufoo WebHook
$message = "Hey $requester,
Your custom email message
goes here";
/*
* The following code requires PHPMailer
*/
require_once('PHPMailer-master/class.phpmailer.php');
$mail = new PHPMailer();
$mail->AddReplyTo($replyto, $from_name);
$mail->SetFrom($from_mail, $from_name);
$mail->AddAddress($mailto, $requester);
$mail->Subject = $subject;
$mail->MsgHTML($message);
$mail->AddAttachment($path . $filename);
if (!$mail->Send()) {
echo "Mail error!";
} else {
echo "Mail success!";
}
You seem to overwrite your PDF file by using the following code:
$str = "Business Plan: \nSome more text";
$fp = fopen("outsource.pdf", 'w+');
fwrite($fp, $str);
fclose($fp);
The w+ option in fopen() will create a new, blank file. You appear to want to send the original file, in which case you wouldn't want these lines of code. What are you trying to accomplish here?