I encounter a very strange problem with ixwebhosting.
I am able to send email using the php mail() function with $subject = "test";
But if $subject = "testing of information send"; then i won't be able to receive any email
But actually "Mail sent!" was displayed in the php page.
if (!mail($email, $subject, $body, $from)) { echo "Error Sending Email!"; }
else
{ echo "Mail sent!"; }
Is it possible that the email is being classed as spam somewhere down the track? Are you able to get logs from the mailserver indicating whether mail gets sent?
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I am trying to get a contact form using the built in mail function to work but I'm running into some problems.
What I would like:
On clicking 'submit' the form is sent to defined emailadres.php.
If sent successfully, a confirmation message is displayed.
If not sent, an error message is displayed.
What is currently happening:
Confirmation message already shows on page load; regardless of form submission.
Mail is not sent.
Code I'm using:
if ($_POST["submit"]) {
mail ($to, $subject, $body, $from);
$sendErr = "Your message has been sent";
} else {
$sendErr = "Your message could not be sent";
}
I'm fairly new to all this so any help figuring out where my thinking stalls would be appreciated. If I need to post more parts of the form I will.
The code you are using does not even check if the mail was sent successfully, it only checks it the formular was submitted. mail() returns true if the mail was sent successfully, false if not. So you can check its return value:
if ($_POST["submit"]) {
$sent = mail ($to, $subject, $body, $from);
// Check here if the mail was sent or not
if ($sent) {
$sendErr = "Your message has been sent";
} else {
$sendErr = "Your message could not be sent";
}
}
I am trying to send an email to myself when an error happens from filling out the php form instead of doing Print="Sorry an error happened"
$error = "ERROR HAPPENED".$order."\r\n";
$error_email = mail($to, $subject, $error, $headers2);
$mail = #mail($email, $subject, $confirmation, $headers2);
if ($mail) {
header("Location: http://www.test.com" );
} else ($error_email);
You're not using brackets after the else.
if($mail)
{
header("Location: http://www.test.com");
}
else
{
mail($to, $subject, $error, $headers2);
echo $error;
}
And you were never using echo $error so the error couldn't be printed.
Your code is a little unclear - and it helps if you ask a question...
However, I'd suggest using a logging framework for this, such as log4php; this includes an email option, so errors can get mailed to you, or you can write them to disk, or the OS log file.
Please be aware that email is totally insecure - it's easy to intercept them, and they are not encrypted. I hope you're not sending sensitive data in the $order variable.
Finally, your code is probably not doing what you want it to.
$error = "ERROR HAPPENED".$order."\r\n";
Build the error string (assuming $order is nicely printable).
$error_email = mail($to, $subject, $error, $headers2);
Send an email to $to. $error_email is now a boolean value.
$mail = #mail($email, $subject, $confirmation, $headers2);
Send an email to $email. Suppress errors. $mail is a boolean.
if ($mail) {
header("Location: http://www.test.com" );
if the mail was sent successfully (to $email), redirect the user.
} else ($error_email);
If the mail was not sent successfully, not sure what you're trying to do...
It's not a good idea to tie your application logic to the random "did I manage to send an email" success.
Hi I was trying to send Email using php in xammp
I have already started Mercury Service
Heres my code
<?php
$to = "nikhil08514#gmail.com";
$subject = "Hi!";
$body="test";
$headers = "From: root#localhost.com";
if (mail($to, $subject, $body, $headers))
{
echo "Message successfully sent!";
}
else
{
echo "Message delivery failed...";
}
?>
when i execute code i am getting output as
Message successfully sent!
But when i check my mail box.I dont see mail.I checked all folders in my Mailbox but its not present
your code is right try that on server it will work surely, xampp does not send directly like that from xammp. by using smtp you can send.
I realize that this is similar to this question, however, I only get a double email when sending to myself. I have sent emails to others with the same script, and they get a single email. This is the email script:
<?php
function sendEmail( $recipient, $sub, $msg )
{
$to = $recipient;
$subject = $sub;
$message = $msg;
mail( $to, $subject, $message );
}
?>
The code calling this is this:
if( $retVal != FALSE ) // No errors in execution of report generation
{
$subject = "Successful Report";
$message = "The report was successfully generated.";
// Notify people about success
sendEmail( $mailto, $subject, $message );
echo "Successful report generation\n";
}
else // Error in report generation
{
$subject = "Unsuccessful Report";
$message = "The report failed to generate.";
// Notify people about failure
sendEmail( $mailto, $subject, $message );
echo "Report generation was unsuccessful\n";
}
where $retval is the return value of system(). Can anyone shed some light on this issue? Or is this something that can be overlooked?
Thanks very much
-rusty
Based on the conversation: You are the only recipient that is receiving double email, and the string "Successful report generation" only prints once. It sounds like PHP is not the culprit here, rather something outside of PHP, such as the MTA.
Take a look at the headers of the two email you receive, particularly the MessageID header. If they are identical, then a single email was sent from PHP (good!) and somewhere along the line it got delivered to you twice.
If they are not identical (messy), then PHP did in fact send out two emails (not likely) or there is a resender somewhere between PHP and your mail client.
In the later case, I would take a deeper look at the headers in your email, to determine the source and route of each email, as well as the envelope to determine where the MTA though it was sending mail to.
I would add the output from debug_backtrace() into the body of your email. This will allow you to determine when the sendEmail function is called and who called it.
$message = "";
$header = "Content-Type: text/html; charset=UTF-8;\r\n";
mail($mailto, $subject, $message, $header);
This is my Code :
<?php
//define the receiver of the email
$to = 'dannyfeher69#gmail.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent.
$message = "Hello World!\n\nThis is my mail.";
//define the headers we want passed.
$header = "From: me#localhost.com";
//send the email
$mail_sent = #mail( $to, $subject, $message);
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
-- it returns mail failed
Please help me
There are several reasons this could fail. The main obstacle to finding the cause is the use of the error control operator (#) in front of the call to the mail() function.
Other possible reasons are the lack of a valid From header. Although you define one in the $header variable, you don't pass it to the mail() function. It's also important that the From header is a valid email address on the domain you're sending the email from. If it isn't, most hosting companies will now reject the email as spam. You might also need to supply a fifth parameter to mail(), which normally consists of a string comprised of -f followed by a valid email address on the current domain.
Yet another possibility is that you are trying to send this from your own computer. The mail() function doesn't support SMTP authentication, so most mail servers will reject mail from sources they don't recognize.
And just to add to all your problems, newlines in emails must be a combination of carriage return followed by newline. In PHP, this is "\r\n", not "\n\n".
Assuming you're using a remote server to send the mail, the code should look something like this:
<?php
//define the receiver of the email
$to = 'dannyfeher69#gmail.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent.
$message = "Hello World!\r\nThis is my mail.";
//define the headers we want passed.
$header = "From: me#localhost.com"; // must be a genuine address
//send the email
$mail_sent = mail($to, $subject, $message, $header);
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>