问题
I am using PHPmailer in order to send an e-voucher to customers. I am wanting to send a dynamic html document to the user as the body text however when i refer to this file, i get an error.
My code is as follows:
$sessionid = '12345';
$cid = '98765';
require('class.phpmailer.php');
$bodytext1 = "Your Evoucher is attached for ".$row_checkout['name'];
$email = new PHPMailer();
$email->From = 'noreply@xxx.com';
$email->FromName = 'xxx.com';
$email->Subject = 'Your e-voucher for '.$row_checkout['name'];
$email->AddAddress( $row_user['email'] );
$email->MsgHTML(file_get_contents('mail.php?sid='.$sessionid.'&cid='.$cartid));
$file_to_attach = $sessionid.'/'.$bookingid.'.pdf';
$email->AddAttachment( $file_to_attach , $bookingid.'.pdf' );
return $email->Send();
When i run this, i get the following error:
Warning: file_get_contents(mail.php?sid=12345&cid=98765) [function.file-get-contents]: failed to open stream: No such file or directory in /home4/mission/public_html/xxx.com/evoucher/new/createandmail.php on line 93
However, if I don't put the variables in the url, i.e.
$email->MsgHTML(file_get_contents('mail.php')
it sends fine but a with a body without the correct fields.
Any suggestions?
回答1:
You have to use a full URL (containing the protocol: http://..
) or use file names. mail.php?sid...
isn't a filename - this is a relative uri which is handeld by apache. If you use file_get_contents('mail.php'), you send the raw php file, which might probably not what you want.
You could also try the virtual
function instead of using file_get_contents('http://server.com/mail.php?...'
回答2:
you cannot add GET parameters to file you want to load.
file_get_contents
would load your php file content - not what script will generate.
if you have url fopen enabled you can add your full url address to mail.php but it's not always the best idea or you can include this mail.php to your code (but first change _GET to your internal variables)
回答3:
I am suggesting getting content of the mail.php using cURL then the content should be fetched correctly. (Or use full url in file_get_contents)
回答4:
You can include a file and use variables (session and cart ID) from the current script. If you set the HTML body in the included PHP script, there will be no problem.
来源:https://stackoverflow.com/questions/17687325/phpmailer-unable-to-load-msghtml