简体   繁体   中英

Can't send email with PHP Mail

I'm trying to send an email using php. I've the following only it doesn't seem to output anything to my browser nor send my email, can anybody see what i'm doing wrong?

    $to = "liam@mysite.co.uk";
$subject = "Alien Sighting";
$message = foreach ($_POST as $key=>$value) {
    $$key = $value;
}
$from = "Sighting@site.co.uk";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";

Can you give us $_POST content please ?

Does your PHP configuration display all errors messages? Make sure to use error_reporting (E_ALL) in your dev environnement to find any typo / error. That should help to get an output to your browser.

你必须一定要设置您的SMTP服务器发送,如果您运行的计算机上运行脚本,否则你可以得到一个测试服务器,所以你不必安装在SMTP它会自动发送电子邮件

Are u using Rmail.php? Do u uploaded your code to a webserver or is it on your local PC?

I am always using Rmail.php to send mails.

$mail = new Rmail();
$mail->setFrom(YOUR ADDRESS);
$mail->setSubject(YOUR SUBJECT);
$mail->setHTML($textSchueler);
$mail->send(array($address));

You'll also want to check and make sure your server is configured to send email at all. While most shared hosting accounts will be configured for you if you are setting up your own server or developing locally PHP may not have the any way to send the messages. Try checking to see if the mail() function returns true indicating the message was successfully sent and then look in the mail logs on the server to verify transmission.

In this kind of situation where PHP does not output anything to your browser (even it should), most likely the script fails before your echo statement.

Make sure you have not disabled error reporting. See PHP runtime configuration error_reporting and display_errors . You can change error reporting behaviour also runtime with error_reporting() function in the code. Enabling error reporting helps you to figure out what the problem might be.

Even you had disabled the error reporting on the browser, check out your webserver logs. They should have details what's the problem (syntax error in the script, failed mail delivery by mail function call... there are many possibilities).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM