我正在使用WAMP服务器2.4。我想向我的 Gmail 帐户发送邮件。下面给出了我用于相同的代码。我阅读了一些论坛,他们建议我在php.ini文件中进行更改。但这不是一个永久的解决方案。也有人建议mail()不适用于gmail?对此有解决方案吗?
<?php
//Checking if entries are ok
if(isset($_POST['submit']))
{
if(isset($_POST['username']))
$id = $_POST['username'];
else
echo "Cannot be blank";
//ensuring mail goes to registered user
$query="SELECT * FROM table1 WHERE id = '$id' ";
$result= mysql_query($query,$con);
if (!($result) )
{
die('Error: ' . mysql_error($con));
}
else
{
$values = mysql_fetch_array($result);
}
// Sending mail...
if(mysql_num_rows($result)== 1)
{
if(isset($_POST['email']))
$to= $_POST['email'];
else
echo "invalid email";
$msg = 'Name :' .$values['name'] ."'n"
.'Id:' .$values['id']."'n"
.'Email:' .$values['email']."'n"
."Your password is:" . $values[password];
mail($to,"Forget your Password",$msg);
header('location: sent_mail.php');
}
else
echo "Verify your username again";
}
else{
echo "sending failed";
header('location: forget_password.php');
exit(0);
}
您可以使用 PHPMailer 或 PHPMimeMail 将邮件从本地主机发送到 gmail。要向 Gmail 发送电子邮件,您应该像发送 SMTP 邮件一样发送经过身份验证的邮件。您应该在邮件配置中配置您的邮件用户名、密码和邮件主机。
适用于Gmail的PHPmailer脚本示例:
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = preg_replace('/[']/','',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "mail@gmail.com"; // GMAIL username
$mail->Password = "12345"; // GMAIL password
$mail->SetFrom('mail@gmail.com', 'Balaji K');
$mail->AddReplyTo("mail2@gmail.com");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->Body($body);
$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
伙计们。经过几个小时的尝试,我发现:你不需要额外的工具,如邮件服务器或梨子添加。这些只会提供您无法控制或洞察的其他潜在安全漏洞。要使邮件功能在本地主机上工作,您需要做的就是更改 php.ini 文件。我刚刚检查了我的 Outlook 帐户设置并将其复制到 php.ini 文件中。所以SMTP服务器,端口,用户名和密码。
现在,您可能认为它不起作用,但您应该知道,如果"发件人"字段的域与接收电子邮件的实际域不同,则许多邮件客户端会拒绝电子邮件。因此,如果 php.ini 文件包含例如 smtp.ziggo.nl 确保标头包含:发件人:info@ziggo.nl
因此,为了在本地主机和远程主机上制作通用代码,我检查是否存在我只有本地的文件(例如 z_local),并相应地设置标头。如果本地文件不存在,我必须在远程ISP上并选择标题"发件人:info@remotesite.com"