This question already has an answer here:
php email form issue
(1 answer)
Closed 9 years ago.
I was trying to send mail using following method. But if I use $headers the functionality is not working.Otherwise it is working fine. Where I went wrong?
if(isset($_REQUEST['submit']))
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$phone=$_REQUEST['phn'];
$country=$_REQUEST['country'];
$company=$_REQUEST['company'];
$subject=$_REQUEST['subject'];
$enquiry=$_REQUEST['message'];
$subject = "xyz.com-".$subject;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$name."\r\n";
$headers .= "Reply-To: ".$email."\r\n";
$msg="
<table><tr><td>Name</td><td>".$name."</td></tr>
<tr><td>Email:</td><td>".$email."</td></tr>
<tr><td>Contact Number:</td><td>".$phone."</td></tr>
<tr><td>Country:</td><td>".$country."</td></tr>
<tr><td>Company:</td><td>".$company."</td></tr>
<tr><td>Message:</td><td>".$enquiry."</td></tr>
</table>";
if(mail("myName#gmail.com",$subject,$msg,$headers))
{
$msg1="Email Sent Successfully";
}
else
{
$msg1='Mail not sent. Please try again';
}
}
Change the line
$headers .= "From: ".$name."\r\n";
with
$headers .= "From: ".$name."<".$email.">"."\r\n";
Everything else seems good, cheers!
Do not use mail() directly, but find one of the many premade classes for mail sending from the net!
As you dig deeper into mailsending, you WILL face alot of problems, which already have been addressed in mail libraries such as PHPMailer or alike.
Besides of that, your code is a perfect example on "how to build a spam mail application" because it is susceptible for Mailheader-Injection attacks!
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I'm sending email using php header.
$to = 'test#email.com';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = '<h1>Test</h1>';
mail($to, $subject, $message, $headers);
This function is working properly.
But after I added "From" mail() function is not working.
$headers .= "From: from#address.com";
Any idea?
May you are trying to spoof email, so it's getting failed.
It would be great practise to keep using Reply-To with From while using mail()
$headers .= "From: from#address.com\r\n";
$headers .= "Reply-To: from#address.com\r\n";
But, personally suggest you to use SMTP instead of mail() or PHPMailer.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
This is my code. I spent hours debugging it. I can't see the error. Why didn't I didn't receive the email?
if(isset($_POST['submit'])){
$to = "admin#example.com";
$subject = "Mesej Website";
$from = $_POST['email'];
$headers = "From: " . strip_tags($from) . "\r\n";
$headers .= "Reply-To: ". strip_tags($from) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<h3>Mesej Daripada '.$_POST['name'].'</h3>';
$message .= '<div style="margin-top:20px;">'.$_POST['message'].'</div>';
$message = '</body></html>';
mail($to,$subject,$message,$headers) or die('mail sending error');
The in built mail function of PHP needs configuration for sending the email.However, if you are using the third party hosting provider this should be already configured or else you need to configure it in php.ini file.
To avoid these configuration issues you can use the phpmailer library where you can all the parameters easily and in a more efficient way.
This question already has answers here:
How do I prevent mails sent through PHP mail() from going to spam? [duplicate]
(6 answers)
Closed 5 years ago.
I have done everything possible but after registration my email goes to spam and i want it to go into inbox
$subject = 'Confirm your email';
$to = $email;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: CGOTY <info#cgoty.com>' . "\r\n";
$body='Hi, Click here to confirm your email https://cgoty.com/activate.php?encrypt='.$encrypt.'&action=activate ';
mail($to,$subject,$message,$headers);
You made a mistake in your script within you mail contructor of function you said message instead of body message instead of body.
So it should look like this
$body='Hi, Click here to confirm your email https://cgoty.com/activate.php?encrypt='.$encrypt.'&action=activate ';
mail($to,$subject,$body,$headers);
It will work for you
Add this to your header:
$headers .= "Reply-To: $email" . "\r\n";
(replace $email with the mail you want)
This question already has answers here:
Sending multiple attachment in an email using PHP
(2 answers)
Closed 7 years ago.
I wonder if anyone can help me? I have done a lot of research on outputting multiple attachments using PHP mail but I just cant quite get my upload form working correctly.
Background:
I have a form which allows you to upload multiple images. The form itself is fine and i have had working with one one image no problem. The form sends a e-mail with an attachment. The problem has arisen when trying to send multiple attachments.
I can only ever get it to send one.
I have a piece of code below which deals with going through the array of files and adding them to headers of the e-mail. (see Below)
$headers = "MIME-Version: 1.0\r\n";
$headers = "X-Mailer: PHP/" . phpversion()."\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."\r\n\r\n";
$headers .= "From: Sell My Rolex <info#sellmyrolex.today>\r\n";
$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=".md5('boundary2')."\r\n\r\n";
$headers .= "--".md5('boundary2')."\r\n";
$headers .= "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\n";
$headers .= $message."\r\n\r\n";
$x="0";
foreach($files as $file){
$handle = fopen($destination.$file, "r");
$content = fread($handle, filesize($destination.$file));
fclose($handle);
$attachment = chunk_split(base64_encode($content));
$headers .= "Content-Type:".$types[$x]."; ";
$headers .= "name=\"".$file."\"\r\n";
$headers .= "Content-Transfer-Encoding:base64\r\n";
$headers .= "Content-Disposition:attachment; ";
$headers .= "filename=\"".$file."\"\r\n";
$headers .= "X-Attachment-Id:".rand(1000,9000)."\r\n\r\n";
$headers .= $attachment."\n\n";
}
This it is sent out
$message = wordwrap($message, 70);
$mailSent = mail($to, $subject, $message, $headers);
However all i get in the e-mail above the main e-mail message is
Content-Type:image/png; name="image1.png"
Content-Transfer-Encoding:base64
Content-Disposition:attachment; filename="image1.png"
X-Attachment-Id:6299
--f2b57013356e3f84c2c6b1d9b77f95d2--Content-Type:image/png; name="cleanoffice.png"
Content-Transfer-Encoding:base64
Content-Disposition:attachment; filename="cleanoffice.png"
X-Attachment-Id:5833
Can anyone point me in the right direction? I know people may want to suggest using a library like PHPmailer rather than the basic php mail but i want to use php mail to get this complete.
Any help is much appreciated.
Thanks
You need to include multiple boundaries to mark different attachments. The following gives you a good example of how it would look like.
http://www.qcode.co.uk/post/70
My client has a Wordpress content management system to which I added a simple contact form with a php form handler. The contact form sends the information by email correctly to all three of my email addressses, but when I change to my client's email address the email never arrives. I have run out of ideas where I could look for the problem. No it does not go to his junk mail folder. :)
Sounds like the email is being routed "internally" through your clients network and not out onto the internet. The chances are they have some restrictions on what machines can be used to send emails internally, or the mail routing system sees the internal email as being "different" and does something odd with it.
Try using (from a cli) :
echo "Testing " | mailx -"Test Subject Line" user#company.co.uk
What is the mail function that you are using? Do you attach a header to it? It sounds like it is being marked as spam from the exchange server. What I use (and have always worked for me) is something like this:
`
function mailme($sendto,$sendername,$from,$subject,$sendmailbody,$bcc="")
{
$subject = nl2br($subject);
$sendmailbody = nl2br($sendmailbody);
if($bcc!="")
{
$headers = "Bcc: ".$bcc."\n";
}
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=utf-8 \nContent-Transfer-Encoding: 8bit\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-MSMail-Priority: Normal\n";
$headers .= "X-Mailer: PHP/"."MIME-Version: 1.0\n";
$headers .= "From: " . $from . "\n";
$headers .= "Content-Type: text/html\n";
mail("$sendto","$subject","$sendmailbody","$headers");
}
`