[英]php mail and SMTP while using cloudflare
我在我的網站上使用cloudflare,我想保持我的服務器的IP(ORIGIN IP)私有,以避免DDoS攻擊被直接發送到我的服務器的IP。 我的服務器使用Apache,PHP,MySQL。
當使用php郵件發送電子郵件時(即使我使用phpmailer庫通過外部SMTP發送電子郵件),我的服務器的IP也被添加到郵件頭中。 它發生在谷歌SMTP,Mailgun和其他人,因為他們的政策可能是在標題中寫入郵件所來自的IP。
目前,我想到的唯一解決方案需要付出很多努力,即創建自己的REST API並通過其他服務器發送電子郵件,如下所示:
ORIGIN SERVER IP通過我的REST API以文本格式將電子郵件數據發送到MY MAIL SERVER IP,然后MY MAIL SERVER IP使用帶有phpmailer的php郵件功能通過SMTP將電子郵件發送給用戶。 這樣,MY MAIL SERVER的IP將出現在電子郵件標題中,而不是ORIGIN SERVER的IP中。
有沒有更優雅的方式來做到這一點? 是否有提供rest API的郵件服務,如果我使用他們的API,他們將不會在電子郵件標題中顯示我的服務器的IP? 或者可能有一個已經開發的REST API /庫用於按照我的要求遠程發送電子郵件,所以我不必從頭開始開發和測試我自己的?
您應該通過他們的API而不是SMTP協議通過mailgun(或sendgrid,或jetmail,或SES,或......)發送電子郵件,您的IP將不會被披露。
例如,對於Mailgun SDK: https : //github.com/mailgun/mailgun-php
$mg = Mailgun::create('key-example');
# Now, compose and send your message.
# $mg->messages()->send($domain, $params);
$mg->messages()->send('example.com', [
'from' => 'bob@example.com',
'to' => 'sally@example.com',
'subject' => 'The PHP SDK is awesome!',
'text' => 'It is so simple to send a message.'
]);
但是大多數提供商都有SDK:
此外, 我建議使用SwiftMailer這是一個功能強大的庫來處理電子郵件。 其中一個很酷的事情是它抽象了傳輸,你可以使用包從SMTP或任何提供者API切換。
您可以使用例如mailchimp,amazon SES或其他郵件服務提供商,他們不應該添加您的IP。 但是這項服務是有償的。
很久以前,在大學時,由於防火牆規則,我不能使用php mail命令,所以編寫我自己的SMTP autentication類。 一段時間后,我開始使用PHPMailer類,我從未遇到過其他問題,即使使用Gmail作為發件人。 請查看https://github.com/PHPMailer/PHPMailer 。
這是一個簡單的例子:
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
從任何雲提供商處獲取實例,向該實例發送REST請求或您喜歡的任何內容,然后您的原始Web服務器的IP將完全不可見。
沒有API或優雅的方法來隱藏您發送的電子郵件中的IP。 任何提供此功能的SMTP提供商都值得列入黑名單,並且會立即被垃圾郵件發送者注冊以濫用此隱私權。
您必須使用創建內部Web中繼系統的想法,才能在啟動SMTP之前發送到其他IP。 但是設置它的麻煩應該比用另一個IP重建當前站點更麻煩。
這聽起來像是一個經典的案例,將您的服務器視為寵物,而不是像牛一樣。 如果一個新的IP重建當前站點比建立和維護一個自定義Web API隱藏暴露自己的IP 的吸引力 ,你需要調查的自動化工具。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.