简体   繁体   中英

Send email over SMTP not changing sender's domain?

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 'business@company.com'.

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);

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM