PHP: I cant send an email to multiple recipients - php

I've checked php manual, but still can't get my script to work
If I delete the BCC line, the mail goes to $to1 but with BCC line, it goes nowhere
here is the code:
// email stuff
$to1 = "address1#mail.com";
$to2 = "address2#mail.com";
$from = "noreply#mail.com";
$subject = "Subject";
$message = "message";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// main header
$headers = "To: ".$to1.$eol;
$headers .= "From: ".$from.$eol;
$headers .= "Bcc: ".$to2.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol;
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$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;
$body .= $message.$eol;
// send message
mail($to1, $subject, $body, $headers);
If anyone has an idea, thanks a lot

Related

After sending email a security warning comes in email body

I am using php mail function with PDF attachment. Here everything is working fine but in the email body this following warning comes
"This email has an attachment that allows unverified scripts to run on your computer when opened. Be careful"
can you suggest ? what is the problem
Are you using the correct headers for sending attachments?
You can try using this pattern (tested and working):
$subject = 'SUBJECT';
$message .= 'Hello world'."\n";
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
$separator = md5(time());
$eol = "\r\n";
$headers = "From: ME <me#mymail.org>".$eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol;
$body = "--".$separator.$eol;
$body .= "Content-Type: text/plain; charset=\"utf-8\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol;
$body .= $message.$eol;
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol;
$body .= $content.$eol;
$body .= "--".$separator."--";
$filename is your attachment, $file is the full path (including $filename).

Sending attachment in email function but not able to get it

I am using this function for attaching pdf in my mail. It's working fine but when the user tries to open the pdf file he gets a message telling him that file can't be opened, because of a problem with file formate.
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = "\r\n";
// attachment name
$filename = $invoice.'.pdf';
// encode data (puts attachment in proper format)
$attachment = chunk_split(base64_encode($filename));
//$attachment = $filename;
// main header
$from = "test#productionserver.in";
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol;
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$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."--";
mail($to,$subject,$body,$headers);
Does anyone have an idea what I made wrong ?
$attachment = chunk_split(base64_encode($filename));
You are encoding the filename as your attachment. You need to encode the actual file and not just its name.
$attachment = chunk_split(base64_encode(file_get_contents($filename)));

PHP mail attachment bad headers?

I'm building a service to send pdf attachments to some email destinations. Recently I have found that some people can't receive the emails properly:
- headers go into email body
- attachments get corrupted
By having a bunch of tests, I'm having a clue that the problem might be the PHP_EOL because when I change it (from \n to \r\n or vice-versa) some people receive it well and others don't.
Is it a coding problem or a server problem?
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "Convite.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $content;
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: Myserver<".$from_mail.">\nBCC: suporte#myserver.com".$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed;\tboundary=\"".$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=\"utf-8\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/pdf; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

Emailing user works but not admin?

I have an order form that is processed and the customer receives the email, however, adding in the admin bit doesn't email the admin?
The following code is the bit that sends to the customer (which works fine) and also the code that should also email the admin but doesn't :(
// email stuff (change data below)
$to = $payment_data['sold_to_email'];
$from = $order_options['admin_email'];
$subject = "Your Order";
//include the message
$message = $order_options['email_message'];
//Build the Order out
$message .= '<br /><br />Thank you for purchasing from us';
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// main header (multipart mandatory)
$headers = "From: ". $company . " <" . $from . ">". $eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
//$headers .= $message.$eol.$eol;
// send message
$sendit = wp_mail($to, $subject, $message, $headers);
$admin_message = $payment_data['admin_field'];
wp_mail($admin_email, 'New Order', $admin_message);
//Code to troubleshoot email problems.
/* if(!$sendit) {
$tempOptions = get_option('order_options');
$tempOptions['admin_email'] = "EMAIL FAILED";
update_option('order_options', $tempOptions);
}
*/

PHP Sendmail sender name is always Apache when header have attachment

We are trying to send an email using sendmail. Everything works fine with normall headers but the moment we add attachment in the header, the sender name comes as Apache. Here is our code snippet
$from_email = "noreply#domain.com";
$separator = md5(time());
$eol = PHP_EOL;
$filename = "attachment.pdf";
$attachment = chunk_split(base64_encode(file_get_contents($filename)));
$text = "Hi!";
// main header (multipart mandatory)
$headers = "From:".$from_email.$eol;
$headers = "Bcc:user#domain.com".$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$message .= "--".$separator.$eol;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= $text.$eol.$eol;
// attachment
$message .= "--".$separator.$eol;
$message .= "Content-Type: application/pdf".$eol;
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol;
$message .= $attachment.$eol;
$message .= "--".$separator."--".$eol;
$b = mail($email, "Your Issue of the STQ",$message, $headers, "-fnoreply#domain.com");
By Adding -fnoreply#domain.com, we are getting like this in email header From: noreply#domain.com (Apache). Not sure where this Apache is coming from?
What could be the problem here.
Thanks
You need a dot on the second line.
$headers = "From:".$from_email.$eol;
$headers .= "Bcc:user#domain.com".$eol;
Make the header like this :
$headers .= 'From: <webmaster#example.com>' . "\r\n";
Also missing the dot on the second line as xyzz pointed

Categories