0

我尝试通过这个 php 代码从 wampserver“localhost”发送电子邮件

<?php

$to      = 'test1@gmail.com'; 
$subject = 'Fake sendmail test'; 
$message = 'If we can read this, it means that our fake Sendmail setup works!'; 
$headers = 'From: test2@gmail.com' . "\r\n" . 
'Reply-To: eng.jirjawi@gmail.com' . "\r\n" . 
'X-Mailer: PHP/' . phpversion(); 

if(mail($to, $subject, $message, $headers)) { 
echo 'Email sent successfully!'; 
} else { 
die('Failure: Email was not sent!'); 
}

?>

我使用了文件“sendmail”并将其放在文件“C:\wamp\sendmail”中,我将设置 sendmail.ini 文件更改为 -->

smtp_server=smtp.gmail.com

smtp_port=465

smtp_ssl=ssl

default_domain=localhost

error_logfile=error.log

auth_username=test2@gmail.com
auth_password=xxxxxxx

pop3_server=
pop3_username=
pop3_password=

force_sender=

force_recipient=

hostname=localhost

我更改了 C:\wamp\bin\apache\apache2.2.22\bin 中的设置 php.ini 文件,并将 C:\wamp\bin\php\php5.4.3 中的设置 php.ini 文件更改为

[mail function]
; For Win32 only.
; http://php.net/smtp
;SMTP = 
; http://php.net/smtp-port
;smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = you@yourdomain

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = "C:\wamp\sendmail\sendmail.exe -t"

我更改了设置appach模块“SSL模块”检查

我更改了设置 php 扩展“php openssl”和“php sockets”

它不适用于代码没有发送电子邮件

在互联网上讨论了很多并尝试了很多方法为什么没有发生发射器以及正确的方法为错误道歉我的英语请帮助并谢谢

4

2 回答 2

2

PHPMailer一直为我在开发服务器 (localhost) 以及实时网站上工作。

在此处下载 PHPMailer ,解压缩并将其放在应用程序的目录中。

当您要发送邮件时,请使用以下代码:

require 'PHPMailerAutoload.php'; //Your path to PHPMailer's directory
$Mail = new PHPMailer();
$Mail->IsSMTP(); // Use SMTP
$Mail->Host        = "smtp.gmail.com"; // Sets SMTP server for gmail
$Mail->SMTPDebug   = 0; // 2 to enable SMTP debug information
$Mail->SMTPAuth    = TRUE; // enable SMTP authentication
$Mail->SMTPSecure  = "tls"; //Secure conection
$Mail->Port        = 587; // set the SMTP port to gmail's port
$Mail->Username    = 'yourusername@gmail.com'; // gmail account username
$Mail->Password    = 'yourpassword'; // gmail account password
$Mail->Priority    = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 =   low)
$Mail->CharSet     = 'UTF-8';
$Mail->Encoding    = '8bit';
$Mail->Subject     = 'Mail test';
$Mail->ContentType = 'text/html; charset=utf-8\r\n';
$Mail->From        = 'test2@gmail.com'; //Your email adress (Gmail overwrites it anyway)
$Mail->FromName    = 'Test';
$Mail->WordWrap    = 900; // RFC 2822 Compliant for Max 998 characters per line

$Mail->addAddress('test1@gmail.com'); // To: test1@gmail.com
$Mail->isHTML( TRUE );
$Mail->Body    = '<b>This is a test mail</b>';
$Mail->AltBody = 'This is a test mail';
$Mail->Send();
$Mail->SmtpClose();

if(!$Mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $Mail->ErrorInfo;
exit;
}

echo 'Message has been sent';

更新:您应该更新您的 PHP 安装以使其正常工作。对于您遇到的错误,请尝试以下操作:

在您的 php.ini 中搜索行

; extension=php_openssl.dll

并删除;所以它变成:

extension=php_openssl.dll
于 2014-05-08T03:40:56.950 回答
0

salem alaykùm(嘿)检查一下它对我有用,

<?php

if ($_SERVER["REQUEST_METHOD"] == 'POST'){
    //print_r($_POST);
    if (mail('name@example.com', 'New Test Email', htmlspecialchars($_POST['msg']))){
        $cas = "thanks for the msg";
    }

 } 

?>

<!doctype html>
<html>
<head> <title>my test company</title>
</head>
<body>
<h3>Contactez nous</h3>
<form action="" method="post">
    <ul>
        <li>
            <label for="name">Name:</label>
            <input type="text" name="name" id="name">
        </li>
        <li>
            <label for="email">Email :</label>
            <input type="text" name="email" id="email">
        </li>
        <li>
            <label for="msg">Message:</label><br />
            <textarea name="msg" id="msg"></textarea>
        </li>

        <li>
            <input type="submit" value="Go!">
        </li>
    </ul>

</form>
<?php 
if (isset($cas)) echo $cas; 
?>
</body>
</html>
于 2014-05-08T18:25:57.357 回答