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

时间:2011-10-23 17:38:55

标签: php email smtp

我写了一个小脚本,向大约200位客户发送电子邮件。问题是脚本在小型托管站点上运行,我需要使用我公司的电子邮件地址作为发件人。这很好,除非大多数客户都有强大的垃圾邮件过滤器,如果发送域与发件人的电子邮件地址域不同(例如,我正在托管脚本的例子),它将阻止我的邮件。 com',但我的电子邮件是'business@company.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);

1 个答案:

答案 0 :(得分:1)

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

http://swiftmailer.org/