Gmail IMAP php mail send as reply of email - php

I want to send an email as reply in gmail through my Laravel CRM system.
My code is as below:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.Auth::user()->f_name.' '.Auth::user()->l_name.'<'.$gmail_address.'>'." \r\n" .
'Reply-To: <'.$gmail_address.'>'. "\r\n" .
'Subject: '.$subject."\r\n".
'To: '.$to."\r\n".
'In-Reply-To: <56F15324.7050704#xxxx.xxx>'. "\r\n" .
'References: <56F15324.7050704#xxxx.xxx>'. "\r\n" .
'X-Mailer: PHP/' . phpversion();
imap_mail ( $to , $subject ,$body,$headers);
But it sends it as a new email, i.e not as a reply.
Ideally it should add "Re: " to subject and append the actual email at the end of reply email body.
Any Help please..

Your In-Reply-To and References headers are hardcoded to some magic value; that's probably not what you want to do.
Here is how I would improve the code:
Use a library which handles the rather low level bits of RFC2047, RFC2231 and especially RFC 5322 for you. It's very likely that any non-ascii characters in your user's l_name produce a non-compliant message. Read the whole RFC5322 to understand how e-mails work. Read about various encodings which come into play.
Track the Message-Id of the message(s) that you're replying to, and set your own In-Reply-To & References headers accordingly.

Related

The e-mail header from the online form is displayed incorrectly

I have a questionnaire form on the web. After filling in this form, I send it to me by email, using the PHP function mail(). The form body and the data it contains, including the private message are displayed correctly on gmail.com. The problem, however, occurs in the header of the email itself. Some characters are displayed incorrectly.
Here is a sample header:
$headers = "Content-Type:text/html; charset=utf-8\r\n";
$headers .= "From:" .$email . "\r\n";
$headers .= "Reply:" . $email . "\r\n";
$headers .= "X-Mailer: PHP/". phpversion() . "\r\n" ;
Required display of email subject:
Nový dotaz -- námět, od Fořt Petr <p.fort1990#gmail.com>
Simultaneous displaying of the subject:
Nový dotaz -- námÄ☒t od: FoÅ☒t Petr <p.fort1990#gmail.com>
The squared times symbol is more like a rectangle.
Is anything wrong? Or where should I look for a mistake?
I'm not sure \r\n works on all platforms
see : Which line break in php mail header, \r\n or \n?
instead
("xxx\r\n\yyy");
use
Header('xxx');
Header('yyy');
or use PHP_EOL, not "\r\n"
Problem solved. My hosting provider uses different character encoding for the headers - I can't explain why, but the following php function will do it all.
function recode_to_utf8 ($text, $encoding = "utf-8")
{
return "=?$encoding?Q?" . imap_8bit($text) . "?=";
}
And now all you have to do is send an email using the mail () method in combination with the method defined above recode_to_utf8(). Like this:
mail(recode_to_utf8($mail_to), recode_to_utf8($subject), recode_to_utf8($message), recode_to_utf8($headers));
I hope it helps others if they have the same problem as me.

Accept and record condition value from received HTML email form

I'm trying to figure out with email confirmation function via email, received by user.
If I have <input type='submit' value='Accept' id='btn1'>, (which is correct way shown by ADyson, instead double quotes: <input type="submit" value="Accept" id="btn1">), then how do I use PHP $_POST['acceptval'] for confirmation from this HTML, which must record value to MySQL database from received message form by included ID, then to pass this condition from the website.
So, if I will include require 'connect.php'; to code below, and will get data for to, message, subject, I will receive message to my userreceiveremail#gmail.com, then I want allow user press button, and record value to his record field, but I can't figure out, how it works:
<?php
$to = "userreceiveremail#gmail.com";
$subject = "Confirmation notification";
$message = "
<html>
<head>
<title>Accept registration</title>
</head>
<body>
<p>Hello, dear User! Please, accept your registration</p>
<input type='submit' value='Accept' id='btn1'>
</body>
</html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <senderemail#gmail.com>' . "\r\n";
$headers .= 'Cc: Some text' . "\r\n";
mail($to,$subject,$message,$headers);
echo "Mail sent successfully";
?>
Maybe my scenario does not correspond to the classic or correct approach to solving this particular task, because I'm just trying to figure out, how to get desired result, anyway yet I can't find any close example for this.
Any advice, guide or example would be very helpful

