Php 通过SMTP发送电子邮件不更改发件人';s域?
我写了一个小脚本,向大约200名客户发送电子邮件。问题是脚本运行在一个小型托管站点上,我需要使用我的公司电子邮件地址作为发件人。这将是好的,除了大多数(如果不是所有的话)我的客户都有强大的垃圾邮件过滤器,如果发送域与发件人的电子邮件地址域不同,它们将阻止我的邮件(也就是说,我在'example.com'托管脚本,但我的电子邮件是'example.com'business@company.com" 我曾尝试将SMTP'ng发送到gmail帐户并以这种方式发送(作为从公司电子邮件发送之前的测试),但它似乎不起作用,它仍然包含我域的所有标题 关于我做错了什么有什么建议吗?还有其他方法吗 我正在使用的SMTP代码:Php 通过SMTP发送电子邮件不更改发件人';s域?,php,email,smtp,Php,Email,Smtp,我写了一个小脚本,向大约200名客户发送电子邮件。问题是脚本运行在一个小型托管站点上,我需要使用我的公司电子邮件地址作为发件人。这将是好的,除了大多数(如果不是所有的话)我的客户都有强大的垃圾邮件过滤器,如果发送域与发件人的电子邮件地址域不同,它们将阻止我的邮件(也就是说,我在'example.com'托管脚本,但我的电子邮件是'example.com'business@company.com" 我曾尝试将SMTP'ng发送到gmail帐户并以这种方式发送(作为从公司电子邮件发送之前的测试),但
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);
如果(!$this->有效邮件地址)返回;
//连接到主机和端口
$smtpConnect=fsockopen($this->smtpServer,$this->port,$errno,$errstr,$this->timeout);
$smtpResponse=fgets($smtpConnect,4096);
if(空($smtpConnect))
{
$this->msg[]=“连接失败:$smtpResponse”;
变量转储($this->msg);
返回;
}
其他的
{
$logArray['connection']=“连接到:$smtpResponse”;
echo“连接已接受”
“$smtpResponse。”继续”;
}
//TLS启动后,你必须再次说HELO
fputs($smtpConnect,“HELO$localhost”。$newLine);
$smtpResponse=fgets($smtpConnect,4096);
$logArray['heloresponse2']=“$smtpResponse”;
//请求身份验证登录
fputs($smtpConnect,“身份验证登录”。$newLine);
$smtpResponse=fgets($smtpConnect,4096);
$logArray['authrequest']=“$smtpResponse”;
//发送用户名
fputs($smtpConnect,base64_encode($this->username)。$newLine);
$smtpResponse=fgets($smtpConnect,4096);
$logArray['authusername']=“$smtpResponse”;
//发送密码
fputs($smtpConnect,base64_encode($this->password)。$newLine);
$smtpResponse=fgets($smtpConnect,4096);
$logArray['authpassword']=“$smtpResponse”;
//来自
fputs($smtpConnect,“邮件来源:FROM_MAIL}>””$newLine);
$smtpResponse=fgets($smtpConnect,4096);
$logArray['mailfromresponse']=“$smtpResponse”;
//电邮至
fputs($smtpConnect,“RCPT-TO:mail\u-TO>”$newLine);
$smtpResponse=fgets($smtpConnect,4096);
$logArray['mailtoresponse']=“$smtpResponse”;
//电子邮件
FPUT($smtpConnect,“数据”。$newLine);
$smtpResponse=fgets($smtpConnect,4096);
$logArray['data1response']=“$smtpResponse”;
$mail\u message=$this->mail\u body;
如果(计数($this->att_文件)>0)
{
foreach($this->att_文件为$val)
$mail_message.=$val;
$mail\U message.=“-”$this->uid。“-”;
}
如果(邮件($this->mail\u to,$this->mail\u subject,$mail\u message,$this->mail\u headers)){
$this->msg[]=“您的邮件已成功提交。”;
}否则
$this->msg[]=“向您发送邮件时出错。”;
//构造标题
//$headers=“MIME版本:1.0”。$newLine;
//$headers.=“内容类型:多部分/混合;边界=\”{$this->uid}\“字符集=iso-8859-1”。$newLine;
$headers.=$this->mail\u headers;
//$headers.=“收件人:{$this->To_name}mail_To}>”$newLine;
//$headers.=“From:{$this->name_From}”。$newLine;
$message=“收件人:{$this->mail\u To}\r\n发件人:{$this->from\r\n收件人:{$this->mail\u subject}\r\n$headers\r\n\r\n{$this->mail\u body}\r\n”;
如果(计数($this->att_文件)>0)
{
foreach($this->att_文件为$val)
$message.=$val;
$message.=“-”$this->uid.“-”;
}
回声$信息;
打印(logArray);
//观察。换行后,它表示消息结束
//fputs($smtpConnect,$message);
//$smtpResponse=fgets($smtpConnect,4096);
//$logArray['data2response']=“$smtpResponse”;
//告别
fputs($smtpConnect,“退出”。$newLine);
$smtpResponse=fgets($smtpConnect,4096);
$logArray['quitresponse']=“$smtpResponse”;
$logArray['quitcode']=substr($smtpResponse,0,3);
fclose($smtpConnect);
//$retVal[“quitcode”]中的返回值为221表示成功
返回($logArray);
您可以尝试使用SwiftMailer,它可以处理通过几乎任何方式发送电子邮件的几乎所有部分。包括登录电子邮件帐户并通过该帐户发送。它是用PHP编写的,因此您可以查看/更改代码
你是在使用gmail smtp smtp.gmail.com服务器吗?还是“smtp'ng进入gmail帐户”意味着其他什么?是的,我在使用smtp.gmail.com,连接到smtp服务器可能是更好的表达方式。结果表明,这是路由器上的一个配置错误以及其他许多垃圾。不过我切换到了这个库(太棒了),并添加了一些优点;现在一切都像一个符咒!谢谢