I have this piece of test code to send an HTML email. For the longest time, it was sending, but not sending in HTML format. And now, it's not sending at all.
I've been comparing with code on the PHP manual, and with similar questions on this site, and I can't see anywhere that the code could be wrong. It's driving me crazy.
Why is this code not sending, and why is it not sending in HTML?
$to = "myemail#mysite.com";
$subject = "Confirmation code for registration";
$message = "<html>
<head>";
$message .= '<meta http-equiv="content-type" content="text/html; charset=utf-8" />';
$message .= "<title>TITLE</title>
</head>
<body>";
$message .= "Thank you for registering! \r\n";
$message .= "
</body>
</html>";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset: utf-8\r\n";
$headers .= "From: My Site <registration#mysite.com>r\n";
$mailresult = mail($to, $subject, $message, $headers);
echo "mail sent! ";
echo $mailresult;
$mailresult comes back as 1, which I assume means success, but no mail is being recieved at the mail account specified.
According to php.net:
Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
Have you configured a mail server to send the mail?
Your code worked on my server perfectly but I received the email as Spam - so check your spam. And do your research to check if your server is configured properly.
Try doing
mail($to, $subject, $message, $headers);
without $mailresult
Related
For some reason when I send an email message, the HTML is still being displayed in plain text. Can someone please tell me what I'm doing wrong here?
Here is the code:
$to = $_SESSION['customer_email'];
$from = "noreplay#mysite.com";
$subject = "Confirmation for Appointment on ".$_SESSION['display_date'];
$headers = "From: ".$from."\r\n";
$headers .= "Content-type: text/html; charseet=UTF-8;\r\n";
$headers .= "MIME-Version: 1.0;\r\n";
$message = "<p>Thank you".$_SESSION['firstname']." for booking online. Your appointment has been confirmed and is scheduled for <strong>".$_SESSION['display_date']."</strong> between the hours of <strong>".$_SESSION['display_time']."</strong>.</p><p> Please expect our crew to arrive between your two hour arrival window. You will receive a courtesy call when our crew is <span>15-30 minutes</span> away. A confirmation email has been sent to <strong>".$_SESSION['customer_email']."</strong>. </p><p> If you have any questions or need to make any changes to your appointment, please contact our office at <strong>1-800-333-1111</strong>. Thank you for choosing Us!
</p>";
if(mail($to, $subject, $message, $header ))
{
$msg = "Your message has been sent";
}
$headers .= "Content-type: text/html; charseet=UTF-8;\r\n";
should be
$headers .= "Content-type: text/html; charset=UTF-8;\r\n";
Two typos:
if(mail($to, $subject, $message, $header ))
should be
if(mail($to, $subject, $message, $headers ))
and in
$headers .= "Content-type: text/html; charseet=UTF-8;\r\n";
change your 'charseet' to
Content-Type: text/html; charset=ISO-8859-1\r\n
It appears nowadays you can use UTF-8
Content-Type: text/html; charset=UTF-8\r\n
and
if(mail($to, $subject, $message, $header))
to
if(mail($to, $subject, $message, $headers))
also mail's RETURN:
Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
It is important to note that just because the mail was accepted for delivery, it
does NOT mean the mail will actually reach the intended destination.
I am working on a WordPress plugin in which I send email using the PHP mail function, but there is a problem with the mail function. It sends emails to my non-Gmail account, but it's doesn't send emails to my Gmail account. I am using following code:
function send_mail()
{
global $wpdb;
$to = 'mymail#gmail.com';
$subject = 'Hello';
$name='my name';
$from="name#mydomain.com";
$message = "
<html>
<head>
<title>my title</title>
</head>
<body>
<div>
<tt> ".Hii How Are you."</tt>
</div>
</body>
</html>";
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset=iso-8859-1\r\n";
$header .= "From: ".$name."<".$from.">\r\n";
mail($to, $subject, $message, $header);
}
Is there something wrong with my code, or is there some issue with the mail function? If any alternate method is available to send email, please give me the link.
Check if adding fifth variable works for you... here is my code for sending emails.
if( mail( $recipient, $subject, $message, $headers, "-f noreply#mydomain.com"))
return "success";
Check the spam folder, It might be there. Its a server issue, it hapened to me also many times. Gmail blocks mails or sends to spam from some servers due to some reasons. Ask your server provider to check why mails are not going to gmail inbox.
use the code sample as below, email address in <> and that last paramter worked for me.
$headers = 'From: <test#test.com>' . "\r\n" .
'Reply-To: <test#test.com>';
mail('<myEmail#gmail.com>', 'the subject', 'the message', $headers,
'-fwebmaster#example.com');
?>
It is the answer provided in PHP mail() function will not send to gmail but will send to my non-gmail account by ARH3, which i have tried and tested
I have a php form that I made and when someone fills out the form it works fine with all other email accounts (yahoo, hotmail, #mydomain.com, etc) but when it comes to sending the email to a gmail user, the email never comes through, not even in the spam box.
$from = $email;
//Testing Below
//$headers = 'From:noreply#mydomainname.com' . "\r\n";
$headers .= "From: $from \n";
$headers .= " MIME-Version: 1.0\r\n ";
$headers .= " Content-Type: text/html; charset=ISO-8859-1\r\n ";
//Mail The Message
mail($to, $subject, $message, $headers);
The code above will not send to gmail but will work for all other email accounts. This one (below) will work for gmail but of course then I don't have the option of html in the email.
$headers = 'From:noreply#mydomainname.com' . "\r\n";
//$headers .= "From: $from \n";
//$headers .= " MIME-Version: 1.0\r\n ";
//$headers .= " Content-Type: text/html; charset=ISO-8859-1\r\n ";
//Mail The Message
mail($to, $subject, $message, $headers);
So basically when I add the MIME and Content type to the headers variable it wont send to a gmail account.
Thanks in advance!
I have replied a question not too long ago related to this. It might have to do with the gmail's email filtering, maybe you are getting too high spam score so it's not even getting into the spam folder, this could be caused by not having correct dns configuration / keys and trying to use html at the same time.
I leave a link to the other answer (It's quite long, and not as easy as you would imagine xD):
PHP and Gmail spam filter
Hope it helps you.
You are not using a proper concatenation operator:
Change your From line to:
$headers .= "From: ".$from;
I'm trying to send a email to user after successfully register. So that following is my .php code.
$to = $email;
$subject = "Signup | Verification";
$message = "Congratulation $f_name $l_name you have been successfully registered. Please
click the link to active your account.\r\n";
$message .= "http://www.maaks.fr/hotel/verify.php?email=$email&hash=$hash\r\n";
$from = "noreply#yoursite.com";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-rype: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding; 7bit\r\n";
$headers = "From:" . $from . "\r\n";
$sentmail = mail($to,$subject,$message,$headers);
if($sentmail)
{
header("Location: thankyou.php?member=successfully");
exit();
}
It doesn't sent a email to the user. I checked it that if $sentmail is ok then It's go thankyou page. But interesting matter is that it's go to thankyou page without sent a email.
Is there anything i forgot?
The mail() function returns true if the message was accepted for delivery, it doesn't mean the message will be delivered.
From the Manual:
Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
Your message is probably being blocked by the recipients spam filter or going straight to the junk folder.
There is one minor typo in your email message but I don't think it will solve your problem.
You have a Content-rype specified, you should change that to Content-type...
But again I don't think that will solve your problem.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php mail function
I am doing this website where I send emails to users when they for example forget their password or similar, but for some reason I can not send emails with the following function:
$email = 'somemail#mail.com';
$subject = 'subject';
$message = 'message blablablablabla';
mail($email, $subject, $message);
Am I doing something wrong or missing something in the code, or is it the hosting company's fault? (I make my website on x10hosting.com). I checked in the manual about mail() but it didn't help me.
Thanks in advance.
Update
Thanks for the help guys, but it turned out to be a problem on the web hosting company I'm on. Everything's working fine now.
try to use with headersenter code here
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n";
$headers .= "From: <".$frommail.">\r\n";
$headers .= "Reply-To: ".$frommail."\r\n";
$mail_sent=mail($tomail, $msg, $headers);`enter code here`
I would guess either there is no sendmail_from value set in php.ini or your host does not support email or has not set it up correctly.
Try setting a from header, and if that doesn't work, contact your host:
mail($email, $subject, $message,'From: you#example.com');
is this on local host? or is it on a webserver
Also remember that mail($to, $subject, $contents) returns a boolean,
if(mail($to, $subject, $body){
echo "Message has been sent";
}
else{
echo "Error has occurred"
}