Viewed   86 times

I'm trying to send an email with PHP.

My problem is actually, the email sent is blank...

My PHP function:

function sendMail($template, $Email_Subject, $USR_Id, $USR_Email) {

    $postdata = http_build_query(
        array(
            'subject' => $Email_Subject
        )
    );

    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );

    $context  = stream_context_create($opts);

    $message = file_get_contents('../../mail/'.$template.'.php', false, $context);

    // Start configuring the email
    $headers .= 'From: Company <[email protected]>' . "rn";
    $headers .= "MIME-Version: 1.0rn";
    $headers .= "Content-Type: text/html; charset=UTF-8rn";

    mail($USR_Email, $Email_Subject, $message, $headers); 
}

My template.php page:

$message = 
'<html>
...
<h1>Subject is : '.$_POST['subject'].'</h1>
...
<html>';
echo $message;

I call the function like this:

sendMail("template", "Account Activation", $USR_Id, $USR_Email);

What is strange is when I echo the $message, it do not echo me Subject is : .... It echoes me Subject is : '.$_POST['subject'].'. Like if PHP dont't work...

Anyone to help me please ?

Thanks.

 Answers

2

If you're just trying to send an email, why are you using stream-contexts and $_POST? This should be done using output buffering (http://php.net/manual/en/book.outcontrol.php):

function sendMail($template, $Email_Subject, $USR_Id, $USR_Email) {

    // everything output between ob_start and ob_end_clean will be stored
    // in a temporary buffer, instead of being send the browser
    ob_start();
    require('../../mail/'.$template.'.php');
    $message = ob_get_clean();

    // Start configuring the email
    $headers  = 'From: Company <[email protected]>' . "rn";
    $headers .= "MIME-Version: 1.0rn";
    $headers .= "Content-Type: text/html; charset=UTF-8rn";

    mail($USR_Email, $Email_Subject, $message, $headers); 
}

In your template you can use all variables that are available in the sendMail function, so use $Email_Subject instead of $_POST. Obviously, if there is anything in $_POST you'd like to print you can still do that using this solution.

Monday, October 24, 2022
 
asperi
 
1

You have $headers .= '...'; followed by $headers = '...';; the second line is overwriting the first.

Just put the $headers .= "Bcc: $emailListrn"; say after the Content-type line and it should be fine.

On a side note, the To is generally required; mail servers might mark your message as spam otherwise.

$headers  = "From: [email protected]" .
  "X-Mailer: phprn";
$headers .= "MIME-Version: 1.0rn";
$headers .= "Content-Type: text/html; charset=ISO-8859-1rn";
$headers .= "Bcc: $emailListrn";
Sunday, December 25, 2022
4

You can specify the encoding in the email headers, like so:

$mail = mail(WEBMASTER_EMAIL,$subject,$message,
        "From: ".$name." <".$email.">rn"
        ."Reply-To: ".$email."rn"
        ."Content-type: text/html; charset=UTF-8rn"
        ."X-Mailer: PHP/" . phpversion());
Wednesday, September 21, 2022
 
3

enable php_openssl.dll in php.ini file and try using gmail. The answer of Pascal Spruit or Omade will work. Don't forget to restart you local server after editing in php.ini file.

Thursday, October 13, 2022
 
5

PHPMailer is the best to use for email.

checkout some below links which will help you in future also:

  1. File Attachments in PHP Mail with PHPMailer
  2. php mailer attachments
  3. attachments using phpMailer
  4. PHPMailer Tutorial
  5. A Simple Example : Sending Email with Attachment Using Phpmailer
  6. PHP send email with multiple attachments with PHPMailer with SMTP Authentication
  7. Sending email with multiple attachments with PHP

may this help you.

Saturday, November 5, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :