繁体   English   中英

PHP邮件有时不发送

[英]Php mail not Send sometimes

这是我的PHP代码,用于发送电子邮件。

  <?php

       class mailer {
       public function send_request_mail($to, $msg) {
       $from="abcd@xyz.com";
       $headers = 'MIME-Version: 1.0' . "\r\n".'Content-type: text/html; charset=iso-8859-1' . "\r\n".'From: ' . $from . "\r\n" . 'Reply-To: ' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion ();
       $message = "ip 192.168.0.9:9035";
       $subject = "subject";
       mail ( $to, $subject, $message, $headers );

             }
       }

$mail=new mailer();

$mail->send_request_mail("abcd@xyz.com", "msg");
?>

有时它的工作原理(对于某些消息)。当我尝试发送上述IP地址时,它失败了。帮助我

希望你一切顺利。

必须在php.ini文件中正确配置PHP,并详细说明系统如何发送电子邮件。 打开/ etc /目录中的php.ini文件,并找到标题为[邮件功能]的部分。

Windows用户应确保提供了两个指令。 第一个称为SMTP,它定义您的电子邮件服务器地址。 第二个称为sendmail_from,它定义您自己的电子邮件地址。

Windows的配置应如下所示:

[mail function]
; For Win32 only.
SMTP = smtp.secureserver.net

; For win32 only
sendmail_from = webmaster@tutorialspoint.com

Linux用户只需要让PHP知道他们sendmail应用程序的位置即可。 路径和任何所需的开关应指定给sendmail_path指令。

Linux的配置应如下所示:

[mail function]
; For Win32 only.
SMTP = 

; For win32 only
sendmail_from = 

; For Unix only
sendmail_path = /usr/sbin/sendmail -t -i

PHP利用mail()函数发送电子邮件。 此功能需要三个必填参数,用于指定收件人的电子邮件地址,邮件主题和实际邮件,此外还有其他两个可选参数。

mail( to, subject, message, headers, parameters );

例:

以下示例将向xyz@somedomain.com发送HTML电子邮件,并将其复制到afgh@somedomain.com。 您可以以这种方式对程序进行编码,使其应从用户那里接收所有内容,然后应发送电子邮件。

<html>
<head>
<title>Sending HTML email using PHP</title>
</head>
<body>
<?php
   $to = "xyz@somedomain.com";
   $subject = "This is subject";
   $message = "<b>This is HTML message.</b>";
   $message .= "<h1>This is headline.</h1>";
   $header = "From:abc@somedomain.com \r\n";
   $header = "Cc:afgh@somedomain.com \r\n";
   $header .= "MIME-Version: 1.0\r\n";
   $header .= "Content-type: text/html\r\n";
   $retval = mail ($to,$subject,$message,$header);
   if( $retval == true )
   {
      echo "Message sent successfully...";
   }
   else
   {
      echo "Message could not be sent...";
   }
?>
</body>
</html>

希望这对您有用! 干杯!

等待您的正面评价!

您可能要使用PHPMailer之类的东西,因为当您使用mail()函数时,您的mail()很有可能最终会成为垃圾邮件或垃圾箱。

这是使用PHPMailer的示例代码,

    <?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->From = 'abcd@xyz.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'ip 192.168.0.9:9035';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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