【问题标题】:PHP eMailing scriptPHP 电子邮件脚本
【发布时间】:2012-10-16 14:57:59
【问题描述】:

所以我编写了一个 php 脚本,当用户忘记密码时,它会向用户发送一个临时密码,以便他们可以登录并更改密码。该脚本工作正常,并且发送的电子邮件包含所有正确的信息。我想要改变的是它是由谁发送的。我想为网站使用谷歌电子邮件应用程序来发送这些电子邮件,而不是由我的网络服务器发送电子邮件。我的脚本的发送部分如下所示:

$email_to = $_POST["email"];
$email_from = "Admin@domain.com";
$email_subject = "Account Information Recovery";
$email_message = "Here is your temporary password:\n\n";

$email_message .= "Password: ".$password."\n";
$email_message .= "\nPlease log into your account and immediately change your password.";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);

但是,当我收到电子邮件时,它来自Admin@webserver。如何使用 Google 的电子邮件应用发送这些电子邮件?

【问题讨论】:

  • 这是因为php的'mail'函数发送'MAIL FROM'作为服务器默认的邮件地址,设置header from address是不够的。
  • 我知道google-app-engine 是您输入google 时出现的第一件事,但实际上,请尝试适当地标记您的问题。
  • PHP 的 mail() 函数很烂。我建议避免它。改用像phpMailer 这样的体面的课程——你会为自己节省很多精力。
  • PHPMailer 不错,你也可以看看 SwiftMailer,它更强大。

标签: php html email user-accounts


【解决方案1】:

可能最好使用PHPMailer:

$mail = new PHPMailer(); 
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; //1 for debugging, spits info out  
$mail->SMTPAuth = true;  
$mail->SMTPSecure = 'ssl'; //needed for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465; 
$mail->Username = 'google_username';  
$mail->Password = 'google_password';           
$mail->SetFrom($email_from, 'Your Website Name');
$mail->Subject = $email_subject;
$mail->Body = $email_message;
$mail->AddAddress($email_to);
$mail->Send();

注意:此示例直接使用 SMTP 发送电子邮件,这将解决问题,但如果主机禁用了 fsockopen,它将无法正常工作。

【讨论】:

  • 所以我加入了phpmailer.inc.php 并且不得不做$mail = new phpmailer();。我使用的是正确的版本吗?另外,我在哪里可以查看调试输出以及如何检查 fsockopen?谢谢
  • 从这里下载最新版本:code.google.com/a/apache-extras.org/p/phpmailer
【解决方案2】:

我建议Swiftmailer。它有一个非常好用且文档齐全的 API,并且支持所有不同类型的传输。

来自文档:

require_once 'lib/swift_required.php';

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
  ->setUsername('your username')
  ->setPassword('your password')
  ;

/*
You could alternatively use a different transport such as Sendmail or Mail:

// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');

// Mail
$transport = Swift_MailTransport::newInstance();
*/

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('john@doe.com' => 'John Doe'))
  ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
  ->setBody('Here is the message itself')
  ;

// Send the message
$result = $mailer->send($message);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    • 2011-09-16
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多