I wrote a script such that if the form fails, the user sees an error message and an email should be sent to me. However, I'm not getting the email. I don't see any mistakes in my script and the email is nowhere to be found (not even in spam). What could be the issue? I believe we are working on a Linux server, so a Mail agent should be installed by default, right? Could a firewall be blocking the messages?
The code:
<?php
if (isset($aErrors) && count($aErrors) > 0 ) {
print '<ul class="errorlist">';
foreach ( $aErrors as $error ) {
print '<li>' . $error . '</li>';
}
print '</ul><br />';
$mail_ontv = "info#example.com";
$_POST['onderwerp'] = "Geen Twinfield Abonnement";
// set datum
$datum = date("d.m.Y H:i");
// set ip
$ip = $_SERVER['REMOTE_ADDR'];
$inhoud_mail .= $_SERVER['SCRIPT_URI'] . "\n\n";
$inhoud_mail .= "Iemand probeerde zich aan te melden, maar had geen Twinfield abonnement!\n";
$inhoud_mail .= "Bedrijfsnaam: " . $_SESSION['bedrijfsnaam'] . "\n";
$inhoud_mail .= "Telefoonnummer: " . $_SESSION['telefoonnummer'] . "\n";
$inhoud_mail .= "E-mail adres: " . $_SESSION['email'] . "\n";
$inhoud_mail .= "Telefoonnummer contactpersoon: " . $_SESSION['telefoonnummercontact'] . "\n";
$inhoud_mail .= "E-mail adres contactpersoon: " . $_SESSION['emailcontact'] . "\n";
$inhoud_mail .= "Gewenste gebruikersnaam: " . $_SESSION['gebruikersnaam'] . "\n";
$inhoud_mail .= "Gekozen abonnement: " . $_SESSION['abonnement'] . "\n\n";
$inhoud_mail .= "Verstuurd op " . $datum . " via het ip " . $ip . "\n\n";
$headers = "From: " . $_SESSION['bedrijfsnaam'] . " <" . $_SESSION['emailcontact'] . ">";
$headers = stripslashes($headers);
$headers = str_replace("\n", "", $headers); // Verwijder \n
$headers = str_replace("\r", "", $headers); // Verwijder \r
$headers = str_replace("\"", "\\\"", str_replace("\\", "\\\\", $headers)); // Slashes van quotes
$_POST['onderwerp'] = str_replace("\n", "", $_POST['onderwerp']); // Verwijder \n
$_POST['onderwerp'] = str_replace("\r", "", $_POST['onderwerp']); // Verwijder \r
$_POST['onderwerp'] = str_replace("\"", "\\\"", str_replace("\\", "\\\\", $_POST['onderwerp'])); // Slashes van quotes
mail($mail_ontv, $_POST['onderwerp'], $inhoud_mail, $headers);
}
?>
Thanks guys!
Several extra thoughts for you beyond what Mac B suggested:
1) There are many PHP libraries out there that will take care of sending an email for you. They can work with a local or a remote SMTP server. For example, http://swiftmailer.org/.
2) Most email providers these days (Gmail, Yahoo, etc) will spam-block email that comes from just your local computer. I recommend using a cheap third-party SMTP service to send your emails - this will help with reliability. One I've been using for years is called CritSend (critsend.com). There are many others.
Did you check the return value of mail()? If you get a boolean fALSE, something blew up while PHP was trying to handle the email over to your SMTP server.
If mail returns true, then the handoff was successful, and you'll have to go look at the SMTP server's logs to find out what happened to the email from that point.
Remember: PHP's involvement with sending email, in a real world equivalent, is taking the envelope and dropping it into a mailbox. That's it. Everything else is completely outside of PHP's purview and control.
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 2 years ago.
This is my code and it's return true but i didn't receive any mail. It's working on another website. I want to check my code is working or not.
$name = $this->input->post('name');
$email = $this->input->post('email');
$phone = $this->input->post('phone');
$message = $this->input->post('msg');
// Email content
$body = "Hi xxx<br /><br />";
$body.= "Name : " . $name . "<br />";
$body.= "Email : " . $email . "<br />";
$body.= "Phone No : " . $phone . "<br />";
$body.= "Message Placed: " . $message . "<br />";
$body.= "<br />For Enquiry Request</br> ";
$subject = "Consultation email";
$to ="xxxxx#xxxxx.in";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <'.$email.'>' . "\r\n";
if(mail($to,$subject,$body,$headers))
{
echo '<script>alert("Your message has been sent, our associate will get back to you soon")</script>';
echo '<script>window.location.href="' . base_url() . '"</script>';
}
else{
echo '<script>alert("your message has been not sent")</script>';
echo "<script>window.location.href='" . base_url() . "'</script>";
}
There are a few things to consider and one of them is that you cannot send emails from localhost. There may be a work around but generally no. Below is an article to assist you if you are sending the mail from a localhost environment
https://www.phpflow.com/php/how-to-send-email-from-localhost-using-php/#:~:text=We%20can%20send%20mail%20from,ini%20file.&text=The%20sendmail%20package%20is%20inbuilt,easily%20send%20mail%20from%20localhost.
And if you are not in a local environment, the below answer from a previous question seems to have enough answers to help you out.
PHP mail function doesn't complete sending of e-mail
Also, you have stated to be using codeigniter. I think you are missing some few things there. check out this other article to fix you up.
https://www.cloudways.com/blog/send-email-codeigniter-smtp/
Let me know which of these helped. If none let me know.
Strange issue - my sendmail.php is working perfectly on desktop and on mobile devices only when requesting desktop websites (in Chrome app), but when using mobile site he does not work at all.
Can someone help me figure this out?
here is the code:
<?php
if(isset($_POST['email'])) {
if (!check_email($_POST['email']))
{
echo 'Please enter a valid email address<br />';
}
else send_email();
}
exit;
function check_email($emailAddress) {
if (filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
return TRUE;
} else {
return FALSE;
}
}
function send_email() {
$message = "\nName: " . $_POST['name'] .
"\nEmail: " . $_POST['email'] ;
$message .= "\nMessage: " . $_POST['comment'] .
"\n\nBrowser Info: " . $_SERVER["HTTP_USER_AGENT"] .
"\nIP: " . $_SERVER["REMOTE_ADDR"] .
"\n\nDate: " . date("Y-m-d h:i:s");
$siteEmail = $_POST['receiver'];
$emailTitle = $_POST['subject'];
$thankYouMessage = "Thank you for contacting us, we'll get back to you shortly.";
if(!mail($siteEmail, $emailTitle, $message, 'From: ' . $_POST['name'] . ' <' . $_POST['email'] . '>'))
{
echo 'error';
}
else
{
echo 'success';
}
}
?>
You must make sure the mobile form has these two elements:
It is being submitted using method="POST".
Your email input has the attribute and value name="email".
I demonstrate where these things are set in the following code. This is incomplete, of course, it's just designed to show you where the two required parts must be.
<form ... method="POST">
...
<input type="text" name="email" ... >
...
</form>
I need to mention one more thing ...
That being said, what you're doing here is EXTREMELY INSECURE. You are allowing someone to set an email's from and two address on a web form. A (not so) clever hacker can easily write a script to turn your server into an open relay for spam or other evil activities. At minimum, you should remove $_POST['receiver'] and replace with with a hard-coded email address or at least not something that can be altered by an end-user when they POST to your form.
So I drilled down and found that the mobile form is directing submissions through this PHP file which was 404.
The issue now is, even when this file is avaiable, emails are not sent...
' . "\r\n"; $headers .=
'From: ' . "\r\n"; $headers = 'MIME-Version: 1.0' .
"\r\n"; $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$message .= "Name: " . $name . ""; $message .= "Subject: "
. $subject . ""; $message .= "Email: " . $email . ""; $message .= "Message: " . $message_text . "";
mail($to, $subject, $message, $headers, "-f noreplay#info.com");
echo 1; }
if(isset($_POST['sendmail']) && $_POST['sendmail'] == 1){
send_mail(); } ?>
I am very new to PHP, so be gentle.
It seems I cant run HTML in PHP.
For example when I try to
echo "<script> success(); </script>";
Result will be:
<script> success(); </script>
Another example:
$msg2 = "<b>Dear</b> ".$name ."\n\n" ."Thank you for your registration, we will be contacting you within 48h."."\n\n" ."Yours ....";
Result will be:
<b>Dear</b> (name of the client)
Thank you for your registration, we will be contacting you within 48h.
Yours ....
I checked the meta its:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
I will give You guys the code ( some of it is in Estonian )
<?php
if (isset($_REQUEST['email'], $_REQUEST['nimi'], $_REQUEST['koolitus'], $_REQUEST['telf'], $_REQUEST['kuupaev'], $_REQUEST['tingimused'])) {
$admin_email = "ville.madison#gmail.com";
$email = $_REQUEST['email'];
$koolitus = $_REQUEST['koolitus'];
$nimi = $_REQUEST['nimi'];
$kuupaev = $_REQUEST['kuupaev'];
$telf = $_REQUEST['telf'];
$marked = $_REQUEST['marked'];
$kodulehemail = "koolitused#registreerumine.ee";
$koolitaja = $_REQUEST['koolitaja'];
$beauty = "info#beautyfactory.ee";
$msg = "Koolitusele registreerumine: " . $koolitus . "\n" . "Soovitud koolitaja: " . $koolitaja . "\n\n" . " Nimi: " . $nimi . "\n" . " Email: " . $email . "\n" . " Telefon: " . $telf . "\n" . " Soovitud kuupäev (aaaa-kk-pp): " . $kuupaev . "\n\n" . "Märked: " . $marked;
$msg2 = '<b>Hea</b> ' . $nimi . "\n\n" . "Täname Teid koolitusele broneerimast, teatame Teile registreeringu kinnitamisest 48h jooksul." . "\n\n" . "Teie Beauty Factory";
mail($admin_email, "Koolitusele registreerumine", $msg, "From:" . $kodulehemail);
mail($email, "Koolitusele registreerumine", $msg2, "From:" . $beauty);
}
?>
At the moment there is only one word that I am trying to make in bold for the sake of testing. But when I get it working I will start editing it more and I want to echo a javascript function in the php if statement.
I have tried using single quotes, double quotes and the:
nl2br(htmlentities($msg2));
None of it works
The message you send is not interpreted as HTML. The default
header of the mail function is text/plain.
If you want your customers to see the bold text, you must specify the content-type as HTML in the message header.
<?php
// Set the content type of the email to HTML (and UTF-8)
$header = 'Content-type: text/html; charset=utf-8' . "\r\n";
// Add the from field
$header .= 'From: ' . $beauty . "\r\n";
// Send email
mail ($email, "Koolitusele registreerumine", $msg2, $header);
?>
However you will not be able to use Javascript as it is intepreted client side. PHP does not interpret Javascript, and most of the mail clients will not execute Javascript for obvious security reasons.
Here is a sample example of how to use html tags in php:
<?php
var $name = "xyz";
var $email = "xyz#gmail.com";
echo '<table border="1" style="width:20%">';
echo '<tr><th>UserName</th><th>Email</th></tr><tr><td>';
echo $name."<br>";
echo '</td><td>';
echo $email."<br>";
echo '</td></tr></table>';
?>
This is basic php and html table format example,you can follow this for write html tags.
The problem was that the function mail() had a default content of text/plain, just needed the $header to make text/html. Thanks guys for your quick answers !
I have a contact form that I'm writing using jQuery and PHP. Below is the PHP bit, which works perfectly fine when I change the $youremail variable to my gmail account, but the emails are never received in my office Outlook account.
Is there something I'm missing?
<?php
// Email Vars
$youremail = "my_email#on_outlook2007.com";
$headers = "From: $name <$email>\n";
$subject = "Subject Line Here";
$ip = $_SERVER['REMOTE_ADDR'];
$message_clean = html_entity_decode(stripslashes($message));
// Format Email
$email_format_cc = $message_clean . "\n\n" . "___________________________________________________________________________" . "\n\n" .
"Name: " . $name . "\n" .
"Company: " . $company . "\n" .
"IP Address: " . $ip . "\n" .
"Sent: " . $timestamp;
$email_format = $email_format_cc . "\n" .
"Page: " . $from_url;
mail($youremail, $subject, $email_format, $headers);
?>
Check your server's mail log to see if/why gmail is bouncing the mail. PHP's mail() function is extraordinarily stupid and claims success even as the universe explodes around it.
What I have is this:
$SQL = "SELECT * FROM users";
$query = mysql_query($SQL) OR die(mysql_error());
$row = mysql_fetch_array($query);
$implodeEmail = implode(", ", $row['paypal']);
$mailTo = "noreply#" . $_SERVER['HTTP_HOST'];
$mailSubject = $bootTitle;
$mailMessage = $_POST['massemail'];
$mailHeaders = "From: noreply#" . $_SERVER['HTTP_HOST'] . "\r\n" .
"Reply-To: noreply#" . $_SERVER['HTTP_HOST'] . "\r\n" .
"X-Mailer: PHP/" . phpversion() . "\r\n" .
"Bcc: " . $implodeEmail;
if (mail($mailTo, $mailSubject, $mailMessage, $mailHeaders))
{
echo "e-Mail successfully sent.";
}
It successfully sends to the $mailTo, but I want it to Bcc to everyone's paypal email, and that isn't working. I thought about just making all emails visible, but I really don't want that, and I don't want to loop through each one and send out an email for each person.
Ideas/help?
just a rough idea:
$implodeEmail = implode("; ", $row['paypal']);
or just noticed that instead of:
$row = mysql_fetch_array($query);
$implodeEmail = implode(", ", $row['paypal']);
should be:
while($row=mysql_fetch_array($query)) $rows[]=$row['paypal'];
$implodeEmail = implode(", ", $rows);
If you include all the addresses in the Bcc header, the server should send a copy to each one. If he doesn't do that, something is wrong with the server. Look in the php.ini mail config, and test your mailserver. And yeah, you should use ";" instead of "," to implode the addresses.
Try phpMailer or SwiftMailer.