Best way to send emails generated by visitors using website get quota form

I'm struggling with this issue for months now.
I have a simple website wich has a "Get price quota" php form that interested visitors can use to ask for prices.
The PHP form sends an email to the admin using PHP's "mail".
Everything works fine with Google, Hotmail, etc EXCEPT, I get my email rejected all the time from Yahoo recipients. Every two or three months I have to get my dedicated IP changed and the hosting company to make unblacklisting requests because emails sent back to the customers bu the admin using email accounts on the same server\domain are rejected. Weird or not, not all of them are rejected.
I went through all info and tips I could find about securing my form, checked the logs to see if somehow my domain\website is used for spam, I have DKIM enabled, etc, nothing works, I know Yahoo has bad problems but I wonder if there is another way to send emails from forms, small amounts, max 100 per month.
I'm going to post my php email script below, please tell me if there is something wrong that could cause Yahoo to blacklist my domain or is it just Yahoo.
//Secure The Submitted Data
$quoteFirstName = stripslashes($_POST["quoteFirstName"]);
$quoteLastName = stripslashes($_POST["quoteLastName"]);
$quoteEmail = stripslashes($_POST["quoteEmail"]);
$quoteDetails = stripslashes($_POST["quoteDetails"]);
$quoteProduct = stripslashes($_POST["quoteProduct"]);
$quoteTel = stripslashes($_POST["quoteTel"]);
$mailtolink = '' . $quoteEmail .'';
//Build The Email To The Admin
$to = "orders#mydomain.com";
$subject = "Price quota request";
$theEmail = "$quoteFirstName $quoteLastName <br /> $mailtolink <br /> Telephone $quoteTel <br /> Price quota for <br /> - $quoteProduct.<br /> Additional info: $quoteDetails";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: " . $quoteEmail . "\r\n" . "Reply-To: " . $quoteEmail . "\r\n" . "X-Mailer: PHP/" . phpversion();
mail($to, $subject, $theEmail, $headers);

How to set the from name in the mail() function [duplicate]

This question already has answers here:
problem with php mail 'From' header
(8 answers)
What is the format for e-mail headers that display a name rather than the e-mail?
(3 answers)
Closed 8 years ago.
When I send a message using the code below, it sends fine. But if I change the $headers to have 'From: Firstname Lastname' it doesn't send for some reason. How do I add my name to the header instead of the email address?
$subject = 'Test message';
$headers = 'From: mymail#mydomain.com' . "\r\n" .
'Reply-To: mymail#mydomain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $themessage, $headers);
Some (most, I think) email clients check the From field to make sure the domain is the same as the domain that sent the message. This is to prevent spoofing. Try something like
"From: Firstname Lastname <name#domain.com>"

Formatting Php Form Variable

A user fills out a form which is sent to my email via the variables their data is read into.
I want to have headers above each users entry, and have these appear bold in the email.
The email should read:
OCCUPATION:
Data the user entered.
I have tried
$Occupationheader = "<strong>"."OCCUPATION"."</strong>"."\n\n" ;
and
$Occupationheader = "<strong>OCCUPATION:<strong>\n\n";
The data is sent to the email, as follows: (this works, but want to format the headings in bold).
mail( "myemailaddress", "subjectmatter",
$Occupationheader.$Occupation);
Any ideas?
Thanks guys.
$mailContent = "<html>
<body>
<strong>HEADER</strong>
<br/>message
</body>
</html>";
NOTE: This is provided as an additional answer.
Without seeing full source code, am providing my (additional) answer below.
The following, need to be present in your code, in order to send out Emails in HTML format:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
As per the PHP manual on the subject: http://php.net/manual/en/function.mail.php

Categories