php - 如何通过本地主机发送邮件?

标签 php email gmail

<?php
$to = "rajesh.bakade65@gmail.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <rajesh.bakade@chpl.tv>' . "\r\n";
//$headers .= 'Cc: myboss@example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>

我的 php.ini 文件是
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 25

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

请建议任何解决方案,以便我能够从本地主机发送邮件

谢谢

最佳答案

使用功能丰富的SwiftMailer你的设置应该是这样的:

username:google email addres
password: your google password
smtp: smtp.gmail.com
port: 587

Here is how you can set your credentials and send the email.
require_once 'lib/swift_required.php';

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

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

//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);

关于php - 如何通过本地主机发送邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3429605/

相关文章:

php - 当用户关闭他的浏览器时从 mysql 表中删除信息

php - Laravel 5 邮件队列不工作

powershell - 比较两个不同的CSV文件并发送电子邮件

email - appmaildev DKIM 测试失败 permerror(无 key )

javascript - 如何在 Google Apps 脚本中忽略自动回复指定的电子邮件

javascript - 如何刷新另一个页面内的页面?

php - 我应该指挥什么对象?

PHP:目录中的最后一个文件

javascript - 在javascript中获取gmail邮件正文时删除html格式

css - Gmail 如何隐藏他们的主题行溢出?