如何使用LAMPP,PHP和Sendmail / Postfix从gmail帐户向其他帐户发送电子邮件?

时间:2018-03-25 17:20:02

标签: php email sendmail postfix lampp

在Windows上试用时,它非常简单。我在PHP文件的正文中有这段代码:

$to = "synewaveltd@gmail.com";
$headers = 'From: synewavecomplaints@gmail.com' . "\r\n" .
    'Reply-To: ' . $_POST['email'] . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
$fullText = "Complaint from : " . $_POST['email'] . "\r\n" . "Name : " .
    $_POST['fullName'] . "\r\n" . $_POST['mainText'] . "\r\n";
mail($to, $_POST['subject'], $fullText, $headers);

我有这个sendmail.ini文件:

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=synewavecomplaints@gmail.com
auth_password=XXXXXXXXXX
force_sender=synewavecomplaints@gmail.com

基本上,我想要做的是从名为synewavecomplaints@gmail.com的帐户发送一封电子邮件到另一个名为synewaveltd@gmail.com的帐户,其中第一封电子邮件有密码。在Windows上,这可行。

现在在Linux上,LAMPP没有sendmail.ini文件。我一直在各处浏览,我可以解决这个问题,比如this,但无论我尝试什么,都行不通。我甚至尝试使用here中的步骤来使用Postfix,但它也没有用。

我意识到这些指南可能因为它们的年龄而错,但我找不到最近的指南来解决这个问题。谁能告诉我如何在linux上实现这一目标呢?

1 个答案:

答案 0 :(得分:0)

I couldn't find a way using Postfix or Sendmail, but I DID find a way using SSMTP.

First install SSMTP: sudo apt-get install ssmtp on ubuntu.

Then, change the configuration files as such:

/etc/ssmtp/ssmtp.conf:

root=username@gmail.com
mailhub=smtp.gmail.com:587
rewriteDomain=gmail.com
hostname=username
UseSTARTTLS=YES 
AuthUser=username@gmail.com
AuthPass=password
FromLineOverride=YES

/etc/ssmtp/revaliases:

root:username@gmail.com:smtp.gmail.com:587
localusername:username@gmail.com:smtp.gmail.com:587

php.ini [the relevant sendmail part anyway]:

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

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = /usr/sbin/ssmtp -t

After doing all of this, everything worked perfectly.