I'm tring to send an email via php
Here is the code:
<?php
//define the receiver of the email
$to = 'xxx#example.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. Each line should be separated with \n
$message = "Hello World!\n\nThis is my first mail.";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: xxx#example.com";
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
But the message is not received in my email.
It is not a good idea to suppress error messages using #, you need to see these errors.
If you are testing this locally you need a mail server to be running. I use Test Mail Server Tool, it's dead simple.
Also note that mail() returning true does not indicate that the mail was successfully sent, only that execution of the command didn't generate an error:
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.
Are you executing this script on your local machine, or on a webserver with SMTP availability? If it's on your own computer, you may not have a mail server it can use.
Consider using PHPMailer instead, it is far more capable than the native mail() function, and includes integrated SMTP support: https://github.com/PHPMailer/PHPMailer
This is my working code:
mail($address, $subject, $message, "From: Service <service#service.net>\n".
"MIME-Version: 1.0\n".
"Content-type: text/plain; charset=\"UTF-8\"\n");
Maybe the the correct carrige return in the headers is important
Related
I am making a system that will be used to collect data from users. I know some will be spam! I just want to know if the mail function returns false if the email is not correct?
Say I have.
<?php
$to = "fake#fake.com";
$header = "From: {$to}";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body, $header)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>
Which message will be printed if the email is not correct?
If it does return false I want a "flag" to be stored with it in that database so the admins can review it and delete it.
Not necessarily.
Per the manual, the mail() function will return true if the message is accepted for delivery - but doesn't guarantee that it will be delivered:
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.
All the server can tell you is the mail was accepted to be passed on or not. Unless you wrote an invalid email address or such to the point it was "no email" and so the server would reject it, then no, sadly, all it can say is "yes the mail was put in the electronic post" or "No it wasnt" it cant tell you if it gets there, read, or otherwise.
In the case you have outlined in your comment
fake#gmail.com
as far as the mail system is concerned this is a valid email address.
Message successfully sent!
will be displayed. The mail function makes no guarantee about getting to the recipient. And before you ask its not possible to check.
Hi guys i am using the mail() from the contact form and for some reason it is not working.
The php coding i have setup is as follows:
// sending email to sales#xxx.com
$to = "hello#xxx.com";
$subject = 'Email From {$name} from your website';
$message = "$name has contacted you from the website and says:
$mcontent
$name 's contact email address is: $email";
$headers = $email . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
mail($to, $subject, $message, $headers) or die ("Failure");
// end of sending mail
returnheader("/xxx/email-sent");
i get no errors at all, it even goes to the success page when completed.
Any ideas why this would not work.
I have checked your code and it is working fine on my server , i am getting e-mail.
Here , it looks like some problem with your SMTP server settings.
There is nothing wrong with your PHP script.
You can find your solution here.
php.ini & SMTP= - how do you pass username & password
Also in windows environment ,
http://www.ruhanirabin.com/php-sendmail-setup-with-smtp-iis-and-windows-servers/
If you are on WIndows, make sure you have an SMTP server in your
php.ini
If you are una Unix, make sure the MTA is running: If it is (at least partly) installed but not runnng, you will get exactly this effect
Edit
If your MTA is not running, and you start it, the mails sent with PHP will go out! They have beein queued, but not processed.
This is probably related to the mail setup. Do you have a mailserver running on the machine? Check sendmail / smtp_server settings in php.ini.
mail() uses the sendmail, by default: sendmail -t -i
It returns TRUE if the mail has been accepted, not if it has been sent:
Return Values
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 would suggest using http://swiftmailer.org/ with SMTP rather than mail().
The first line of $headers is invalid if $email is just an email address. Presumably you would want something like:
$headers = "Cc: " . $email . "\r\n";
or
$headers = "From: " . $email . "\r\n";
The cause can be for example, you don't have setup a mail server, or configuration of firewall if you has it.
You should use double quotes instead of single quotes in the variable $subject
$subject = "Email From {$name} from your website";
I am sending mails from php mail() : and I want to receive a failed message if sending is failed to the destinatio .
$to = 'itsdfdsf#7sisters.in';
$email_from = "info#7sisters.in";
$full_name = 'XXXX';
$from_mail = $full_name.'<'.$email_from.'>';
$subject = "testing sender name";
$message = "";
$message .= '
<p><strong>This is only a test mail. Please do not reply.</strong><br />
';
$from = $from_mail;
//$headers = "" .
// "Reply-To:" . $from . "\r\n" .
// "X-Mailer: PHP/" . phpversion();
$headers = "From:" . $from_mail . "\r\n" .
"Reply-To:" . $from_mail . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if(!mail($to,$subject,$message,$headers))
{
echo 'failed !!';
}
But although $to mail does no exist,it is not showing failed !!
The mail method is just sending the mail out. If it does not receive any errors (e.g. by not finding the server etc), it will return succesfull. You will not be able to know if the mail actually landed in the inbox of the recipient unless you create some code around bounced emails etc.
I think what you want is to check for a real email not only a valid formatted email. So I would suggest you to have a look at this blog
check the return from of mail
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.
Although the fact it is returning true probably means that your mail program is accepting the message but then failing when it tries to send to no one...
You should run the $to through a validator to check its a valid address and then throw an error if its not, don't rely on mail() to filter out things which you already know are wrong, or can check against easily.
--UPDATE
Then check out #SeRPRo , but what your trying to do is hard work to test programatically - its far easier and more reliable to send an e-mail which requires the user to click a link to verify that it's real than try querying SMTP servers which all have different behaviour (read: are broken to different degrees). Also note that your intended behaviour (code wise) is hard to differentiate from a spammers so don't be surprised to find it difficult going if you avoid the verification e-mail route.
But although $to mail does no exist,it is not showing failed !!
actually the fact that mail is being delivered to SMTP server, doesn't mean it will be delivered to the end user. There's no easy way in PHP to check whether it's delivered.
You could CC yourself as a way of testing that it is leaving the outbox.
In my case it helped to set the return-path via the commandline parameter "-f", which can be passed in the $additional_parameters parameter of mail(). so i call
mail($to, $subject, $message, $headers, "-f address.where.i.want.the.bounces#xy.com");
... according to some comments on http://www.php.net/manual/en/function.mail.php hosting-inviroments react different and have different restrictions (address might need to be registered in the same hosting-account, or be on the same domain, the same as the "From:" in the heade ... and so on)
The page where I got the bounces to be received (with non of the mentioned restrictions, as it seems) is hosted at Domainfactory http://www.df.eu
Use phpmailer to send email and set $mail->AddCustomHeader('Return-path:bounce#mail.com');
This will send bounce email at bounce#mail.com if recipient mail id does not exist or recipient does not receive email by any other case.
I have a problem on a server for which the additional headers get ignored.
Code to reproduce:
$toEmail="customer#domain.com";
$subject="subject";
$message="this is a message";
$fromEmail="Customer Service <customerservice#domain.net>";
$replyToEmail="Customer Service <customerservice#domain.net>";
mail("$toEmail", $subject, $message, "FROM: $fromEmail","-f$replyToEmail");
echo "mail sent";
The from address ends up being:
customerservice#domain.net.prod3.domain.net
prod3.domain.net is the name of the server, so it concatenates the from address and the machine name.
Server is LAMP running Centos5.
Thanks!
Cam
1) Please enclosed the email name with double quote, e.g. $fromEmail = "\"Customer Service\" "
2) Please change the "FROM" to "From". For example, "From: $fromEmail"
$subjt = $subject;
$message = $message;
$toinfo .= $snteadd[$i];
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: <$fromemailid>\r\n";
$headers .= "Cc: <$sendCC>\r\n";
$headers .= "Bcc: <$sendBCC>\r\n";
$headers .= "Return-Path: <$fromemailid>\r\n";
$headers .= "Errors-To: <$fromemailid>\r\n";
$headers .= "X-Mailer: PHP 4.x". phpversion()
#mail($snteadd[$i], $subjt, $message, $headers);
Return Values
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.
Then you can do:
if (#mail($snteadd[$i], $subjt, $message, $headers))
{
echo 'delivered to smtp';
}
else
{
echo 'not delivered to smtp';
}
If you want check mail reach the your sender, then probably better solution will be get link to your script with unique arguments to determinate mail id, and get information in mail text to click link.
The mail function returns true on success or false on failure.
if (!#mail($snteadd[$i], $subjt, $message, $headers))
{
// It failed...
See http://php.net/manual/en/function.mail.php
Check the return value of mail function which returns false when there it fails.
if(#mail($snteadd[$i], $subjt, $message, $headers) === false) {
// mail failed.
}
Just to expand on previous answers, the # is used to explicitly suppress error messages from the following function being displayed - so what your code does is send an email and DON'T TELL ME if it it fails... This is usually only good if you either a) don't care or b) are checking that it worked yourself - eg with an if block as shown elsewhere
While the mail() function returns true on success and false on failure, the status it reports only means "accepted for delivery by the local server", absolutely not "delivered".
The SMTP protocol used for most email guarantees nothing. It should try to deliver the e-mail, sometime; again, there's no guarantee that it will be in ten minutes, tomorrow, or this month (it happens). Even worse, there is no reliable way to tell whether a mail you've sent has actually been delivered to the intended recipient.
Even if the e-mail is delivered to the recipient, it may get shuffled to the spam bin, and the user won't see it.
In other words, the only thing that you can be sure of with e-mail is whether or not you've sent it; anything beyond that is uncertain. (see also this for other possible pitfalls)
In case someone else is wondering why the "Return-Path" header they are setting is not being honored, many php settings replace that header with something else, usually a default email address for the server (eg., something#webserver.domain.com). Use the (often overlooked) 5th parameter, $additional_parameters in this way:
mail($toMail, $subject, $message, $other_headers, "-fyour_return_path_email_address#whatever.com")
The your_return_path_email_address#whatever.com is the email you want the return path to be. The -f part that precedes this makes the "envelope sender" that email address.