String "1" gets added in php mail() - php

I'm sending an email using php's mail() function. My code is as following:
$to = email#email.com;
$subject = "Subject of email";
ob_start();
echo include('emailcontent.php');
$message = ob_get_contents();
ob_end_clean();
$headers =
'From: Email <email#email.com>' . "\r\n" .
'Reply-To: Email <no-reply#email.com>' . "\r\n" .
"Content-type:text/html;charset=UTF-8" . "\r\n" .
"MIME-Version: 1.0" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
For some reason the string "1" is added at the end of the message. This happens in the include('emailcontent.php'), because if I add another string to the message after the include the "1" is added before that addition.
In the emailcontent.php is no 1 or whatsoever.

echo include('emailcontent.php');
Remove the echo. That echo puts 1 in there
You might ask why does echo put 1 in there when its not added anywhere? Because when your file is successfully included the return value of that include is TRUE and when you send that to echo it prints 1. Try
echo 2==2; //1

Why you are echoing the php page? You have to include the page like,
include('emailcontent.php');
Remove the echo it is causing the 1.

Related

send html format through php mail() [duplicate]

This question already has answers here:
Send HTML in email via PHP
(8 answers)
Closed 4 years ago.
This is in code-igniter. To send mail I have written a function using php mail(). The code is:
function mailsend()
{
$to="mymail#gmail.com";
$subject="test";
$message="<table>
<tr>
<td>Hello</td>
<td>World</td>
</tr>
</table>";
echo $message;
mail($to,$subject,$message);
}
When I send mail from server side, I get the html code in my mail, not the real table. But the $messageshows the real table in browser. Is there any way in mail() to get the real table in the mail?
You can send the html content in email via setting the header portion. Below is the sample code you can use.
<?php
$to = 'maryjane#email.com';
$subject = 'Marriage Proposal';
$from = 'peterparker#email.com';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Jane!</h1>';
$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</body></html>';
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
In order to send html content in your email with mail function you will need to add header options to it, like so:
$headers = 'From: test#yourdomain.com' . "\r\n" .
'Reply-To: test#yourdomain.com' . "\r\n" .
"Content-type:text/html;charset=UTF-8" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($sendTo, $subject, $message, $headers);
Add an header to your mail function like this
$headers = 'From: test#yourdomain.com' . "\r\n" .
'Reply-To: hello#example.com' . "\r\n" .
"Content-type:text/html;charset=UTF-8" . "\r\n" .
'X-Mailer: PHP/' . phpversion();

How to display my html page in mail box

