通过 SMTP 发送电子邮件而不更改发件人的域?

发布于 2024-12-11 14:46:08 字数 3818 浏览 0 评论 0原文

我编写了一个小脚本,用于向大约 200 位客户发送电子邮件。问题是该脚本在一个小型托管站点上运行,我需要使用我公司的电子邮件地址作为发件人。这很好,除非我的大多数(如果不是全部)客户都有强大的垃圾邮件过滤器,如果发送域与发件人的电子邮件地址域不同(也就是说,我将脚本托管在“example.com”),它们会阻止我的消息。 com',但我的电子邮件是 '[电子邮件受保护]'。

我尝试过 SMTP'ng 到 Gmail 帐户并以这种方式发送(作为从公司电子邮件发送之前的测试),但它似乎不起作用,它仍然有我的域的所有标题

。我做错了什么?还有其他方法吗?

我正在使用的 SMTP 代码:

if (!$this->valid_mail_adresses) return; 

//connect to the host and port
$smtpConnect = fsockopen($this->smtpServer, $this->port, $errno, $errstr, $this->timeout);
$smtpResponse = fgets($smtpConnect, 4096);
if(empty($smtpConnect))
{
    $this->msg[] = "Failed to connect: $smtpResponse";
    var_dump($this->msg);
    return;
}
else
{
    $logArray['connection'] = "<p>Connected to: $smtpResponse";
    echo "<p />connection accepted<br>".$smtpResponse."<p />Continuing<p />";
}

