PHP only sending one mail() in a page - php

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);

Related

How to set variable in while loop and call it after

I am trying to modify email notification code so that when a condition is met, a variable will change.
Basically, if the last $key_row->keyVal in the loop has any upper or lower letters in the 13 character string, then it will make the server addresss variable equal to server2.domain.com. Else server address variable equals server1.domain.com . Server address variable does not exist yet and won't be used outside this area.
Thank you for any help.
if (isset($_POST['emailcustomer']) && $_POST['emailcustomer'] == "1")
{
// Send email
$to = email_sanitize($row->ordEmail);
$subject = 'Keys';
$message = '
<b>Keys Order</b><br />
Thanks for your recent order. Here are the keys that you ordered: '.$row->ordID.':<br /><br />
';
while ($key_row = $key_query->fetch_object())
{
$message .= 'Username: ' . $key_row->keyVal . ' / Password: ' . $key_row->keyVal . ' / Expiration: ' . strtoupper(date('d-M-Y', strtotime($key_row->keyEnd))) . '<br />';
}
$message .= '<br />
<br />
Server Address: server.domain.com (<---Variable Here)<br />
';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $from_name . ' <' . $from_email . '>' . "\r\n";
mail($to, $subject, $message, $headers);
print 'Email sent.';
}
fetch the data first then put in an array
$data_info = array();
while($row=mysql_fetch_assoc($result_main)){
$data_info[]= $row;
}
then you will cal it using this
$z=0;
for($y=1;$y<=$Num_rows_Data;$y++){
$get_bar = $data_info[$z]['name_of_fields'];
$z++;
}

Customize a Php mail with html

I want to customize an php email with some html but I'm struggling with that...
After submit my form i receive the email but happen this
I was following this tutorial
I put the code i used.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Alright, lets send the email already!
if ( empty( $errorString ) ) {
$mailbody .= '<html><body>';
$mailbody .= '<img src="//http://xxxx/xxxxx/wp-content/uploads/2015/06/email.jpg" alt="Website Change Request" />';
$mailbody .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$mailbody .= __('Nome:', 'ci_theme' ) . ' ' . $clientname . "\n";
$mailbody .= __('Email:', 'ci_theme' ) . ' ' . $email . "\n";
$mailbody .= __('Chegada:', 'ci_theme' ) . ' ' . $arrive . "\n";
$mailbody .= __('Saída:', 'ci_theme' ) . ' ' . $depart . "\n";
$mailbody .= __('Adultos:', 'ci_theme' ) . ' ' . $guests . "\n";
$mailbody .= __('Crianças:', 'ci_theme' ) . ' ' . $children . "\n";
$mailbody .= __('Quarto:', 'ci_theme' ) . ' ' . $room->post_title . "\n";
$mailbody .= __('Mensagem:', 'ci_theme' ) . ' ' . $message . "\n";
$mailbody .= __('Hora do Check-in (hh:mm):', 'ci_theme' ) . ' ' . $timeguest . "\n";
$mailbody .= __('Contacto:', 'ci_theme' ) . ' ' . $contactguest . "\n";
$mailbody .= __('Autorização:', 'ci_theme' ) . ' ' . $autorizo . "\n";
$mailbody .= __('Cama Extra:', 'ci_theme' ) . ' ' . $camaextra . "\n";
$mailbody .= "</table>";
$mailbody .= "</body></html>";
mail($to, $mailbody, $headers);
// If you want to receive the email using the address of the sender, comment the next $emailSent = ... line
// and uncomment the one after it.
// Keep in mind the following comment from the wp_mail() function source:
/* If we don't have an email from the input headers default to wordpress#$sitename
* Some hosts will block outgoing mail from this address if it doesn't exist but
* there's no easy alternative. Defaulting to admin_email might appear to be another
* option but some hosts may refuse to relay mail from an unknown domain. See
* http://trac.wordpress.org/ticket/5007.
*/
//$emailSent = wp_mail(ci_setting('booking_form_email'), get_option('blogname').' - '. __('Booking form', 'ci_theme'), $mailbody);
$emailSent = wp_mail(ci_setting('booking_form_email'), get_option('blogname').' - '. __('Formulário de Reserva','theme-text-domain', 'ci_theme'), $mailbody, 'From: "'.$clientname.'" <'.$email.'>');
$emailSent2 = wp_mail( $email, __('Booking Inquiry','theme-text-domain', 'ci_theme'), __('Thank you so much for your interest in Hotel Aveiro Center! We will get back to you within 24 hours to answer your request.','theme-text-domain','ci_theme'),'From: Hotel Aveiro Center <xxxx#xxxx>');
}
Here is a simple example I have done .
//build email to be sent
$to = $email;
$subject = $site_url;
$subject .= ": Activate Your Account";
$message = "
<html>
<head>
<title>Account Activation</title>
</head>
<body>
<h3>Account Activation</h3>
<p>Dear ".$user.", thank you for registering at ".$site_url.".</p>
<p>Please click on the link below to activate your account:</p>
<a href='".$site_url."/confirm_user_reg.php?activation_key=".$activation_key."'>http://www.".$site_url."</a>.
<p>If the above link does not work, copy and paste the below URL to your browser's address bar:</p>
<p><i>http://www.".$site_url."/confirm_user_reg.php?activation_key=".$activation_key."</i></p><br/>
<p>If you did not initiate this request, simply disregard this email, and we're sorry for bothering you.</p>
<br/><br/>
<p>Sincerely,</p>
<p>The ".$site_url." Team.</p>
</body>
</html>
";
// To send HTML mail, the Content-type header must be set
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

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

Putting new line feed in the text for an email php

I am using the following code
$message = $mess0 . "</br>" . $mess1 . "</br>" . $mess2 . "</br>" . $mes1 . "</br></br>" . $mes2 . "</br>" . $mes23 . "</br></br>" . $mes3 . "</br></br>" . $mes4 . "</br>" . $mes5 . "</br>" . $mes6 . "</br>" . $mes7 . "</br>" . $mes8 . "</br>" . $mes9 . "</br></br>" . $mes10 ;
$message = "<html><body><p>".$message."</p></body></html>";
$this->Mail($storeEmail, $subject, $message);
function Mail($to, $subject, $message)
{
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: The Vow Engagement Ring Finder <thevow.engagement#gmail.com>' . "\r\n";
// Mail it
if(!mail($to, $subject, $message, $headers)) {
throw new Exception('There was a problem trying to send an email.');
}
}
The problem is all is i get is one paragraph. I have added <br>s but its like they don't work. The mail I get is simple paragraph without any new line feeds.
Mistake in <br /> tags
$message = $mess0 . "</br>" . $mess1...
^^^^^^
Replace all </br> with <br />
It will not work. You have to use \r\n for line breaks. Also try <br />.

replying to email address on the from

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);

Categories