1

I have a problem with sending emails to gmail through Php mail() function. I tested this script with my yahoo mail and there is no problem (email body has no attachments). Here is my code:

$mailto = "info@email.com";
$charset = "utf-8";
$subject = "subject text";
$content = "text/html";

    $headers  = "MIME-Version: 1.0\n";
    $headers .= "Content-Type: $content  charset=$charset\n";
    $headers .= "Date: ".date("Y-m-d (H:i:s)",time())."\n";
    $headers .= "From: \"".$mailto."\" <".$mailto.">\n";
    $headers .= "X-Mailer: My Send E-mail\n";
    $message = "message text";

    mail($_POST['posEmail'],$subject,$message,$headers);
orolo
  • 3,951
  • 2
  • 30
  • 30
Andrey
  • 810
  • 1
  • 9
  • 20
  • 1
    Note that this code could be **very** dangerous unless used in a protected environment only. One should sanitize all parameters to mail() or one will get in trouble (there are robots out there that automatically check if your script is vulnerable, so this script would be abused for spamming sooner or later). The vulnerability is in first parameter which is taken from $_POST directly. Of course, you might be protecting access to this script with other means. YMMV. – johndodo Oct 26 '11 at 06:13
  • @Pilgrim You have told that problem with sending email to GMAIL, Have you checked in spam folder ? and please mention your **error**... – Ashok KS Oct 26 '11 at 06:23
  • @johndodo thanks for caution. i will add defence to Post variables. I receive email, but text message receives as attachment not as regular text – Andrey Oct 26 '11 at 06:56

4 Answers4

1
$headers .= "Content-Type: $content; charset=$charset\n";

The semicolon between content and charset?

And try terminating the line with \r\n, not \n.

gd1
  • 11,300
  • 7
  • 49
  • 88
  • i review headers from original mail and there was an ; between content type and charset. I was trying both \r\n and \n nothing changes. – Andrey Oct 26 '11 at 06:55
1

Use phpmailer class, it is much safer and easier to use. And it supports attachments and similar.

johndodo
  • 17,247
  • 15
  • 96
  • 113
1
$headers =  'To: ' . $epost . "\r\n";
$headers .= 'From: ' . $mailto . "\r\n";
$headers .= 'Message-ID: <' . $ordernummer . ">\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: text/plain; charset=UTF-8' . "\r\n";
$headers .= 'Content-Transfer-Encoding: quoted-printable';

These headers work. I was also getting mails as an attachment named noname.txt.

I Added Message-ID, Content-Transfer-Encoding, took away Real Names from To and From, skipped the <> around the emails, took away quotes around text/plain and around UTF-8 which I capitalized.

antyrat
  • 27,479
  • 9
  • 75
  • 76
Jonas Äppelgran
  • 2,617
  • 26
  • 30
0

you just edit your php.ini and put this

SMTP = smtp.gmail.com

smtp_port = 25

sendmail_from = youremailid@gmail.com

Send email using the GMail SMTP server from a PHP page

Community
  • 1
  • 1
Naveenbos
  • 2,532
  • 3
  • 34
  • 60
  • I don't think it has anything to do with the problem of the OP and, more, it won't work at all – gd1 Oct 26 '11 at 06:08
  • i'm don't want to sen email from gmail account. i have a problem receiving mails to gmail account – Andrey Oct 26 '11 at 06:52