//you have to say HELO again after TLS is started
fputs($smtpConnect, "HELO $localhost". $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['heloresponse2'] = "$smtpResponse";

//request for auth login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authrequest'] = "$smtpResponse";

//send the username
fputs($smtpConnect, base64_encode($this->username) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authusername'] = "$smtpResponse";

//send the password
fputs($smtpConnect, base64_encode($this->password) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authpassword'] = "$smtpResponse";

//email from
fputs($smtpConnect, "MAIL FROM: <{$this->from_mail}>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailfromresponse'] = "$smtpResponse";

//email to
fputs($smtpConnect, "RCPT TO: <$this->mail_to>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailtoresponse'] = "$smtpResponse";

//the email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data1response'] = "$smtpResponse";


$mail_message = $this->mail_body; 
if (count($this->att_files) > 0)
{ 
    foreach ($this->att_files as $val)
        $mail_message .= $val; 
    $mail_message .= "--".$this->uid."--"; 
} 
    if (mail($this->mail_to, $this->mail_subject, $mail_message, $this->mail_headers)) { 
        $this->msg[] = "Your mail is succesfully submitted."; 
} else
    $this->msg[] = "Error while sending you mail."; 


//construct headers
//$headers = "MIME-Version: 1.0" . $newLine;
//$headers .= "Content-type: multipart/mixed; boundary=\"{$this->uid}\" charset=iso-8859-1" . $newLine;
$headers .= $this->mail_headers;
//$headers .= "To: {$this->to_name} <{$this->mail_to}>" . $newLine;
//$headers .= "From: {$this->name_from} <$from>" . $newLine;

$message = "To: {$this->mail_to}\r\nFrom: {$this->from_mail}\r\nSubject: {$this->mail_subject}\r\n$headers\r\n\r\n{$this->mail_body}\r\n";
if (count($this->att_files) > 0)
{ 
    foreach ($this->att_files as $val)
        $message .= $val; 
    $message .= "--".$this->uid."--"; 
} 
echo $message;
print_r($logArray);
//observe the . after the newline, it signals the end of message
//fputs($smtpConnect, $message);
//$smtpResponse = fgets($smtpConnect, 4096);
//$logArray['data2response'] = "$smtpResponse";

// say goodbye
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['quitresponse'] = "$smtpResponse";
$logArray['quitcode'] = substr($smtpResponse,0,3);
fclose($smtpConnect);
//a return value of 221 in $retVal["quitcode"] is a success
return($logArray);

I have a small script that I wrote to send an email out to about 200 customers. The issue is that the script is running on a small hosting site, and I need to use my companies email address as the sender. This would be fine, except most if not all of my customers have robust spam filters that will block my message if the sending domain isn't the same as the sender's email address domain (aka, I'm hosting the script at 'example.com', but my email is '[email protected]'.

I've tried SMTP'ng into a gmail account and sending that way (as a test before I send it from the company email), but it doesn't seem to work, it still has all the headers of my domain.

Any suggestions on what I'm doing wrong? Is there another way to do this?

The SMTP code I'm using:

if (!$this->valid_mail_adresses) return; 

//connect to the host and port
$smtpConnect = fsockopen($this->smtpServer, $this->port, $errno, $errstr, $this->timeout);
$smtpResponse = fgets($smtpConnect, 4096);
if(empty($smtpConnect))
{
    $this->msg[] = "Failed to connect: $smtpResponse";
    var_dump($this->msg);
    return;
}
else
{
    $logArray['connection'] = "<p>Connected to: $smtpResponse";
    echo "<p />connection accepted<br>".$smtpResponse."<p />Continuing<p />";
}

//you have to say HELO again after TLS is started
fputs($smtpConnect, "HELO $localhost". $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['heloresponse2'] = "$smtpResponse";

//request for auth login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authrequest'] = "$smtpResponse";

//send the username
fputs($smtpConnect, base64_encode($this->username) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authusername'] = "$smtpResponse";

//send the password
fputs($smtpConnect, base64_encode($this->password) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authpassword'] = "$smtpResponse";

//email from
fputs($smtpConnect, "MAIL FROM: <{$this->from_mail}>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailfromresponse'] = "$smtpResponse";

//email to
fputs($smtpConnect, "RCPT TO: <$this->mail_to>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailtoresponse'] = "$smtpResponse";

//the email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data1response'] = "$smtpResponse";


$mail_message = $this->mail_body; 
if (count($this->att_files) > 0)
{ 
    foreach ($this->att_files as $val)
        $mail_message .= $val; 
    $mail_message .= "--".$this->uid."--"; 
} 
    if (mail($this->mail_to, $this->mail_subject, $mail_message, $this->mail_headers)) { 
        $this->msg[] = "Your mail is succesfully submitted."; 
} else
    $this->msg[] = "Error while sending you mail."; 


//construct headers
//$headers = "MIME-Version: 1.0" . $newLine;
//$headers .= "Content-type: multipart/mixed; boundary=\"{$this->uid}\" charset=iso-8859-1" . $newLine;
$headers .= $this->mail_headers;
//$headers .= "To: {$this->to_name} <{$this->mail_to}>" . $newLine;
//$headers .= "From: {$this->name_from} <$from>" . $newLine;

$message = "To: {$this->mail_to}\r\nFrom: {$this->from_mail}\r\nSubject: {$this->mail_subject}\r\n$headers\r\n\r\n{$this->mail_body}\r\n";
if (count($this->att_files) > 0)
{ 
    foreach ($this->att_files as $val)
        $message .= $val; 
    $message .= "--".$this->uid."--"; 
} 
echo $message;
print_r($logArray);
//observe the . after the newline, it signals the end of message
//fputs($smtpConnect, $message);
//$smtpResponse = fgets($smtpConnect, 4096);
//$logArray['data2response'] = "$smtpResponse";

// say goodbye
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['quitresponse'] = "$smtpResponse";
$logArray['quitcode'] = substr($smtpResponse,0,3);
fclose($smtpConnect);
//a return value of 221 in $retVal["quitcode"] is a success
return($logArray);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑凤梨 2024-12-18 14:46:08

您可以尝试使用 SwiftMailer,它几乎可以通过任何方式处理发送电子邮件的所有部分。包括登录电子邮件帐户并通过该帐户发送。它是用 PHP 编写的,因此您可以查看/更改代码。

http://swiftmailer.org/

You might try using SwiftMailer which handles almost all parts of sending an email through almost any means. Including logging into an email account and sending through that. It's written in PHP, so you can view/change the code.

http://swiftmailer.org/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文