Mail function always calling the else part - php

I have two domain name. For example https://example.com and https://example2.com.
I am sending test mail using mail function.
I am able to send the email from https://example.com and I am getting the success but the same code I am using for https://example2.com and I am not getting the email it's always calling the else part.
Test mail
<?PHP
$sender = 'xxx#xx.com';
$recipient = 'zzz#zz.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From: ' . $sender . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html;charset=UTF-8" . "\r\n";
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
else
{
echo "Error: Message not accepted";
$errorMessage = error_get_last()['message'];
echo $errorMessage;
}
?>

The From header is not valid as the next one is concatenated right after it:
$headers = 'From:' . $sender;
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
Should be something like:
// add \r\n
$headers = 'From: ' . $sender . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html;charset=UTF-8" . "\r\n";

Try this simple php mail function
$to = $email_id;
$subject = "";
$message = "Your mail Body Content. you also use here HTML Tags for better performence. ";
$headers = 'From: xyz#domainname.com' . "\r\n" .
'Reply-To: xyz#domainname.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers.= "MIME-Version: 1.0" . "\r\n";
$headers.= "Content-type:text/html;charset=UTF-8" . "\r\n";
if(mail($to, $subject, $message, $headers)){
echo "Message accepted";
}else{
echo "Error: Message not accepted";
}

Check if mail function is enabled on example2.com by using below:
if ( function_exists( 'mail' ) )
{
echo 'mail() is available';
}
else
{
echo 'mail() has been disabled';
}
If disabled, enable it via php.ini or in cPanel.

Related

How do i add from address using CodeIgniter?

I'm working on small CodeIgniter application where I have to build my own contact us form, everything is working fine, receiving email but I need just add From Address in the mail function?
CodeIgniter Mail Function
public function form() {
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'trim|required|alpha');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('phone', 'Phone', 'trim|required|numeric');
$this->form_validation->set_rules('message', 'Message', 'trim|required');
$data['success'] = false;
if ($this->form_validation->run() == TRUE) {
#mail(config('webmaster_email'), 'Contact Us from ABC',""
. "Full Name: $_POST[name]\n"
. "Email: $_POST[email]\n"
. "Phone: $_POST[phone]\n"
. "Message: $_POST[message]\n"
. "");
$data['success'] = true;
}
$this->load->view($this->module, $data);
}
Need To Add This Line In The Form Mail Function
'From: webmaster#example.com'
You need to use mail headers to send email from your desired email address.
if you need simple email without attachment and markup use mail function else i would prefer using PHP Mailer, see How to send mail using phpmailer
function sendmail($to, $subject, $message, $from)
{
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: ' . $from . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
#mail($to, $subject, $message, $headers);
if ($result) return 'sent';
else return 'error';
}
$to = config('webmaster_email');
$from = "webmaster#example.com";
$subject = "Contact Us from ABC";
$message =
"Full Name: $_POST[name]\n"
. "Email: $_POST[email]\n"
. "Phone: $_POST[phone]\n"
. "Message: $_POST[message]\n";
$result = sendmail($to, $subject, $message, $from);
var_dump($result);
PHP mail() 4th parameter used for headers.
Complete PHP Email headers:
$headers = "From: Your Website < mail#yourwebsite.com >\n";
$headers .= "Cc: Your Website < mail#yourwebsite.com >\n";
$headers .= "X-Sender: Your Website < mail#yourwebsite.com >\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "X-Priority: 1\n"; // Urgent message!
$headers .= "Return-Path: mail#Your Website.com\n"; // Return path for errors
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";

PHP mail() works for Yahoo but not for Gmail

This part of my code sends mail to yahoo but doesn't send mail to Gmail. I also referred a lot of related FAQs' and documentation on PHP mail() but those didn't work out for me.
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if(!empty($_POST['submit']))
{
$to = $_POST['email'];
$subject = 'Annual Report';
$message = $_POST['template'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: from#example.com" . "\r\n";
$headers .= "Reply-To: replyto#example.com" . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
$headers .= "X-Priority: 1" . "\r\n";
$retval = mail($to,$subject,$message,$headers);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
}
else
{
echo "empty";
}
}

how to change the from attribute to my email in mail() function

I am trying to send an email to my customers when they place an order on my website. But the from appears like this:
From : n9da2313
I need to exchange it with info#awraq.me
I tried this but didn't work
`
$to = "founder#awraq.me";
$subject = "Your order";
$message = "Your order was placed successfuly";
$headers = "From: info#awraq.me";
$headers .= "\r\nReply-To: info#awraq.me";
$headers .= "\r\nX-Mailer: PHP/".phpversion();
mail($to,$subject,$message,$headers,"-f info#awraq.me");
`
try this-
$to = "somebody#example.com, somebodyelse#example.com";
$subject = "HTML email";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to,$subject,$message,$headers);
If its not working then check if you have done some configuration in php.ini file.

Reply to won't work on php form submitting to itself

I can't get the reply to function to work on a contact form that submits to itself.
The BCC is working fine. Any help is appreciated.
$to = 'email#email.com';
$subject = ''.$_POST['emailsubject'].'';
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1';
$headers .= 'Reply-To: '.$_POST['email'].'';
'X-Mailer: PHP/' . phpversion();
$headers .= 'Bcc: email#email.com' . "\r\n";
mail($to, $subject, $msg, $headers); $sendMessage = true; unset($_POST); } } ?> <?php if($invalidCaptcha) { ?>
<?php
$from_add = "test#gmail.com";
$to_add = "email#email.com";
$subject = "Test Msg";
$message="test msg.";
$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";
if(mail($to_add,$subject,$message,$headers))
{
$msg = "Mail sent OK";
}
else
{
$msg = "Error sending email!";
}
?>

Multiple php email issue

For my project I had to create a function that sends two email. One to the customer and the other to the a seller. Both emails will have different contents.
I wrote the two function using the standard PHP mail function as below.
$to = "xxxx#xxxx.com";
$subject = 'xxxx';
$message = "hello"
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
Now, While testing the system on my own company's web server both emails seems to be sent and received. However, when I migrated the same system into an external server. only one email gets sent. primarily, the first email in the stack.
While I suspect the issue has something to do with the later server configuration, I am wondering where should I go next to debug this issue.
There were a few things in your "posted" code that were missing.
A missing semi-colon at the end of $message = "hello" (unless that was a typo/paste error?) and a dot in the first $headers
Also, not having a From: header attribute will surely result having the Email sent to and regarded as SPAM.
Having fixed those issues and added extra header information, the following code worked and did not end up in my SPAM, but the INBOX successfully.
<?php
$to = "xxxx#xxxx.com";
$email = "email#example.com";
$subject = 'xxxx';
$message = "hello";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
?>
Or with a success echo'ed message:
<?php
$to = "xxxx#xxxx.com";
$email = "email#example.com";
$subject = 'xxxx';
$message = "hello";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if(mail($to, $subject, $message, $headers))
{
echo "Message sent.";
}
else{
echo "Something went wrong.";
}
?>
Visit the PHP.net website for more information on the mail() and header() functions.
http://php.net/manual/en/function.mail.php

Categories