replying to email address on the from - php

here's the process.php. I want to receive the email from the email address on the form so I could reply to them
<?php
$where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));
// To send HTML mail, the Content-type header must be set
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\n";
$headers .= "From: " . $_POST['field_9'] . "\n";
$headers .= "Reply-To: ". $_POST['field_9'] . "\n";
mail("harshad#madiganpratt.com","Reservaion Enquiry","Form data:
Preferred Arrival Date : " . $_POST['field_1'] . "
Preferred Departure Date: " . $_POST['field_2'] . "
Room Category: " . $_POST['field_3'] . "
Adults: " . $_POST['field_4'] . "
Children: " . $_POST['field_5'] . "
Please contact me by: " . $_POST['field_6'] . "
First Name: " . $_POST['field_7'] . "
Last Name: " . $_POST['field_8'] . "
Email: " . $_POST['field_9'] . "
Phone: " . $_POST['field_10'] . "
");
include("confirm.html");
?>
......

You'll need to set the headers:
// To send HTML mail, the Content-type header must be set
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\n";
$headers .= "From: " . $from . "\n";
$headers .= "Reply-To: ". $from . "\n";
$headers .= "CC: email#eami.com\n";
// Mail it
mail($to, $subject, $message, $headers);

Related

PHP only sending one mail() in a page

All of sudden, my PHP code that sends an email after someone registers on a form stopped working. I'm cracking my head trying to figure out what is going on...
I already tested a little piece of code emailing myself and it works.
I observed the console window in Chrome while the page executes, but it doesn't show any errors.
The code sends me an email with the form info and sends an email to the user's inbox telling that the registration was successful.
// SENDING EMAIL
//To me
$line_break = "\r\n";
$emailSender = "subscriptions#mydomain.com";
$emailTOme = "me#mydomain.com";
$cco = "support#mydomain.com";
$subject= "my subject";
$messageHTML = '
<p>Nome.......: ' . $nome . '</p>
<p>E-mail.......: '.$email . ' </p>
<p>Profissão.......: '. $profissao . ' </p>
<p>Endereço.....: '. $endereco . ', ' . $endereco_num . ' </p>
<p>Complemento.....: '. $complemento . '</p>
<p>Bairro.......: ' . $bairro . ' </p>
<p>CEP..........: '. $cep . ' </p>
<p>Cidade.......: ' . $cidade . ' </p>
<p>UF...........: ' . $uf . ' </p>
<p>Telefone.....: ' . $telefone . ' </p>
<p>Celular......: ' . $celular . ' </p>
<p>Inscrição p/.: ' . $evento . '</p>
<p>Valor da inscrição: <b>R$' . $valor_inscricao . '</b></p>';
$headers = "MIME-Version: 1.1" . $line_break;
$headers .= "Content-type: text/html; charset=iso-8859-1". $line_break;
$headers .= "From: " . $emailSender . $line_break;
$headers .= "Return-Path: " . $emailSender . $line_break;
$headers .= "Bcc: " . $cco . $line_break;
$headers .= "Reply-to: " . $emailSender . $line_break;
mail($emailTOme , $subject, $messageHTML,$headers, $emailSender);
//email for the user
$messageHTMLuser = '
<p> Lorem </p>
<p> Ipsum </b></p>';
$headers2 = "MIME-Version: 1.1" . $line_break;
$headers2 .= "Content-type: text/html; charset=iso-8859-1". $line_break;
$headers2 .= "From: " . $emailSender . $line_break;
$headers2 .= "Return-Path: " . $emailSender . $line_break;
$headers2 .= "Reply-to: " . $emailSender . $line_break;
mail($email, 'We recieved your registration', $messageHTMLuser, $headers2, $emailSender);
The problem is that now only the second mail() function works.
The $email on the second function comes from the previous form.
Try to comment out the "BCC" line.
As the current php mail don't support this option (anymore) - apparantly as a way to avoid bcc-form-spamers..
In order to use advanced email functions, you would probably be better off using PHPMailer (or a similar lib): https://packagist.org/packages/phpmailer/phpmailer
Add this to the start of your script and it should show you your errors (if any) at the top of the page.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

php mail function headers with attachment [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm not sure why but this has suddenly stopped sending out emails. I have tried to test the email server using mail("email#domain.com","test","test message"); and it sends it fine. I have a feeling something is wrong with my headers?
I have tried to send it with and without attachments but it is not working.
Any help would be greatly appreciated.
Thanks in advance!
<?php
extract($_POST);
$dir = '../uploads/';
$files_temp = scandir($dir, 1);
foreach ($files_temp as $key=>$value) {
if (strpos($value,$file_id) !== false) {
$files[] = $value;
}
}
$message = "";
$message .= "Firm: " . $_POST['firm'] . "\n";
$message .= "Attorney: " . $_POST['attorney'] . "\n";
$message .= "Main Contact: " . $_POST['main_contact'] . "\n";
$message .= "Phone: " . $_POST['phone'] . "\n";
$message .= "Cell: " . $_POST['cell'] . "\n";
$message .= "Fax: " . $_POST['fax'] . "\n";
$message .= "Address: " . $_POST['address'] . "\n";
$message .= "Email: " . $_POST['email'] . "\n";
$message .= "Court County: " . str_replace("_"," ",$_POST['court_county']) . "\n";
$message .= "Court Name: " . $_POST['court_name'] . "\n";
$message .= "Case Type: " . str_replace("_"," ",$_POST['case_type']) . "\n";
$message .= "Appearance Type: " . $_POST['appearance_type'] . "\n";
$message .= "Date: " . $_POST['date'] . "\n";
$message .= "Time: " . $_POST['time'] . "\n";
$message .= "Department: " . $_POST['department'] . "\n";
$message .= "Case Name: " . $_POST['case_name'] . "\n";
$message .= "Case Number: " . $_POST['case_number'] . "\n";
$message .= "Your Client: " . $_POST['your_client'] . "\n";
$message .= "Client Present: " . $client_present_text . "\n";
$message .= "What do you want the attorney to accomplish at this hearing?: " . $_POST['text1'] . "\n";
$message .= "Explain case background: " . $_POST['text2'] . "\n";
$message .= "Signature: " . $_POST['signature'] . "\n";
// email fields: to, from, subject, and so on
$to = "email#domain.com";
$from = "email#domain.com";
$subject ="Appearance form";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($dir.$files[$x],"rb");
$data = fread($file,filesize($dir.$files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
// send
$ok = #mail($to, $subject, $message, $headers);
Looks like my code wasn't the problem. It was an issue with mailing it from info#mydomain.com. Changed $from variable to a gmail email address and it started sending emails again.

php mail attachment - error - how to debug

I search and try different methods to produce a php mail with an attachment, which is saveable with the Client (Outlook). It doesn't work porper and I don't know why?
If the script send the mail with this code:
$result = mail($mail_to, $subject, $headers);
I got an email with an inline base64 coded attachment and if use it in this way:
$result = mail($mail_to, $subject, "", $headers);
or
$result = mail($mail_to, $subject, $text, $headers);
I got the PHP Error: "false" - the mail could not send.
So it isn't and Mailserverproblem, I think and from headersview I think, it should work and I get no mail error on the Linux site.
A part from the mail coming with the attachment inline:
MIME-Version: 1.0
From: user#nowhere.xyz
Content-Type: multipart/mixed; boundary="F34A3E8D822EE5A70EEDE9C3C143243C"
--F34A3E8D822EE5A70EEDE9C3C143243C
Content-Type: multipart/alternative; boundary="A0403290562B2A2F19FB62E48C51BE33"
--A0403290562B2A2F19FB62E48C51BE33
Content-Type: text/plain
Content-Transfer-Encoding: 8bit
Hello Mr. Xyz,
Some stupid text as an example.
--A0403290562B2A2F19FB62E48C51BE33--
--F34A3E8D822EE5A70EEDE9C3C143243C
Content-Type: application/CSV; charset="ISO-8859-1"; name="trouble.csv"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="trouble.csv"
...
YWNodW5nIG5pY2h0IGVyZm9yZGVybGljaCAvIG3DtmdsaWNoIjsiIjsiIjsK
--F34A3E8D822EE5A70EEDE9C3C143243C--
The matching PHP code:
$text = "Hello Mr. Xyz," . $nl . "Some stupid text as an example." . $nl;
$content = chunk_split(base64_encode($file));
$headers = "MIME-Version: 1.0" . $nl;
$headers .= "From: " . $mail_from . $nl;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary1 . "\"" . $nl . $nl;
$headers .= "--" . $boundary1 . $nl;
$headers .= "Content-Type: multipart/alternative; boundary=\"" . $boundary2 . "\"" . $nl . $nl;
$headers .= "--" . $boundary2 . $nl;
$headers .= "Content-Type: text/plain" . $nl;
$headers .= "Content-Transfer-Encoding: 8bit" . $nl . $nl;
$headers .= $text . $nl;
$headers .= "--" . $boundary2 . "--" . $nl . $nl;
$headers .= "--" . $boundary1 . $nl . $nl;
$headers .= "Content-Type: application/excel; charset=\"ISO-8859-1\"; name=\"xyz.csv\"" . $nl;
$headers .= "Content-Transfer-Encoding: base64" . $nl;
$headers .= "Content-Disposition: attachment; filename=\"xyz.csv\"" . $nl . $nl;
$headers .= $content . $nl;
$headers .= "--" . $boundary1 . "--" . $nl;
So, where is the bug? How can I debug such a Situation?
Thx in advance...
This one works....
$text = "Hello Mr. Xyz," . $crnl . "Some stupid text as an example." . $crnl;
$content = chunk_split(base64_encode($file));
$headers = "MIME-Version: 1.0" . $crnl;
$headers .= "From: " . $mail_from . $crnl;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary1 . "\"" . $nl . $crnl;
$headers .= "Content-Type: text/plain" . $nl;
$headers .= "--" . $boundary1 . $crnl;
$headers .= 'Content-Length: '.strlen($text) . $crnl;
$headers .= "Content-Transfer-Encoding: 8-bit" . $nl . $crnl;
$headers .= $text;
$headers .= "--" . $boundary1 . $crnl;
$headers .= "Content-Type: application/excel; charset=\"ISO8859-1\"; name=\"xyz.csv\"" . $nl . $crnl;
$headers .= "--" . $boundary1 . $crnl;
$headers .= "Content-Disposition: attachment; filename=\"xyz.csv\"" . $crnl;
$headers .= 'Content-Length: ' . strlen($content) . $crnl;
$headers .= "Content-Transfer-Encoding: base64" . $nl . $crnl;
$headers .= $content;
$headers .= "--" . $boundary1 . "--"
...with a small bug. I have an extra attment, but that's for now ok. We other thing is a longer play with "\n", "\r\n" and a mixture of both on the right places (lines).

Why is PHP's mail() sending me dozens of blank emails?

I'm using PHP's mail() function within a WordPress theme to send form submissions to my email.
However, mail() has automatically sent me at least 20 blank emails,
within the last five minutes, without being submitted.
Could someone shed some light on what I've done to create this situation?
Here is the code from my (functions.php file)
function send_my_form(){
$form = array();
$form['fstname'] = $_POST['fstname'];
$form['lstname'] = $_POST['lstname'];
$form['email'] = $_POST['email'];
$form['message'] = $_POST['message'];
$send_to = 'fakeemail#gmail.com';
$subject = 'You\'ve recieved an email from' . $form['fstname'] . $form['fstname'];
$return = "-f" . $send_to;
$message = "First Name: " . $form['fstname'] . "\r\n";
$message .= "Last Name: " . $form['lstname'] . "\r\n";
$message .= "Email: " . $form['email'] . "\r\n";
$message .= "Message: " . $form['message'] . "\r\n";
$headers = 'MIME-Version: 1.0' . '\r\n';
$headers .= 'Content-type: text/html; charset=iso-8859-1' . '\r\n';
$headers .= 'From: ' . $send_to . '\r\n';
$headers .= 'Reply-To: ' . $form['email'] . '\r\n';
$headers .= 'Return-Path: ' . $send_to . '\r\n';
$headers .= '\r\nX-Mailer: PHP/' . phpversion();
mail($send_to, $subject, $message, $headers, $return);
}
add_action('wp_head', 'send_my_form');
This is what I mean by running it in a conditional. Verify the fields are set before running the mail script.
function send_my_form(){
if(isset($_POST['fstname']) && isset($_POST['lstname']) && isset($_POST['email']) && isset($_POST['message']))
{
$form = array();
$form['fstname'] = $_POST['fstname'];
$form['lstname'] = $_POST['lstname'];
$form['email'] = $_POST['email'];
$form['message'] = $_POST['message'];
$send_to = 'fakeemail#gmail.com';
$subject = 'You\'ve recieved an email from' . $form['fstname'] . $form['fstname'];
$return = "-f" . $send_to;
$message = "First Name: " . $form['fstname'] . "\r\n";
$message .= "Last Name: " . $form['lstname'] . "\r\n";
$message .= "Email: " . $form['email'] . "\r\n";
$message .= "Message: " . $form['message'] . "\r\n";
$headers = 'MIME-Version: 1.0' . '\r\n';
$headers .= 'Content-type: text/html; charset=iso-8859-1' . '\r\n';
$headers .= 'From: ' . $send_to . '\r\n';
$headers .= 'Reply-To: ' . $form['email'] . '\r\n';
$headers .= 'Return-Path: ' . $send_to . '\r\n';
$headers .= '\r\nX-Mailer: PHP/' . phpversion();
mail($send_to, $subject, $message, $headers, $return);
}
}

PHP mail() - HTML shows up as an attachment once a file attachment is added

Had finally gotten all the bugs out of this and now they said "Oh, we'll need to add attachments..." So, this sends an html mail with a plaintext version and was doing just swell. Now that I have the attachments arriving the mail clients are showing the plaintext version inline and the html version as another attachment and then a seemingly empty 93 byte file with a name like ATT00248.txt.
Can anyone either bash me over the head from behind or tell me where I am going wrong? I want the HTML inline where available in the mail user interface, the plain text version where HTML is not available, and the single attachment as an attachment.
Any help?
<?php
$template = $_SERVER['DOCUMENT_ROOT'] . '/leads/templates/'.$_SESSION['templateFile'];
ob_start();
include($template);
$html = ob_get_contents();
ob_end_clean();
if (strlen($html) == 0) {
echo "The template at $template did not load.";
exit;
}
$email = $_SESSION['user']->email;
$name = $_SESSION['user']->first_name . ' ' . $_SESSION['user']->last_name;
$from = "$name <$email>";
$subject = unslash($_SESSION['subject']);
$TextMessage = strip_tags(unslash($_SESSION['message']));
$notice_text = "This is a multi-part message in MIME format.";
$plain_text = str_replace(' ',' ', $TextMessage);
if ($_SESSION['attachment']) {
$fileatt = 'files/' . $_SESSION['attachment'];
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));
$mailtype = 'mixed';
$fileatt_type = "application/octet-stream";
$fileatt_name = $_SESSION['attachment'];
} else {
$mailtype = 'alternative';
}
$semi_rand = md5(time());
$mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand";
$mime_boundary_header = chr(34) . $mime_boundary . chr(34);
$body = "$notice_text
--$mime_boundary
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
$plain_text
--$mime_boundary
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
$html
--$mime_boundary
";
$body .= "Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Disposition: attachment;\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--$mime_boundary\n";
// #1 //
if ($to = $_SESSION['recipients'][0]) {
mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/$mailtype;\n" .
" boundary=" . $mime_boundary_header);
echo "Email sent to " . htmlentities($to) . ".<br />";
}
// #2 //
if ($to = $_SESSION['recipients'][1]) {
mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/$mailtype;\n" .
" boundary=" . $mime_boundary_header);
echo "Email sent to " . htmlentities($to) . ".<br />";
}
// #3 //
if ($to = $_SESSION['recipients'][2]) {
mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/$mailtype;\n" .
" boundary=" . $mime_boundary_header);
echo "Email sent to " . htmlentities($to) . ".<br />";
}
// #4 //
if ($to = $_SESSION['recipients'][3]) {
mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/$mailtype;\n" .
" boundary=" . $mime_boundary_header);
echo "Email sent to " . htmlentities($to) . ".<br />";
}
// #5 //
if ($to = $_SESSION['recipients'][4]) {
mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/$mailtype;\n" .
" boundary=" . $mime_boundary_header);
echo "Email sent to " . htmlentities($to) . ".<br />";
}
// CC self? //
if ($_SESSION['cc_me']) {
mail($from, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/$mailtype;\n" .
" boundary=" . $mime_boundary_header);
echo "Email sent to " . htmlentities($from) . ".<br />";
}
if ($fileatt) {
unlink($fileatt);
}
echo "<a href='email_start.php'>Click here</a> to send another email.";
list($_SESSION['email'], $_SESSION['subject'], $_SESSION['bullets'], $_SESSION['message'],
$_SESSION['templateFile'], $_SESSION['template'], $_SESSION['cc_me'], $_SESSION['recipients']) = '';
?>
Pekka had it right - It was simple and robust to use Swiftmailer. http://swiftmailer.org
I'd post this as a comment, but it's too long.
// #1 //
if ($to = $_SESSION['recipients'][0]) {
mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/$mailtype;\n" .
" boundary=" . $mime_boundary_header);
echo "Email sent to " . htmlentities($to) . ".<br />";
}
// #2 ... #3 ... #4 ... #5
Will end up executing all blocks, since ($to = $_SESSION['recipients'][0]) will be always true. It will also display the "Email sent to ..." even when mail() fails.
What you want is:
if (in_array($to, $_SESSION['recipients'])) {
if (mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/$mailtype;\n" .
" boundary=" . $mime_boundary_header)) {
echo "Email sent to " . htmlentities($to) . ".<br />";
}
}
Or, if you really want to mail everyone, or
foreach ($_SESSION['recipients'] as $to ) {
if (mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/$mailtype;\n" .
" boundary=" . $mime_boundary_header)) {
echo "Email sent to " . htmlentities($to) . ".<br />";
}
}

Categories