简体   繁体   中英

PHP Mail Just works sometimes, others not?

So I'm working on a Wordpress installation for a buddy, making a form send to his email address. I've been testing out the mail function and.. Well, it seems like after a certain number of times I test it, it just stops working...

I've got an

if( mail( ... ) )
    echo " =) things are workin out all right...";
else
    echo "fuk...";

statement checking to see whether mail is sending.. and after a while it just stops working.

Is there a setting that limits the number of mails that can be set or something? Am I just sending too much mail?!

Now.. After I wait a while (say a day), mail is suddenly working again.. hm...

Some hosts limit how many messages can be sent per minute/hour/day.


To work around this, I set up a second Gmail account to send messages from a script using PHPMailer , then made this script (called mail.php ):

<?php
include_once 'phpmailer/class.phpmailer.php';
function do_mail($from, $name, $to, $subject, $message, $debug = false) {
    $blah = base64_decode('base64-encoded password here');
    $mail = new PHPMailer();
    $mail->IsSMTP();
    if($debug) $mail->SMTPDebug = 2;
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'tls';
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;
    $mail->Username = 'username@gmail.com';
    $mail->Password = $blah;
    $mail->SetFrom($from, $name);
    $mail->AddAddress($to, $to);
    $mail->Subject = $subject;
    $body = $message;
    $mail->MsgHTML($body);
    $mail->AltBody = $message;
    if($mail->Send()) {
        return true;
    } else {
        return $mail->ErrorInfo;
    }
}
?>

Then, to send a message:

<?php
include_once 'mail.php';
$result = do_mail('username@gmail.com', 'First Last', 'someone@example.com', 'Subject here', 'message here');
// Or, with debugging:
$result = do_mail('username@gmail.com', 'First Last', 'someone@example.com', 'Subject here', 'message here', true);
// Print the result
var_dump($result);
?>

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