Emailing user works but not admin? - php

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);
}
*/

Related

Sending an email with pdf attachment in php

I m new to php. I need to send the email with the pdf attachment.I m able to send an email with the attachment. But unable to open the pdf. I get the some error like this
Store information to the database(done),
Sends the staff an email with the new customer information (done), and
Send the customer a "thank you message" email with a pdf file attachment (not working).
I mean, the customer does receive an email, but when he/she opens the pdf file, I get the following error message:
"Acrobat could not oen 'file_name' because it is either not a supported file type or because the file has been damaged(for example, it was sent as an email attachment and wasn't correctly decoded)..."
If someone could help me resolve this problem, that would be great. Thanks!
Here is my code:
$to = 'form#kronova.in, ' . $Email;
$subject = 'ABC :: Admission Form Details';
$repEmail = 'form#kronova.in';
$fileName = 'ABC-Admission.pdf';
$fileatt = $pdf->Output($fileName, 'E');
$attachment = chunk_split($fileatt);
$eol = PHP_EOL;
$separator = md5(time());
$headers = 'From: Principal abc <'.$repEmail.'>'.$eol;
$headers .= 'MIME-Version: 1.0' .$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$message = "--".$separator.$eol;
$message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$message .= "Thanks for filling online application form. Your online admission registration number is E0000". mysql_insert_id() . "." .$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol;
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment".$eol.$eol;
$message .= $attachment.$eol;
$message .= "--".$separator."--";
if (mail($to, $subject, $message, $headers)){
echo "Email sent";
}
else {
echo "Email failed";
}

Send email with the pdf attachment in php

I m new to php. I need to send the email with the pdf attachment.I m able to send an email with the attachment. But unable to open the pdf. I get the some error like this
"Acrobat could not oen 'file_name' because it is either not a supported file type or because the file has been damaged(for example, it was sent as an email attachment and wasn't correctly decoded)..."
If someone could help me resolve this problem, that would be great. Thanks!
Here is my code:
$to = 'form#kronova.in, ' . $Email;
$subject = 'ABC :: Admission Form Details';
$repEmail = 'form#kronova.in';
$fileName = 'ABC-Admission.pdf';
$fileatt = $pdf->Output($fileName, 'E');
$attachment = chunk_split($fileatt);
$eol = PHP_EOL;
$separator = md5(time());
$headers = 'From: Principal abc <'.$repEmail.'>'.$eol;
$headers .= 'MIME-Version: 1.0' .$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$message = "--".$separator.$eol;
$message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$message .= "Thanks for filling online application form. Your online admission registration number is E0000". mysql_insert_id() . "." .$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol;
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment".$eol.$eol;
$message .= $attachment.$eol;
$message .= "--".$separator."--";
if (mail($to, $subject, $message, $headers)){
echo "Email sent";
}
else {
echo "Email failed";
}
Try this:
$attachment = chunk_split(base64_encode($fileatt));
Instead of
$attachment = chunk_split($fileatt);

blank email generated from php form

I have the below code which is executed when someone hits a send button on a form.
My issue is I am receiving an email with the correct subject line and from/to email address. However if the email contains an attachment the body then is blank. However if their is no email attachment the email body gets populated with the text the user would of typed in.
I should note I understand that the code is not as safe as in its current state does not limit inappropriate files like exe's from being uploaded. However I am not wanting or needing a lesson in how to make the code safe. I just need help with how/why the text body does not get populated when an attachment is uploaded?
<?php
//send email
if(isset($_POST['sendemail']))
{
$txtSid = md5(uniqid(time()));
$emailsubject = $_POST['emailsubject'];
$emailpriority = $_POST['emailpriority'];
$emailto = $_POST['emailto'];
$emailcc = $_POST['cc'];
$emailbcc = $_POST['bcc'];
$txtFilesName = $_FILES["attachment"]["name"];
$emailbody = $_POST['wysiwg_editor'];
//if their is an attachment add notice in body of email
if($txtFilesName !='')
{
$emailbody = "$emailbody <br /><br />File name $txtFilesName attached to this email.";
}
$headers = "From: $my_fname $my_lname <$my_emailaddr>\r\n";
$headers .= "Reply-To: $my_fname $my_lname <$my_emailaddr>\r\n";
$headers .= "CC: $emailcc\r\n";
$headers .= "BCC: $emailbcc\r\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$txtSid."\"\n\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= $emailbody."\n\n";
if($emailpriority == 'high priority')
{
$headers .= "X-Priority: 1 (Highest)\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "Importance: High\n";
}
$headers .= "--".$txtSid."\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "Content-Transfer-Encoding: 7bit\n\n";
/***********Email Attachment************/
if($_FILES["attachment"]["name"] != "")
{
$txtFilesName = $_FILES["attachment"]["name"];
$txtContent = chunk_split(base64_encode(file_get_contents($_FILES["attachment"]["tmp_name"])));
$headers .= "--".$txtSid."\n";
$headers .= "Content-Type: application/octet-stream; name=\"".$txtFilesName."\"\n";
$headers .= "Content-Transfer-Encoding: base64\n";
$headers .= "Content-Disposition: attachment; filename=\"".$txtFilesName."\"\n\n";
$headers .= $txtContent."\n\n";
}
mail("$emailto","$emailsubject","$emailbody","$headers");
echo "<div class='alert alert-success'>Email Sent!</div>";
}
OK I re-copy and pasted the code I am using and it has fixed my issue. It seems that I needed a $headers .= $emailbody."\n\n"; line of code inside the if email attachment section.
Under the if statement that detects if there is an attachment uploaded, you are not combining the previous emailbody and the notice together. Instead, use this code:
$emailbody = $emailbody."<br /><br />File name ".$txtFilesName." attached to this email.";

PHP: I cant send an email to multiple recipients

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

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