<?php
$to = 'abc#gmail.com';
$subject = 'the subject';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = file_get_contents('http://domain.com/newsletter.html');
$headers = 'From: xyz#gmail.com' . "\r\n" .
'Reply-To: xyz#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
This in my mail code. Here I am sending my newsletter to my existing customer.
Can anyone help me how can I send/open the newsletter as the page through mail ?
I suggest you use the following, yet you won't be able to use an http:// call, because this goes against security reasons.
By doing so, anyone could include whatever they want from whatever website. The file must reside on your server in order for the following to work.
Making use of the include(), ob_start() and ob_get_clean() functions.
You must use a relative path to the file you wish to include.
<?php
ob_start();
include 'file.xxx';
$message = ob_get_clean();
$to= 'email#example.com';
$subject = 'the subject';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: email#example.com' . "\r\n" .
'Reply-To: email#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)){
echo "Message sent.";
}
else{
echo "There was a problem. Check your logs.";
}
?>
Footnotes:
I quote jsalonen from a comment: "Keep in mind that CSS support in email readers is (probably intentionally) very limited: campaignmonitor.com/css - so if it works in a regular browser, it may or may not work in an email reader."
Most Email clients such as Google will ignore CSS, so it's best to use inline CSS.
Images:
Another thing you will need to do is, if you are using images, then that's where you will need to use an http:// call to the image location(s).
I.e.: <img src="http://www.example.com/images/image_1.jpg">
if allow_url_fopen is available in the ini file, you can do this:
$html = file_get_contents('http://domain.com/newsletter.html');
$message = $html;
The reason this should work is because most email clients can read emails sent as HTML (as long as you're sending emails as HTML and not just plaintext emails).
You can use echo htmlspecialchars($html); to see what HTML you're sending.

Ajax form submission error using PHP Mailer

I implemented this http://www.websitecodetutorials.com/code/jquery-plugins/jquery-ajaxsubmit.php tutorial on my form, and the form works successfully and displays the thank you div on submission. But the problem is I don't receieve any email, seems like PHP mailer is not working.
Please see the code below
<?php
// Insert your email/web addresses and correct paths
$mailto = 'adil#adilsaleem.co.uk' ;
$from = "web#city.com" ;
$formurl = "http://astonstorlin.co.uk/citycoaches3/formmail.php" ;
$errorurl = "http://astonstorlin.co.uk/citycoaches3/error.php" ;
$thankyouurl = "http://astonstorlin.co.uk/citycoaches3/thankyou.php" ;
// Place Your Form info here...
$pickuppoint = ($_POST['pickuppoint']);
$destination = ($_POST['destination']);
// Check If Empty
// Add more Validation/Cleaning here...
// Place your Corresponding info here...
$message =
"Pick Point: $pickuppoint\n\n" .
"Destination: $destination\n\n" .
"noppl: $noppl\n\n"
;
// Leave Alone
mail($mailto, $from, $message,
"From: \"$name\" <$email>" . $headersep . "Reply-To: \"$name\" <$email>" . $headersep );
header( "Location: $thankyouurl" );
exit ;
?>
I think your headers are not working. $headersep is not defined and moreover please follow :
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($to, $subject, $message, $headers);
Example copied from http://php.net/manual/en/function.mail.php you can refer for more reading this link.
If you are on a shared hosting for example, sometimes they require you to use a fifth parameter like additional_parameters.
mail('nobody#example.com', 'the subject', 'the message', null,'-fwebmaster#example.com');
Check example 3 here: http://php.net/manual/en/function.mail.php
Thank you very much for all your help people, much appreciated.
I got the script working by deleting
"From: \"$name\" <$email>" . $headersep . "Reply-To: \"$name\" <$email>" . $headersep );
And the script started working. I have tried using other HTML php mailer but them don't seem to work with this tutorial example for some reason.http://www.websitecodetutorials.com/code/jquery-plugins/jquery-ajaxsubmit.php

keeping line break while sending email using php

I am using below php code to send email after collecting user entered email ID (email-box) and content from a textarea(output_textarea) as message.
$to = $_REQUEST['email-box'] ;
$message = $_REQUEST['output_textarea'];
$subject = 'form meta cli script';
$headers = 'From: info#mydomain.com' . "\r\n" .
'Reply-To: me#mydomain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
But outlook is removing the line break and on top giving message "Extra Line breaks in this message were removed" How can I keep the line break in outlook?

sending get_defined_vars() to yourself via email (PHP)

During development when other people are experimenting with my site and have issues, I want to be able to find out where my code was having issues, get_defined_vars() is probably the most useful thing for me in finding out what happened at this point.
I am up to the point of writing this function, however it is returning:
Parse error: syntax error, unexpected '='
Does anybody know a way to send yourself get_defined_vars() from php?
if(isset($_GET['sendmeanemail'])){
$emailarr = get_defined_vars();
$to = 'myemail#gmail.com';
$subject = 'Debug Report for'. $currentApiUser['first-name']. ' '. $currentApiUser['last-name'];
$message = '<pre>\n';
$message. = print_r(addslashes($emailarr));
$message. = '</pre>';
$headers = 'From: webmaster#domain.com' . "\r\n" .
'Reply-To: webmaster#domain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
Edit: This was fixed by changing to: (pre tags arent actually necessary, am figuring out how to better format for gmail)
if(isset($_GET['sendmeanemail'])){
$emailarr = get_defined_vars();
$to = 'myemail#gmail.com';
$subject = 'Debug Report for'. $currentApiUser['first-name']. ' '. $currentApiUser['last-name'];
$message = '<pre>';
$message .= print_r($emailarr, true);
$message .= '</pre>';
$headers = 'From: webmaster#domain.com' . "\r\n" .
'Reply-To: webmaster#domain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
You need to call print_r with the second parameter set to true. Also, addslashes should not be called on an array. It should look like:
print_r($emailarr,true);
Last, you need to move the . (period) next to the equal sign. Your code should look like:
$message = '<pre>\n';
$message .= print_r($emailarr,true);
$message .= '</pre>';

Categories