如何使用PHP脚本发送电子邮件

时间:2014-10-08 05:03:56

标签: php

我正在尝试使用php脚本发送电子邮件,但它不起作用,你可以建议我错误

以下代码

<?php
try
 {
     $mail="atul@divyampg.0fess.us";
     $contents="message";
     $emailto1="atulkumaronline@gmail.com";
     $subject="testing";
     $headers="adesh";
     mail($emailto1, $subject, $contents, $headers);
     echo "mail send";
}
catch(Exception $e) {
     echo 'Message: ' .$e->getMessage();
}
?>

2 个答案:

答案 0 :(得分:1)

http://php.net/manual/en/function.mail.php。 &#34; adesh&#34;不是标题。

有效标题: -

'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

此外,邮件将返回bool值。您可以这样实现: -

$contents="message";
$emailto1="atulkumaronline@gmail.com";
$subject="testing";
if(mail($emailto1, $subject, $contents))
    echo "Sent";
else
    echo "Error sending Email";

答案 1 :(得分:0)

试试这个。

。                 

            if(isset($_POST['send']))
            {
                 $name     = $_POST['name'];
                 $email    = $_POST['email']; //'email' must be the same as name="email" in the input
                 $subject  = $_POST['subject'];
                 $message  = $_POST['message'];
                 $spamcheck = $_POST['spamcheck'];

                if(trim($name) == '')
                {
                    $error = '<div class="error">Please enter your name!</div>';
                }
                  else if(trim($email) == '')
                {
                    $error = '<div class="error">Please enter your email address!</div>';
                }
                else if(!isEmail($email))
                {
                    $error = '<div class="error">You have enter an invalid e-mail address. Please, try again!</div>';
                }
                  else if(trim($subject) == '')
                {
                    $error = '<div class="error">Please enter a subject!</div>';
                }
                  else if(trim($message) == '')
                {
                    $error = '<div class="error">Please enter your message!</div>';
                }
                  else if(trim($spamcheck) == '')
                {
                    $error = '<div class="error">Please enter the number for Spam Check!</div>';
                }
                  else if(trim($spamcheck) != '5')
                {
                    $error = '<div class="error">Spam Check: The number you entered is not correct! 2 + 3 = ????</div>';
                }
                if($error == '')
                {
                    if(get_magic_quotes_gpc())
                    {
                        $message = stripslashes($message);
                    }

                    // make sure to change the email address below or you will nver receive mails
                    // the email will be sent to:
                    $to      = "henry4u.u4edozie@gmail.com";

                    // the email subject
                    // '[Contact Form] :' will appear automatically in the subject.
                    // You can change the text if you wish.
                    $subject = '[Contact Form] : ' . $subject;

                    // the mail message ( add any additional information if you want )
                    $msg     = "$message";

                    //Extras: User info (Optional!)
                    //Delete this part if you don't need it
                    //Display user information such as Ip address and browsers information...
                    $msg .= " \r\n\n---User information--- \r\n"; //Title
                    $msg .= "User IP : ".$_SERVER["REMOTE_ADDR"]."\r\n"; //Sender's IP
                    $msg .= "Browser info : ".$_SERVER["HTTP_USER_AGENT"]."\r\n"; //User agent
                    $msg .= "User come from : ".$_SERVER["HTTP_REFERER"]; //Referrer
                    // END Extras

                    mail($to, $subject, $msg, "From: $name <$email>\r\nReply-To: $name <$email>\r\nReturn-Path: $email\r\n");
            ?>

                <!-- Message sent! (change the text below as you wish)-->
                <h1>Congratulations!!</h1>
                   <p>Thank you <b><?=$name;?></b>, your message is sent! We will get back to you as soon as possible.</p>
                   <p>Return to the <a href="index.html">home page</a> or use the navigation above.</p>
                <!--End Message Sent-->

        <?php
            }
        }
        if(!isset($_POST['send']) || $error != '')
        {
        ?>