Mail sending error PHP, didn't get message [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
This is my code. I spent hours debugging it. I can't see the error. Why didn't I didn't receive the email?
if(isset($_POST['submit'])){
$to = "admin#example.com";
$subject = "Mesej Website";
$from = $_POST['email'];
$headers = "From: " . strip_tags($from) . "\r\n";
$headers .= "Reply-To: ". strip_tags($from) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<h3>Mesej Daripada '.$_POST['name'].'</h3>';
$message .= '<div style="margin-top:20px;">'.$_POST['message'].'</div>';
$message = '</body></html>';
mail($to,$subject,$message,$headers) or die('mail sending error');

The in built mail function of PHP needs configuration for sending the email.However, if you are using the third party hosting provider this should be already configured or else you need to configure it in php.ini file.
To avoid these configuration issues you can use the phpmailer library where you can all the parameters easily and in a more efficient way.

Related

PHP mail() works randomly? [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
So I have a normal but long HTML form on this page
The PHP mail script for the form is:-
$to = "something#gmail.com";
$from = $_POST['contact_email'];
$subject = "Application form submission";
$message = "<h2>Tell us what you need?</h2>";
$message .= "Loan Amount Required ?";
$message .= "<br>";
$message .= $_POST['tell_loan_amount'];
$message .= "<br>";
$message .= "<br>";
$message .= "What For?";
$message .= "<br>";
$message .= $_POST['tell_what_for'];
/* and so on */
$headers = "From: $from" . "\r\n" ;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset: utf8\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
if( mail($to,$subject,$message,$headers)) {
echo "<h1>Thank you for the Application</h1>";
echo "<p>We will now review the application. This process normally takes 2-3 Business Hours. If we need to discuss any aspect of the application we will contact you. If you have any questions at all please do not hesitate to contact us on <strong>1300 815 462</strong></p>";
}
else {
echo "failed";
}
Now the problem is that mail only gets sent sometimes. I have had several clients contact and said that they successfully reached the thank you page and received the success message but we never received the mail. And yes, not in Spam either.
Is it happening because it is set to gmail?
Or is it happening because of incorrect encoding? (Our clients are filling form in English.)
Or do I need to use mb_send_mail() instead of mail() and remove the encoding code altogether?
use this command on you server it will output what happen to your sent mail :
tail -f /var/log/maillog
sometimes the provider block port 25

After registration my email goes to spam and not inbox [duplicate]

This question already has answers here:
How do I prevent mails sent through PHP mail() from going to spam? [duplicate]
(6 answers)
Closed 5 years ago.
I have done everything possible but after registration my email goes to spam and i want it to go into inbox
$subject = 'Confirm your email';
$to = $email;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: CGOTY <info#cgoty.com>' . "\r\n";
$body='Hi, Click here to confirm your email https://cgoty.com/activate.php?encrypt='.$encrypt.'&action=activate ';
mail($to,$subject,$message,$headers);
You made a mistake in your script within you mail contructor of function you said message instead of body message instead of body.
So it should look like this
$body='Hi, Click here to confirm your email https://cgoty.com/activate.php?encrypt='.$encrypt.'&action=activate ';
mail($to,$subject,$body,$headers);
It will work for you
Add this to your header:
$headers .= "Reply-To: $email" . "\r\n";
(replace $email with the mail you want)

Can we send HTML emails using PHP? [duplicate]

This question already has answers here:
Send HTML in email via PHP
(8 answers)
Closed 7 years ago.
I have been working on a email script and it works. Prob is I do it using a textarea . can I send an HTML email?if so , how?
Edit: Found the answer
Ofcourse you can. You have to set headers for that
$to = 'abc#example.com';
$subject = 'My Subject';
$message='<b>test</b>';
$headers = "From: test#test.com\r\n";
$headers .= "Reply-To: test#test.com\r\n";
$headers .= "CC: test2#test.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to,$subject,$message,$headers);

php mail function is not working on online server [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
i want to send a mail the code is given below. i cant understand waht is happening . It works on localhost but not on live server.
if (isset($_POST['test_mail'])){
$to = '2606ankit#gmail.com'; //put email address on which mail send
$subject = "Newsletter"; //Put subject of mail here
$from = 'ankit#studiokrew.com'; //put email address from
//email body start
// $body .= file_get_contents('file/'.$filename.'');
$body .= 'anio';
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: '.$from. "\r\n";
//if you need to send cc mail then uncomment below line and change email address
//$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to,$subject,$body,$headers);
}
try this..
<?php if (isset($_POST['test_mail'])){
$host=$_SERVER['HTTP_HOST'];
$replyto="<no-reply >";
$to ='2606ankit#gmail.com';
$subject = "Newsletter";
$from = 'ankit#studiokrew.com';
$headers = "From: \"Invoice\"<noreply#$host>\n";
$headers .= "Reply-To: ".$replyto."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"abc"\"\r\n\r\n";
$headers .= 'From: '.$from. "\r\n";
$header .= "Content-Type:text/html; charset=\"iso-8859-1\"\n";
$body = "This is a multi-part message in MIME format.\r\n";
$body .= "Content-type:text/html; charset=iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= 'anio';
mail($to,$subject,$body,$headers);
}
?>
check the MTA logs on your server.also web server logs the answer will be there somewhere.
Test with a simple mail() script that does not require any external data, just to make sure that your server indeed can send mail. If that fails, you'd have to contact the server admins for help.

Send mail functionality not working [duplicate]

This question already has an answer here:
php email form issue
(1 answer)
Closed 9 years ago.
I was trying to send mail using following method. But if I use $headers the functionality is not working.Otherwise it is working fine. Where I went wrong?
if(isset($_REQUEST['submit']))
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$phone=$_REQUEST['phn'];
$country=$_REQUEST['country'];
$company=$_REQUEST['company'];
$subject=$_REQUEST['subject'];
$enquiry=$_REQUEST['message'];
$subject = "xyz.com-".$subject;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$name."\r\n";
$headers .= "Reply-To: ".$email."\r\n";
$msg="
<table><tr><td>Name</td><td>".$name."</td></tr>
<tr><td>Email:</td><td>".$email."</td></tr>
<tr><td>Contact Number:</td><td>".$phone."</td></tr>
<tr><td>Country:</td><td>".$country."</td></tr>
<tr><td>Company:</td><td>".$company."</td></tr>
<tr><td>Message:</td><td>".$enquiry."</td></tr>
</table>";
if(mail("myName#gmail.com",$subject,$msg,$headers))
{
$msg1="Email Sent Successfully";
}
else
{
$msg1='Mail not sent. Please try again';
}
}
Change the line
$headers .= "From: ".$name."\r\n";
with
$headers .= "From: ".$name."<".$email.">"."\r\n";
Everything else seems good, cheers!
Do not use mail() directly, but find one of the many premade classes for mail sending from the net!
As you dig deeper into mailsending, you WILL face alot of problems, which already have been addressed in mail libraries such as PHPMailer or alike.
Besides of that, your code is a perfect example on "how to build a spam mail application" because it is susceptible for Mailheader-Injection attacks!

Categories