Jump to content

Recommended Posts

I am trying to use vcalendar on a site that i am working on and i need to edit the e-mail function they are using to put a user name and password in for the smtp server.

 

My question is how can i pass the a user name and password for my smtp server to the php mail function so that it can use my smtp server as a relay.

 

Thanks in advance for the help.

Link to comment
https://forums.phpfreaks.com/topic/113658-php-mail-function/
Share on other sites

stolen from About.com

<?php
require_once "Mail.php";

$from = "Sandra Sender <sender@example.com>";
$to = "Ramona Recipient <recipient@example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_password";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
} else {
  echo("<p>Message successfully sent!</p>");
}
?>

Link to comment
https://forums.phpfreaks.com/topic/113658-php-mail-function/#findComment-584438
Share on other sites

I have tired this and i am getting a error. I am using this in a function.

 

error:

 

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vcalendar/common_functions.php:316) in /var/www/vcalendar/registration.php on line 532

 

here is there original code:

function SendEmailMessage($variable, $email_to, $parameters) {
        global $calendar_config;
        $value = 0;

        if (strlen($variable)) {
                $db = new clsDBcalendar();
                $SQL = "SELECT email_template_from, ".
                                        "email_templates.email_template_subject AS mainSubject, ".
                                        "email_templates_lang.email_template_subject AS localeSubject, ".
                                        "email_templates.email_template_body AS mainBody, ".
                                        "email_templates_lang.email_template_body AS localeBody ".
                           "FROM email_templates LEFT JOIN email_templates_lang ".
                                        "ON email_templates.email_template_id = email_templates_lang.email_template_id ".
                           "WHERE email_template_type = ".$db->ToSQL($variable, ccsText).
                                        " AND language_id = ".$db->ToSQL(CCGetSession("locale"), ccsText);
                $db->query($SQL);
                if ($db->next_record()) {
                        $email_subject = CCStrLen($db->f("localeSubject"))? $db->f("localeSubject") : $db->f("mainSubject");
                        $email_body = CCStrLen($db->f("localeBody"))? $db->f("localeBody") : $db->f("mainBody");
                        $email_from = CCStrLen($db->f("email_template_from"))? $db->f("email_template_from") : $calendar_config["site_email"$

                        while (list($key,$value) = each($parameters))
                                $email_body = str_replace($key,$value,$email_body);

                        $email_body = str_replace("{site_url}",ServerURL,$email_body);

                        if (strlen(trim($calendar_config["SMTP"]))) {
                                $value = mailsmtp($email_from, $email_to, $email_subject, $email_body, "From: $email_from\nContent-Type: tex$
                        } else {
                                $value = @mail($email_to, $email_subject, $email_body, "From: $email_from\nContent-Type: text/plain");
                                if ($value)
                                        $value = "";
                                else
                                        $value = "Email sending ERROR";
                        }
                }
                $db->close();
    }
    return $value;
}

 

and when i change it it looks like this:

 

function SendEmailMessage($variable, $email_to, $parameters) {
        global $calendar_config;
        $value = 0;

        if (strlen($variable)) {
                $db = new clsDBcalendar();
                $SQL = "SELECT email_template_from, ".
                                        "email_templates.email_template_subject AS mainSubject, ".
                                        "email_templates_lang.email_template_subject AS localeSubject, ".
                                        "email_templates.email_template_body AS mainBody, ".
                                        "email_templates_lang.email_template_body AS localeBody ".
                           "FROM email_templates LEFT JOIN email_templates_lang ".
                                        "ON email_templates.email_template_id = email_templates_lang.email_template_id ".
                           "WHERE email_template_type = ".$db->ToSQL($variable, ccsText).
                                        " AND language_id = ".$db->ToSQL(CCGetSession("locale"), ccsText);
                $db->query($SQL);
                if ($db->next_record()) {
                        $email_subject = CCStrLen($db->f("localeSubject"))? $db->f("localeSubject") : $db->f("mainSubject");
                        $email_body = CCStrLen($db->f("localeBody"))? $db->f("localeBody") : $db->f("mainBody");
                        $email_from = CCStrLen($db->f("email_template_from"))? $db->f("email_template_from") : $calendar_config["site_email"$

                        while (list($key,$value) = each($parameters))
                                $email_body = str_replace($key,$value,$email_body);

                        $email_body = str_replace("{site_url}",ServerURL,$email_body);

require_once "Mail.php";
$from = $email_from;
$to = $email_to;
$subject = $email_subject;
$body = $email_body;

$host = "mysmtpserver";
$username = "XXXXXX";
$password = "XXXXXXXXXX";

$headers = array('From' => $from,
'To' => $to,
'Subject' => $subject);

$smtp = Mail::factory('smtp',
        array('host' =>$host,
                'auth' => $ture,
                'username' => $username,
                'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
  $vaule = "";
} else {
  echo("<p>Message successfully sent!</p>");
  $value = "Email sending ERROR";
}

                }
                $db->close();
    }
    return $value;
}

 

if i run a smiler script in its own file it works fine, but it will not work it in a function i guess. Is there anyone who has any ideas.

 

Thanks in advance for the help.

Link to comment
https://forums.phpfreaks.com/topic/113658-php-mail-function/#findComment-584579
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.