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"
}
Related
This might sound similar like previously asked questions but trust me it's not
I Was trying to send an email that uses an HTML template via PHP mail() function from Localhost and a Hostinger Server but they created different problems.
On localhost the email was being sent as plain text although there were headers
$headers =
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=UTF-8";
I have gone through all the similar questions in stackoverflow and tried each and every thing but I couldn't make it work. After some more research on this I found out this
I assume, that your email client is considering the smtp-server "unsafe", and hence is just going to display all the html as plaintext, rather than rendering it
Therefore I switched over my hosting and tried to do the same but this time I found that the headers are causing the problem. The email is not sent if the header variable is passed in the mail() function. I tried to concatenate the headers which didn't worked. Then I made an array of headers and joined them with php implode which too didn't worked. On a similar question on stackoverflow I found that webmails mess up if html, head, body tags are used as they use xhtml. I removed them and still no success.
I tried error reporting too and it showed module sqlite3 already loaded which I think is not related to mail.
Below is my code
php
<?php
$email_template = file_get_contents("path/to/my/template");
$lucky_number = rand(999999, 111111);
$email_template = str_replace("{{user}}", "User", $email_template);
$email_template = str_replace("{{lucky_number}}", $lucky_number, $email_template);
$sender = "from:iusername#host.com"; // I found that if I dont use from, my mail ends up in spam folder
$receiver = "username#host.com";
$subject = "Random Subject Name";
$headers =
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=UTF-8";
if(mail($receiver, $subject, $email_template, $sender, $headers))
{
echo "Email Sent Successfully";
}
else
{
echo "Email Sending Failed";
}
P.S I can't use PHPMailer or other similar libraries
The sender information should be inside the headers
Hence, please change the following lines:
$headers =
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=UTF-8";
if(mail($receiver, $subject, $email_template, $sender, $headers))
to
$sender = "iusername#host.com";
$headers = "From: $sender <$sender>\r\nReply-To: $sender\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
if(mail($receiver, $subject, $email_template, $headers))
Well I am using the PHP MAIL function and for some reason every email it sends has a weird;
This is at the end of any email as I said, I am not quite sure why this is happening.
$from = 'From: support#phycraft.co.uk';
$to = $user_email; // Send email to our user
$subject = 'PhyCraft Support Ticket :: Closed :: ' . $t_subject; // Give the email a subject
$message = '
Hello '. $username.'.
Your support ticket '.$t_subject.' has been closed due to being inactive for 48 hours.
If you still need help with the ticket please reopen the ticket by replying to it.
~PhyCraft
';
$headers = 'From:support#phycraft.co.uk' . "\r\n"; // Set from headers
mail($to, $subject, $message, $from); // Send our email
I can't see what in the code woud make that appear to be honest.
Most issues with php's mail() function are problems with the MTA and not with PHP itself. I've never heard of this before making it even more likely it's a MTA issue.
You've not provided any useful information beyond the PHP code. What OS is this on (mail() on MSWindows is very different from everyhere else). Do you control the server? What MTA is configured? Have you tried sending an email from the command line?
The extra stuff at the end looks like HTML - is this byte for byte what's in the email or what you see in your mail client?
BTW it's not a good idea to explicitly put "\r\n" at the end of your headers - but you seem to have forgotten to add them as a parameter. Also, your missing a space between "From:" and the email address.
Can you try it with following $headers ( only \n ).
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/plain; charset = \"ISO-8859-1\";\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "From: support#phycraft.co.uk\n";
$headers .= "\n";
mail($to, $subject, $message, $headers);
and 2. try it without
$headers .= "Content-Transfer-Encoding: 8bit\n";
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
Using the php mail() function to send out emails when a user requests a login.
It was working fine last week, emails were being received by all my coworkers who share the same mail server for our company. Now however, the emails are not being received by that mail server, but received on others (comcast.net, uservoice.com, gmail.com, etc.) just fine.
No error from the php mail() function so the emails are being sent, just for whatever reason they are all of a sudden blocked by our mail server.
No settings have been changed to the php scripts or the mail server.
Any ideas??? I have tried everything!
<?PHP
$timestamp = date('Y-m-d H:i:s');
$to = 'james.hickman#MYCOMPANY.com';
$from = 'support#MYCOMPANY.uservoice.com';
$subject = 'Admin Test';
$message = 'Just a simple test message! - '.$timestamp;
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "Return-Path: ".$from."\r\n";
$headers .= "From: ".$from."\r\n";
$headers .= "Organization: MYCOMPANY Support\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n" ;
if(mail($to, $subject, $message, $headers, "-f ".$from))
echo "Success!";
else
echo "Failed";
?>
If you are able to send mail out to other services and just not your own server then the problem is not with php's mail function. Php's mail function just sends out mail and you yourself said that u are able to send mails to other hosts. So the problem is not as devious as it sounds. Try and check if your server is out of disk-space.
I'm trying to use BCC fields in some PHP email. My code is as below:
$to = "myemail#email.com";
$subject = 'Subject here';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "From: <myemail#email.com>\r\n";
$headers .= "Bcc: <myemail#email.com>\r\n";
//send the email
if (!mail($to, $subject, 'body', $headers)){
echo "Error";
}
What I've discovered is that if I include the bcc field in my headers the script just errors and the email is not sent. I've looked everywhere for the syntax and my understanding is that what I have is correct, but clearly something is wrong! Thanks
CC and BCC headers when using php's mail() function are blocked by the suhosin security module.
I found this in: http://forums.asmallorange.com/topic/11501-php-mail-cc-bcc-headers/
I cannot reproduce it, works for me
http://sandbox.phpcode.eu/g/12908.php
check your mailserver
What line is the error on. Maybe it's good to use all double quotes or all single quotes also, and not mixed up. You can also remove the concat dot in the MIME line.