使用Codeigniter在电子邮件上添加“发件人”标题

时间:2020-02-29 15:31:01

标签: php codeigniter email header cpanel

我正在使用CPanel中的Codeigniter,我的代码已经发送了一封邮件,但是当邮件到达接收者时,主机名就会显示在发送者上。 我尝试通过以下方式回答问题:Change the sender name php mail instead of sitename@hostname.com,但在Codeigniter中,它们不起作用。

这是我的代码:

uses
   Androidapi.JNI.App,
   Androidapi.JNI.GraphicsContentViewText,
   Androidapi.JNI.Support;

function TMainService.AndroidServiceStartCommand(const Sender: TObject;
     const Intent: JIntent; Flags, StartId: Integer): Integer;
var
  ExtraData: String;
  {$ifdef VER330}
  ServiceChannel: JNotificationChannel;
  NotificationManager: JNotificationManager;
  Obj: JObject;
  {$endif}
  NewIntent: JIntent;
  ncb: JNotificationCompat_Builder;
  ntf: JNotification;
  PendingIntent: JPendingIntent;

begin

   Result := TJService.JavaClass.START_NOT_STICKY;

   // can't ref .O on earlier phones, must hardcode
  if TJBuild_VERSION.JavaClass.SDK_INT > 26 then // JBuild_VERSION_CODES.JavaClass.O                    begin
    {$ifdef VER330}
    // new ways for SDK > 26 (won't be called when API < 26 anyways)
    ServiceChannel := TJNotificationChannel.JavaClass.init(
      StringtoJString(CHANNEL_ID),
      StrToJCharSequence('My Service Channel'),
      TJNotificationManager.JavaClass.IMPORTANCE_DEFAULT
    );

    Obj := TAndroidHelper.Context.getSystemService(
    TJContext.JavaClass.NOTIFICATION_SERVICE);
    NotificationManager := TJNotificationManager.Wrap(Obj);
    NotificationManager.createNotificationChannel(ServiceChannel);

    NewIntent:= TAndroidHelper.Context.getPackageManager().getLaunchIntentForPackage(
      TAndroidHelper.Context.getPackageName());
    NewIntent.setAction(TJIntent.JavaClass.ACTION_MAIN);
    NewIntent.addCategory(TJIntent.JavaClass.CATEGORY_LAUNCHER);
    NewIntent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent := TJPendingIntent.JavaClass.getActivity(
      TAndroidHelper.Context, 0, NewIntent,
      TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK
    );

    ncb := TJNotificationCompat_Builder.JavaClass.init(
      TAndroidHelper.Context,
      StringToJString(CHANNEL_ID)
    );

   ncb.setContentTitle(StrToJCharSequence('MyService'));
   // ncb.setTicker(StrToJCharSequence('MyCommsService')); // can't remember why this is commented out to be honest
   ncb.setSmallIcon(JavaService.getApplicationInfo.icon);
   ncb.setContentIntent(PendingIntent);
   ncb.setOngoing(True);
   ntf := ncb.build;
   {$endif VER330}
 end
 else
 begin
   {$ifdef ORDINARY_NOTIFICATION}
    PendingIntent := TJPendingIntent.JavaClass.getActivity(
      JavaService.getApplicationContext, 0, Intent, 0
    );
    ntf := TJNotification.Create;
    ntf.icon := JavaService.getApplicationInfo.icon;
    ntf.setLatestEventInfo(
      JavaService.getApplicationContext,
      StrToJCharSequence('MyService'),
      StrToJCharSequence('MyCommsService'), PendingIntent);
    {$endif}
    {$ifdef CLICKABLE_NOTIFICATION}
    NewIntent:= TAndroidHelper.Context.getPackageManager().getLaunchIntentForPackage(
      TAndroidHelper.Context.getPackageName());
    NewIntent.setAction(TJIntent.JavaClass.ACTION_MAIN);
    NewIntent.addCategory(TJIntent.JavaClass.CATEGORY_LAUNCHER);
    NewIntent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent := TJPendingIntent.JavaClass.getActivity(
      TAndroidHelper.Context, 0, NewIntent,
      TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK
    );

    ncb := TJNotificationCompat_Builder.JavaClass.init(TAndroidHelper.Context);
    ncb.setContentTitle(StrToJCharSequence('MyService'));
    ncb.setTicker(StrToJCharSequence('MyCommsService'));
    ncb.setSmallIcon(JavaService.getApplicationInfo.icon);
    ncb.setContentIntent(PendingIntent);
    ncb.setOngoing(True);
    ntf := ncb.build;
    {$endif}
  end;

  JavaService.startForeground(StartId, ntf);

  if Intent <> nil then
  begin
     ExtraData := TAndroidHelper.JStringToString(
       Intent.getStringExtra(TAndroidHelper.StringToJString('ExtraData')));
  end;
end;

但是,正如我所说,当邮件到达收件人时,它们会显示主机名和一个完全不同的邮件地址,就像我在 $config = Array( 'protocol' => 'ssmtp', 'smtp_host' => 'ssl://ssmtp.googlemail.com', 'smtp_port' => 465, 'smtp_user' => 'mail@domainiwant.com', 'smtp_pass' => 'password', 'mailtype' => 'html', 'charset' => 'iso-8859-1', 'useragent' => 'MY NAME', ); $this->load->library('email', $config); $this->email->set_newline("\r\n"); $this->email->from('mail@domainiwant.com', 'MY NAME'); $email_to = 'receiver@gmail.com'; $this->email->to($email_to); $this->email->message('Message testing ...'); $this->email->send(); 上输入的一样。

我知道这只会设置$config,但我想将邮件地址设置为the envelope sender,而不是使用mail@domainiwant.com接收邮件

1 个答案:

答案 0 :(得分:1)

根据HERE提供的CodeIgniter电子邮件库的文档,您的整个问题就是一个简单的错字。

$config['protocol']允许mailsendmailsmtp作为值。如果您未设置变量或使用不允许的值,则整个库默认为mail,它将尝试使用您自己的服务器作为邮件网关(这说明了为什么发件人地址显示为{{ 1}})

将协议从username@servername更改为ssmtp,以便您实际使用要使用的Google SMTP服务器,并获得期望的结果