1

所以我写了一个 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 的电子邮件应用程序发送这些电子邮件?

4

2 回答 2

2

可能最好使用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,它将无法正常工作。

于 2012-10-26T10:45:38.610 回答
1

我会建议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);
于 2012-10-26T13:26:53.100 回答