admin管理员组文章数量:1336408
I have implemented an email forwarding feature in my Laravel 9 application using PHP 8.0. The forwarding works, but when the email is forwarded, the original "To" field and recipient information are removed or altered. Instead, the forwarded email appears as if it was sent from the CRM account, losing the original structure of the email.
public function emailForward(Request $request)
{
$imap = new Imap([
'host' => config('app.mail_host'),
'user' => config('app.support_user_id'),
'password' => config('app.support_user_password'),
'port' => config('app.mail_port'),
'ssl' => config('app.mail_ssl'),
'novalidatecert' => config('app.mail_novalidatecert'),
]);
// return $imap->countMessages();
// Fetch the email
$message = $imap->getMessage(203);
// Extract the email body
$body = '';
if ($message->isMultipart()) {
foreach (new RecursiveIteratorIterator($message) as $part) {
if (stripos($part->contentType, 'text/plain') !== false) {
$body = $part->getContent();
break;
} elseif (stripos($part->contentType, 'text/html') !== false) {
// $body = $part->getContent();
$body = nl2br($part->getContent());
}
}
} else {
// $body = $message->getContent();
$body = nl2br($part->getContent());
}
// Forward the email
Mail::raw($body, function ($mail) use ($message) {
$mail
->from('[email protected]', 'Amazon')
->to('[email protected]', 'Jhon')
// ->to('[email protected]', 'Jhon')
->subject('Fwd:alt.xyz[enter image description here][1]');
});
return response()->json(['message' => 'Email forwarded successfully!']);
}
I have implemented an email forwarding feature in my Laravel 9 application using PHP 8.0. The forwarding works, but when the email is forwarded, the original "To" field and recipient information are removed or altered. Instead, the forwarded email appears as if it was sent from the CRM account, losing the original structure of the email.
public function emailForward(Request $request)
{
$imap = new Imap([
'host' => config('app.mail_host'),
'user' => config('app.support_user_id'),
'password' => config('app.support_user_password'),
'port' => config('app.mail_port'),
'ssl' => config('app.mail_ssl'),
'novalidatecert' => config('app.mail_novalidatecert'),
]);
// return $imap->countMessages();
// Fetch the email
$message = $imap->getMessage(203);
// Extract the email body
$body = '';
if ($message->isMultipart()) {
foreach (new RecursiveIteratorIterator($message) as $part) {
if (stripos($part->contentType, 'text/plain') !== false) {
$body = $part->getContent();
break;
} elseif (stripos($part->contentType, 'text/html') !== false) {
// $body = $part->getContent();
$body = nl2br($part->getContent());
}
}
} else {
// $body = $message->getContent();
$body = nl2br($part->getContent());
}
// Forward the email
Mail::raw($body, function ($mail) use ($message) {
$mail
->from('[email protected]', 'Amazon')
->to('[email protected]', 'Jhon')
// ->to('[email protected]', 'Jhon')
->subject('Fwd:alt.xyz[enter image description here][1]');
});
return response()->json(['message' => 'Email forwarded successfully!']);
}
Share
asked Nov 19, 2024 at 18:51
HusainHusain
52 bronze badges
1
- In emailing today you shouldn't be sending emails on behalf of just anybody, you should only be sending from the domains you controls. If you don't, the likelihood of your emails disappearing in a black hole is getting close to 100%. – KIKO Software Commented Nov 19, 2024 at 18:54
1 Answer
Reset to default 0The issue arises because when you forward an email in your Laravel application, you are manually constructing the forwarded email using Mail::raw. This does not preserve the original "To", "From", "CC", or "BCC" fields as they appear in the original email. Instead, the email is sent as if it were a new email from the email address defined in the from() method.
To solve this problem and preserve the original structure of the email, you need to include the original headers, such as the original "To" and "From" fields, in the body of the forwarded email. Here's how you can modify your implementation to address this:
public function emailForward(Request $request)
{
$imap = new Imap([
'host' => config('app.mail_host'),
'user' => config('app.support_user_id'),
'password' => config('app.support_user_password'),
'port' => config('app.mail_port'),
'ssl' => config('app.mail_ssl'),
'novalidatecert' => config('app.mail_novalidatecert'),
]);
// Fetch the email
$message = $imap->getMessage(203);
// Extract the email headers and body
$originalHeaders = [
'From' => $message->getHeader('from'),
'To' => $message->getHeader('to'),
'Subject' => $message->getHeader('subject'),
];
$body = '';
if ($message->isMultipart()) {
foreach (new RecursiveIteratorIterator($message) as $part) {
if (stripos($part->contentType, 'text/plain') !== false) {
$body = $part->getContent();
break;
} elseif (stripos($part->contentType, 'text/html') !== false) {
$body = nl2br($part->getContent());
}
}
} else {
$body = nl2br($message->getContent());
}
// Create the forwarded email content
$forwardedBody = "Forwarded message:\n\n";
$forwardedBody .= "From: {$originalHeaders['From']}\n";
$forwardedBody .= "To: {$originalHeaders['To']}\n";
$forwardedBody .= "Subject: {$originalHeaders['Subject']}\n\n";
$forwardedBody .= "---- Original Message ----\n\n";
$forwardedBody .= $body;
// Forward the email
Mail::raw($forwardedBody, function ($mail) use ($message) {
$mail
->from('[email protected]', 'Amazon')
->to('[email protected]', 'Jhon')
->subject('Fwd: ' . $message->getHeader('subject'));
});
return response()->json(['message' => 'Email forwarded successfully!']);
}
本文标签: phpSubject Issue with Email ForwardingOriginal quotToquot Field is MissingStack Overflow
版权声明:本文标题:php - Subject: Issue with Email Forwarding - Original "To" Field is Missing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742405095a2468657.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论