我一直在尝试通过PHP发送邮件,在阅读了tutos和论坛之后,我仍然做不到,这是我的配置以及我尝试过的操作:
centos 7
php 7.4.6
php.ini mail():
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = test.exemple@gmail.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = /usr/sbin/sendmail -t -i
sendmail_from = me@myserver.com
我已经安装了sendmail,我可以这样通过控制台发送邮件:
mail ('mon.mail@gmail.com', "Test email", "test simple", null, "-f autre.mail@gmail.com");
这是邮件日志
[root@dockertest /]# tail /var/log/maillog
Aug 12 15:42:08 dockertest sendmail[84407]: 07CDg8xw084407: from=root, size=99, class=0, nrcpts=1, msgid=<202008121342.07CDg8xw084407@dockertest.cable.mr.local>, relay=root@localhost
Aug 12 15:42:08 dockertest postfix/smtpd[84408]: connect from localhost[127.0.0.1]
Aug 12 15:42:08 dockertest postfix/smtpd[84408]: 89054859389: client=localhost[127.0.0.1]
Aug 12 15:42:08 dockertest postfix/cleanup[84411]: 89054859389: message-id=<202008121342.07CDg8xw084407@dockertest.cable.mr.local>
Aug 12 15:42:08 dockertest sendmail[84407]: 07CDg8xw084407: to=mon.mail.com, ctladdr=root (0/0), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30099, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (Ok: queued as 89054859389)
Aug 12 15:42:08 dockertest postfix/qmgr[1583]: 89054859389: from=<root@dockertest.cable.mr.local>, size=601, nrcpt=1 (queue active)
Aug 12 15:42:08 dockertest postfix/smtpd[84408]: disconnect from localhost[127.0.0.1]
Aug 12 15:42:08 dockertest postfix/smtp[84412]: connect to gmail-smtp-in.l.google.com[2a00:1450:400c:c0c::1b]:25: Network is unreachable
Aug 12 15:42:09 dockertest postfix/smtp[84412]: 89054859389: to=<mon.mail.com>, relay=gmail-smtp-in.l.google.com[108.177.15.26]:25, delay=0.82, delays=0.08/0.02/0.22/0.49, dsn=2.0.0, status=sent (250 2.0.0 OK 1597239729 k17si1693312wrd.443 - gsmtp)
Aug 12 15:42:09 dockertest postfix/qmgr[1583]: 89054859389: removed
我测试了一些代码,它们没有返回任何错误,但是没有用
<?php
// the message
$msg = "Line of text";
// send email
if(!mail("mon.mail@gmail.com","My subject",$msg)){
var_dump(error_get_last()['message']);
}
?>
和另一个带有标题的
<?php
$to = "mon.mail@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=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>
我不知道我的配置或代码是否错误,请告诉我是否需要更多信息。