dr_rk
dr_rk

Reputation: 4565

Sending email through PHP mail function not working for long emails

I am trying to use the php mail function to send emails from my site. Users on the site type their emails in an html form. I have tested the mail functionality and it works when the email text is small such as:

this is an email 

However, when I type a long email, the mail() function returns 1 but no email is actually received. The code to dispatch email is simply:

$email_message = $_POST['email_mssg_from_form'];
$ret = mail($to, $subject, $email_message);

I have tried to regularize contents of $email_message using:

$email_message = wordwrap($email_message, 70, "\r\n");      
$email_message = addslashes($email_message);

but this didn't help. I am not sure how else to handle quotes and long lines. Normally when a user types an email on a form, there will be continuous lines of text and only paragraphs will be separated by \r\n

Upvotes: 3

Views: 1692

Answers (1)

karancan
karancan

Reputation: 2162

I have had similar problem where the length of the message was causing inconsistencies. This is what did the trick for me:

Use this line to encode your entire message using base64:

$email_message = chunk_split(base64_encode($email_message));

Then, add a header to the mail append this your header:

$headers = "Content-Transfer-Encoding: base64\r\n\r\n";

That will tell the mail client that your message is base64 encoded.

You will have to modify the call to the mail() functio as follows:

mail($to, $subject, $email_message, $headers);

Upvotes: 4

Related Questions